Skip to content

Commit d4e85a1

Browse files
mhaggergitster
authored andcommitted
get_sha1_hex(): do not read past a NUL character
Previously, get_sha1_hex() would read one character past the end of a null-terminated string whose strlen was an even number less than 40. Although the function correctly returned -1 in these cases, the extra memory access might have been to uninitialized (or even, conceivably, unallocated) memory. Add a check to avoid reading past the end of a string. This problem was discovered by Thomas Rast <trast@student.ethz.ch> using valgrind. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3793ac5 commit d4e85a1

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,16 @@ static inline int get_sha1_with_context(const char *str, unsigned char *sha1, st
819819
{
820820
return get_sha1_with_context_1(str, sha1, orc, 0, NULL);
821821
}
822+
823+
/*
824+
* Try to read a SHA1 in hexadecimal format from the 40 characters
825+
* starting at hex. Write the 20-byte result to sha1 in binary form.
826+
* Return 0 on success. Reading stops if a NUL is encountered in the
827+
* input, so it is safe to pass this function an arbitrary
828+
* null-terminated string.
829+
*/
822830
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
831+
823832
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
824833
extern int read_ref(const char *filename, unsigned char *sha1);
825834
extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);

hex.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ int get_sha1_hex(const char *hex, unsigned char *sha1)
3939
{
4040
int i;
4141
for (i = 0; i < 20; i++) {
42-
unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
42+
unsigned int val;
43+
/*
44+
* hex[1]=='\0' is caught when val is checked below,
45+
* but if hex[0] is NUL we have to avoid reading
46+
* past the end of the string:
47+
*/
48+
if (!hex[0])
49+
return -1;
50+
val = (hexval(hex[0]) << 4) | hexval(hex[1]);
4351
if (val & ~0xff)
4452
return -1;
4553
*sha1++ = val;

0 commit comments

Comments
 (0)