Skip to content

Commit 02e32b7

Browse files
peffgitster
authored andcommitted
notes: document length of fanout path with a constant
We know that a fanned-out sha1 in a notes tree cannot be more than "aa/bb/cc/...", and we have an assert() to confirm that. But let's factor out that length into a constant so we can be sure it is used consistently. And even though we assert() earlier, let's replace a strcpy with xsnprintf, so it is clear to a reader that all cases are covered. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ce4fb9 commit 02e32b7

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

notes.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
539539
return fanout + 1;
540540
}
541541

542+
/* hex SHA1 + 19 * '/' + NUL */
543+
#define FANOUT_PATH_MAX 40 + 19 + 1
544+
542545
static void construct_path_with_fanout(const unsigned char *sha1,
543546
unsigned char fanout, char *path)
544547
{
@@ -551,7 +554,7 @@ static void construct_path_with_fanout(const unsigned char *sha1,
551554
path[i++] = '/';
552555
fanout--;
553556
}
554-
strcpy(path + i, hex_sha1 + j);
557+
xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_sha1 + j);
555558
}
556559

557560
static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
@@ -562,7 +565,7 @@ static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
562565
void *p;
563566
int ret = 0;
564567
struct leaf_node *l;
565-
static char path[40 + 19 + 1]; /* hex SHA1 + 19 * '/' + NUL */
568+
static char path[FANOUT_PATH_MAX];
566569

567570
fanout = determine_fanout(tree, n, fanout);
568571
for (i = 0; i < 16; i++) {
@@ -595,7 +598,7 @@ static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
595598
/* invoke callback with subtree */
596599
unsigned int path_len =
597600
l->key_sha1[19] * 2 + fanout;
598-
assert(path_len < 40 + 19);
601+
assert(path_len < FANOUT_PATH_MAX - 1);
599602
construct_path_with_fanout(l->key_sha1, fanout,
600603
path);
601604
/* Create trailing slash, if needed */

0 commit comments

Comments
 (0)