提交 02e32b7d 编写于 作者: J Jeff King 提交者: Junio C Hamano

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: NJeff King <peff@peff.net>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 7ce4fb94
......@@ -539,6 +539,9 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
return fanout + 1;
}
/* hex SHA1 + 19 * '/' + NUL */
#define FANOUT_PATH_MAX 40 + 19 + 1
static void construct_path_with_fanout(const unsigned char *sha1,
unsigned char fanout, char *path)
{
......@@ -551,7 +554,7 @@ static void construct_path_with_fanout(const unsigned char *sha1,
path[i++] = '/';
fanout--;
}
strcpy(path + i, hex_sha1 + j);
xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_sha1 + j);
}
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,
void *p;
int ret = 0;
struct leaf_node *l;
static char path[40 + 19 + 1]; /* hex SHA1 + 19 * '/' + NUL */
static char path[FANOUT_PATH_MAX];
fanout = determine_fanout(tree, n, fanout);
for (i = 0; i < 16; i++) {
......@@ -595,7 +598,7 @@ static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
/* invoke callback with subtree */
unsigned int path_len =
l->key_sha1[19] * 2 + fanout;
assert(path_len < 40 + 19);
assert(path_len < FANOUT_PATH_MAX - 1);
construct_path_with_fanout(l->key_sha1, fanout,
path);
/* Create trailing slash, if needed */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册