type_from_string_gently: make sure length matches

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>

Jeff King committed Apr 17, 2015 at 10:52 UTC b7994af0f92e6cf150544aebe3c3fc526faaf2c3
2 files changed +10 -1
object.c
+2 -1
@@ -41,7 +41,8 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
41 len = strlen(str);
42
43 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
44 - if (!strncmp(str, object_type_strings[i], len))
44 + if (!strncmp(str, object_type_strings[i], len) &&
45 + object_type_strings[i][len] == '\0')
46 return i;
47
48 if (gentle)
t/t1007-hash-object.sh
+8
@@ -201,4 +201,12 @@ test_expect_success 'corrupt tag' '
201 test_must_fail git hash-object -t tag --stdin </dev/null
202 '
203
204 +test_expect_success 'hash-object complains about bogus type name' '
205 + test_must_fail git hash-object -t bogus --stdin </dev/null
206 +'
207 +
208 +test_expect_success 'hash-object complains about truncated type name' '
209 + test_must_fail git hash-object -t bl --stdin </dev/null
210 +'
211 +
212 test_done