Skip to content

Commit b1ddfb9

Browse files
peffgitster
authored andcommitted
diff_populate_gitlink: use a strbuf
We allocate 100 bytes to hold the "Submodule commit ..." text. This is enough, but it's not immediately obvious that this is the case, and we have to repeat the magic 100 twice. We could get away with xstrfmt here, but we want to know the size, as well, so let's use a real strbuf. And while we're here, we can clean up the logic around size_only. It currently sets and clears the "data" field pointlessly, and leaves the "should_free" flag on even after we have cleared the data. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 21f9d0f commit b1ddfb9

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

diff.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -2704,21 +2704,21 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
27042704

27052705
static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
27062706
{
2707-
int len;
2708-
char *data = xmalloc(100), *dirty = "";
2707+
struct strbuf buf = STRBUF_INIT;
2708+
char *dirty = "";
27092709

27102710
/* Are we looking at the work tree? */
27112711
if (s->dirty_submodule)
27122712
dirty = "-dirty";
27132713

2714-
len = snprintf(data, 100,
2715-
"Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
2716-
s->data = data;
2717-
s->size = len;
2718-
s->should_free = 1;
2714+
strbuf_addf(&buf, "Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
2715+
s->size = buf.len;
27192716
if (size_only) {
27202717
s->data = NULL;
2721-
free(data);
2718+
strbuf_release(&buf);
2719+
} else {
2720+
s->data = strbuf_detach(&buf, NULL);
2721+
s->should_free = 1;
27222722
}
27232723
return 0;
27242724
}

0 commit comments

Comments
 (0)