get_oid_hex_segment(): don't pad the rest of `oid`

Remove the feature of `get_oid_hex_segment()` that it pads the rest of the `oid` argument with zeros. Instead, do this at the caller who needs it. This makes the functionality of this function more coherent and removes the need for its `oid_len` argument. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Aug 26, 2017 at 10:28 UTC d49852d6f89bfde3cb20b6ca5866f67f6c04a894
1 file changed +13 -11
notes.c
+13 -11
@@ -339,15 +339,14 @@ static void note_tree_free(struct int_node *tree)
339 * - hex - Partial SHA1 segment in ASCII hex format
340 * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
341 * - oid - Partial SHA1 value is written here
342 - * - oid_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
342 * Return 0 on success or -1 on error (invalid arguments or input not
344 - * in hex format). Pad oid with NULs up to oid_len.
343 + * in hex format).
344 */
345 static int get_oid_hex_segment(const char *hex, unsigned int hex_len,
347 - unsigned char *oid, unsigned int oid_len)
346 + unsigned char *oid)
347 {
348 unsigned int i, len = hex_len >> 1;
350 - if (hex_len % 2 != 0 || len > oid_len)
349 + if (hex_len % 2 != 0)
350 return -1;
351 for (i = 0; i < len; i++) {
352 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
@@ -356,8 +355,6 @@ static int get_oid_hex_segment(const char *hex, unsigned int hex_len,
355 *oid++ = val;
356 hex += 2;
357 }
359 - for (; i < oid_len; i++)
360 - *oid++ = 0;
358 return 0;
359 }
360
@@ -442,24 +439,29 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
439 goto handle_non_note;
440
441 if (get_oid_hex_segment(entry.path, path_len,
445 - object_oid.hash + prefix_len,
446 - GIT_SHA1_RAWSZ - prefix_len))
442 + object_oid.hash + prefix_len))
443 goto handle_non_note; /* entry.path is not a SHA1 */
444
445 type = PTR_TYPE_NOTE;
446 } else if (path_len == 2) {
447 /* This is potentially an internal node */
448 + size_t len = prefix_len;
449
450 if (!S_ISDIR(entry.mode))
451 /* internal nodes must be trees */
452 goto handle_non_note;
453
454 if (get_oid_hex_segment(entry.path, 2,
458 - object_oid.hash + prefix_len,
459 - GIT_SHA1_RAWSZ - prefix_len))
455 + object_oid.hash + len++))
456 goto handle_non_note; /* entry.path is not a SHA1 */
457
462 - object_oid.hash[KEY_INDEX] = (unsigned char) (prefix_len + 1);
458 + /*
459 + * Pad the rest of the SHA-1 with zeros,
460 + * except for the last byte, where we write
461 + * the length:
462 + */
463 + memset(object_oid.hash + len, 0, GIT_SHA1_RAWSZ - len - 1);
464 + object_oid.hash[KEY_INDEX] = (unsigned char)len;
465
466 type = PTR_TYPE_SUBTREE;
467 } else {