Skip to content

Commit d72cae1

Browse files
peffgitster
authored andcommitted
get_sha1_with_context: always initialize oc->symlink_path
The get_sha1_with_context() function zeroes out the oc->symlink_path strbuf, but doesn't use strbuf_init() to set up the usual invariants (like pointing to the slopbuf). We don't actually write to the oc->symlink_path strbuf unless we call get_tree_entry_follow_symlinks(), and that function does initialize it. However, readers may still look at the zero'd strbuf. In practice this isn't a triggerable bug. The only caller that looks at it only does so when the mode we found is 0. This doesn't happen for non-tree-entries (where we return S_IFINVALID). A broken tree entry could have a mode of 0, but canon_mode() quietly rewrites that into S_IFGITLINK. So the "0" mode should only come up when we did indeed find a symlink. This is mostly just an accident of how the code happens to work, though. Let's future-proof ourselves to make sure the strbuf is properly initialized for all calls (it's only a few struct member assignments, not a heap allocation). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0a487e commit d72cae1

File tree

2 files changed

+1
-1
lines changed

2 files changed

+1
-1
lines changed

sha1_name.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,7 @@ static int get_sha1_with_context_1(const char *name,
15111511

15121512
memset(oc, 0, sizeof(*oc));
15131513
oc->mode = S_IFINVALID;
1514+
strbuf_init(&oc->symlink_path, 0);
15141515
ret = get_sha1_1(name, namelen, sha1, flags);
15151516
if (!ret)
15161517
return ret;

tree-walk.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,6 @@ enum follow_symlinks_result get_tree_entry_follow_symlinks(unsigned char *tree_s
589589
int i;
590590

591591
init_tree_desc(&t, NULL, 0UL);
592-
strbuf_init(result_path, 0);
593592
strbuf_addstr(&namebuf, name);
594593
hashcpy(current_tree_sha1, tree_sha1);
595594

0 commit comments

Comments
 (0)