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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 24, 2015 at 17:08 UTC
02e32b7debbcbe5910c11a515801751b349577d7
1 file changed
+6
-3
notes.c
+6
-3
@@ -539,6 +539,9 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
539
return fanout + 1;
540
}
541
542
+/* hex SHA1 + 19 * '/' + NUL */
543
+#define FANOUT_PATH_MAX 40 + 19 + 1
544
+
545
static void construct_path_with_fanout(const unsigned char *sha1,
546
unsigned char fanout, char *path)
547
{
@@ -551,7 +554,7 @@ static void construct_path_with_fanout(const unsigned char *sha1,
554
path[i++] = '/';
555
fanout--;
556
}
554
- strcpy(path + i, hex_sha1 + j);
557
+ xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_sha1 + j);
558
}
559
560
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,
565
void *p;
566
int ret = 0;
567
struct leaf_node *l;
565
- static char path[40 + 19 + 1]; /* hex SHA1 + 19 * '/' + NUL */
568
+ static char path[FANOUT_PATH_MAX];
569
570
fanout = determine_fanout(tree, n, fanout);
571
for (i = 0; i < 16; i++) {
@@ -595,7 +598,7 @@ redo:
598
/* invoke callback with subtree */
599
unsigned int path_len =
600
l->key_sha1[19] * 2 + fanout;
598
- assert(path_len < 40 + 19);
601
+ assert(path_len < FANOUT_PATH_MAX - 1);
602
construct_path_with_fanout(l->key_sha1, fanout,
603
path);
604
/* Create trailing slash, if needed */