Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit e6dfcd6

Browse files
peffgitster
authored andcommitted
replace dangerous uses of strbuf_attach
It is not a good idea to strbuf_attach an arbitrary pointer just because a function you are calling wants a strbuf. Attaching implies a transfer of memory ownership; if anyone were to modify or release the resulting strbuf, we would free() the pointer, leading to possible problems: 1. Other users of the original pointer might access freed memory. 2. The pointer might not be the start of a malloc'd area, so calling free() on it in the first place would be wrong. In the two cases modified here, we are fortunate that nobody touches the strbuf once it is attached, but it is an accident waiting to happen. Since the previous commit, commit_tree and friends take a pointer/buf pair, so we can just do away with the strbufs entirely. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ffefb5 commit e6dfcd6

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

notes-cache.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ int notes_cache_write(struct notes_cache *c)
4848
{
4949
unsigned char tree_sha1[20];
5050
unsigned char commit_sha1[20];
51-
struct strbuf msg = STRBUF_INIT;
5251

5352
if (!c || !c->tree.initialized || !c->tree.ref || !*c->tree.ref)
5453
return -1;
@@ -57,9 +56,8 @@ int notes_cache_write(struct notes_cache *c)
5756

5857
if (write_notes_tree(&c->tree, tree_sha1))
5958
return -1;
60-
strbuf_attach(&msg, c->validity,
61-
strlen(c->validity), strlen(c->validity) + 1);
62-
if (commit_tree(msg.buf, msg.len, tree_sha1, NULL, commit_sha1, NULL, NULL) < 0)
59+
if (commit_tree(c->validity, strlen(c->validity), tree_sha1, NULL,
60+
commit_sha1, NULL, NULL) < 0)
6361
return -1;
6462
if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL,
6563
0, QUIET_ON_ERR) < 0)

notes-merge.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,6 @@ int notes_merge_commit(struct notes_merge_options *o,
673673
struct dirent *e;
674674
struct strbuf path = STRBUF_INIT;
675675
char *msg = strstr(partial_commit->buffer, "\n\n");
676-
struct strbuf sb_msg = STRBUF_INIT;
677676
int baselen;
678677

679678
strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
@@ -720,10 +719,8 @@ int notes_merge_commit(struct notes_merge_options *o,
720719
strbuf_setlen(&path, baselen);
721720
}
722721

723-
strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
724722
create_notes_commit(partial_tree, partial_commit->parents,
725-
sb_msg.buf, sb_msg.len,
726-
result_sha1);
723+
msg, strlen(msg), result_sha1);
727724
if (o->verbosity >= 4)
728725
printf("Finalized notes merge commit: %s\n",
729726
sha1_to_hex(result_sha1));

0 commit comments

Comments
 (0)