]> granicus.if.org Git - git/commitdiff
type_from_string_gently: make sure length matches
authorJeff King <peff@peff.net>
Fri, 17 Apr 2015 14:52:48 +0000 (10:52 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 Apr 2015 20:54:39 +0000 (13:54 -0700)
When commit fe8e3b7 refactored type_from_string to allow
input that was not NUL-terminated, it switched to using
strncmp instead of strcmp. But this means we check only the
first "len" bytes of the strings, and ignore any remaining
bytes in the object_type_string. We should make sure that it
is also "len" bytes, or else we would accept "comm" as
"commit", and so forth.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object.c
t/t1007-hash-object.sh

index aedac243f0db05351a1ec7ae8b5ed297e51121e8..91bdde25307ebfd853082c7e38d3806b388b08d5 100644 (file)
--- a/object.c
+++ b/object.c
@@ -41,7 +41,8 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
                len = strlen(str);
 
        for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
-               if (!strncmp(str, object_type_strings[i], len))
+               if (!strncmp(str, object_type_strings[i], len) &&
+                   object_type_strings[i][len] == '\0')
                        return i;
 
        if (gentle)
index f83df8eb8b143086df00e163a30ad96e43404001..ebb3a69c8cd68a51184f809357bb60cb7b35f928 100755 (executable)
@@ -201,4 +201,12 @@ test_expect_success 'corrupt tag' '
        test_must_fail git hash-object -t tag --stdin </dev/null
 '
 
+test_expect_success 'hash-object complains about bogus type name' '
+       test_must_fail git hash-object -t bogus --stdin </dev/null
+'
+
+test_expect_success 'hash-object complains about truncated type name' '
+       test_must_fail git hash-object -t bl --stdin </dev/null
+'
+
 test_done