mirror of
https://github.com/git/git.git
synced 2024-05-19 16:26:09 +02:00
[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>
This commit is contained in:
parent
7912c07037
commit
5c2a7fbc36
4
Makefile
4
Makefile
|
@ -1,4 +1,8 @@
|
||||||
|
# -DCOLLISION_CHECK if you believe that SHA1's
|
||||||
|
# 1461501637330902918203684832716283019655932542976 hashes do not give you
|
||||||
|
# enough guarantees about no collisions between objects ever hapenning.
|
||||||
CFLAGS=-g -O3 -Wall
|
CFLAGS=-g -O3 -Wall
|
||||||
|
|
||||||
CC=gcc
|
CC=gcc
|
||||||
|
|
||||||
PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
|
PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
|
21
read-cache.c
21
read-cache.c
|
@ -216,8 +216,25 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
||||||
if (fd < 0)
|
if (fd < 0) {
|
||||||
return (errno == EEXIST) ? 0 : -1;
|
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);
|
write(fd, buf, size);
|
||||||
close(fd);
|
close(fd);
|
||||||
return 0;
|
return 0;
|
Loading…
Reference in New Issue