Skip to content

Commit d49852d

Browse files
mhaggergitster
authored andcommitted
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>
1 parent 4ebef53 commit d49852d

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

notes.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,14 @@ static void note_tree_free(struct int_node *tree)
339339
* - hex - Partial SHA1 segment in ASCII hex format
340340
* - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
341341
* - oid - Partial SHA1 value is written here
342-
* - oid_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
343342
* 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).
345344
*/
346345
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)
348347
{
349348
unsigned int i, len = hex_len >> 1;
350-
if (hex_len % 2 != 0 || len > oid_len)
349+
if (hex_len % 2 != 0)
351350
return -1;
352351
for (i = 0; i < len; i++) {
353352
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,
356355
*oid++ = val;
357356
hex += 2;
358357
}
359-
for (; i < oid_len; i++)
360-
*oid++ = 0;
361358
return 0;
362359
}
363360

@@ -442,24 +439,29 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
442439
goto handle_non_note;
443440

444441
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))
447443
goto handle_non_note; /* entry.path is not a SHA1 */
448444

449445
type = PTR_TYPE_NOTE;
450446
} else if (path_len == 2) {
451447
/* This is potentially an internal node */
448+
size_t len = prefix_len;
452449

453450
if (!S_ISDIR(entry.mode))
454451
/* internal nodes must be trees */
455452
goto handle_non_note;
456453

457454
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++))
460456
goto handle_non_note; /* entry.path is not a SHA1 */
461457

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;
463465

464466
type = PTR_TYPE_SUBTREE;
465467
} else {

0 commit comments

Comments
 (0)