Skip to content

Commit 5c2a7fb

Browse files
paskyPetr Baudis
authored andcommitted
[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 <[email protected]> Signed-off-by: Petr Baudis <[email protected]>
1 parent 7912c07 commit 5c2a7fb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
# -DCOLLISION_CHECK if you believe that SHA1's
2+
# 1461501637330902918203684832716283019655932542976 hashes do not give you
3+
# enough guarantees about no collisions between objects ever hapenning.
14
CFLAGS=-g -O3 -Wall
5+
26
CC=gcc
37

48
PROG= update-cache show-diff init-db write-tree read-tree commit-tree \

read-cache.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,25 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
216216
int fd;
217217

218218
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
219-
if (fd < 0)
220-
return (errno == EEXIST) ? 0 : -1;
219+
if (fd < 0) {
220+
void *map;
221+
static int error(const char * string);
222+
223+
if (errno != EEXIST)
224+
return -1;
225+
#ifndef COLLISION_CHECK
226+
fd = open(filename, O_RDONLY);
227+
if (fd < 0)
228+
return -1;
229+
map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
230+
if (map == MAP_FAILED)
231+
return -1;
232+
if (memcmp(buf, map, size))
233+
return error("SHA1 collision detected!"
234+
" This is bad, bad, BAD!\a\n");
235+
#endif
236+
return 0;
237+
}
221238
write(fd, buf, size);
222239
close(fd);
223240
return 0;

0 commit comments

Comments
 (0)