summaryrefslogtreecommitdiff
path: root/read-cache.c
diff options
context:
space:
mode:
authorPetr Baudis <pasky@ucw.cz>2005-04-13 09:14:06 (GMT)
committerPetr Baudis <xpasky@machine>2005-04-13 09:14:06 (GMT)
commit5c2a7fbc362e4227ced84c32c3fdc9682d085962 (patch)
tree958b0946a5082fbc6797209fb225f5d7a786b786 /read-cache.c
parent7912c07037cf704394e9bcb7cb24c05ee03aa921 (diff)
downloadgit-5c2a7fbc362e4227ced84c32c3fdc9682d085962.zip
git-5c2a7fbc362e4227ced84c32c3fdc9682d085962.tar.gz
git-5c2a7fbc362e4227ced84c32c3fdc9682d085962.tar.bz2
[PATCH] SHA1 naive collision checking
When compiled with -DCOLLISION_CHECK, we will check against SHA1 collisions when writing to the object database. From: Christopher Li <chrislgit@chrisli.org> Signed-off-by: Petr Baudis <pasky@ucw.cz>
Diffstat (limited to 'read-cache.c')
-rw-r--r--read-cache.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/read-cache.c b/read-cache.c
index 5453694..2ee96bc 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -216,8 +216,25 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
int fd;
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
- if (fd < 0)
- return (errno == EEXIST) ? 0 : -1;
+ if (fd < 0) {
+ void *map;
+ static int error(const char * string);
+
+ if (errno != EEXIST)
+ return -1;
+#ifndef COLLISION_CHECK
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return -1;
+ map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (map == MAP_FAILED)
+ return -1;
+ if (memcmp(buf, map, size))
+ return error("SHA1 collision detected!"
+ " This is bad, bad, BAD!\a\n");
+#endif
+ return 0;
+ }
write(fd, buf, size);
close(fd);
return 0;