Skip to content

Commit 903fc7d

Browse files
peffgitster
authored andcommitted
name-rev: replace static buffer with strbuf
When name-rev needs to format an actual name, we do so into a fixed-size buffer. That includes the actual ref tip, as well as any traversal information. Since refs can exceed 1024 bytes, this means you can get a bogus result. E.g., doing: git tag $(perl -e 'print join("/", 1..1024)') git describe --contains HEAD^ results in ".../282/283", when it should be ".../1023/1024~1". We can solve this by using a heap buffer. We'll use a strbuf, which lets us write into the same buffer from our loop without having to reallocate. Signed-off-by: Jeff King <[email protected]>
1 parent cddac45 commit 903fc7d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

builtin/name-rev.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ static const char *get_exact_ref_match(const struct object *o)
238238
return NULL;
239239
}
240240

241-
/* returns a static buffer */
242-
static const char *get_rev_name(const struct object *o)
241+
/* may return a constant string or use "buf" as scratch space */
242+
static const char *get_rev_name(const struct object *o, struct strbuf *buf)
243243
{
244-
static char buffer[1024];
245244
struct rev_name *n;
246245
struct commit *c;
247246

@@ -258,10 +257,9 @@ static const char *get_rev_name(const struct object *o)
258257
int len = strlen(n->tip_name);
259258
if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
260259
len -= 2;
261-
snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
262-
n->generation);
263-
264-
return buffer;
260+
strbuf_reset(buf);
261+
strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
262+
return buf->buf;
265263
}
266264
}
267265

@@ -271,10 +269,11 @@ static void show_name(const struct object *obj,
271269
{
272270
const char *name;
273271
const struct object_id *oid = &obj->oid;
272+
struct strbuf buf = STRBUF_INIT;
274273

275274
if (!name_only)
276275
printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
277-
name = get_rev_name(obj);
276+
name = get_rev_name(obj, &buf);
278277
if (name)
279278
printf("%s\n", name);
280279
else if (allow_undefined)
@@ -283,6 +282,7 @@ static void show_name(const struct object *obj,
283282
printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
284283
else
285284
die("cannot describe '%s'", oid_to_hex(oid));
285+
strbuf_release(&buf);
286286
}
287287

288288
static char const * const name_rev_usage[] = {
@@ -294,6 +294,7 @@ static char const * const name_rev_usage[] = {
294294

295295
static void name_rev_line(char *p, struct name_ref_data *data)
296296
{
297+
struct strbuf buf = STRBUF_INIT;
297298
int forty = 0;
298299
char *p_start;
299300
for (p_start = p; *p; p++) {
@@ -314,7 +315,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
314315
struct object *o =
315316
lookup_object(sha1);
316317
if (o)
317-
name = get_rev_name(o);
318+
name = get_rev_name(o, &buf);
318319
}
319320
*(p+1) = c;
320321

@@ -332,6 +333,8 @@ static void name_rev_line(char *p, struct name_ref_data *data)
332333
/* flush */
333334
if (p_start != p)
334335
fwrite(p_start, p - p_start, 1, stdout);
336+
337+
strbuf_release(&buf);
335338
}
336339

337340
int cmd_name_rev(int argc, const char **argv, const char *prefix)

0 commit comments

Comments
 (0)