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 <peff@peff.net>
Jeff King committed
Mar 28, 2017 at 15:46 UTC
903fc7da44b091a87c4bc00a892930786c71a709
1 file changed
+12
-9
builtin/name-rev.c
+12
-9
@@ -238,10 +238,9 @@ static const char *get_exact_ref_match(const struct object *o)
238
return NULL;
239
}
240
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)
243
{
244
- static char buffer[1024];
244
struct rev_name *n;
245
struct commit *c;
246
@@ -258,10 +257,9 @@ static const char *get_rev_name(const struct object *o)
257
int len = strlen(n->tip_name);
258
if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
259
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;
263
}
264
}
265
@@ -271,10 +269,11 @@ static void show_name(const struct object *obj,
269
{
270
const char *name;
271
const struct object_id *oid = &obj->oid;
272
+ struct strbuf buf = STRBUF_INIT;
273
274
if (!name_only)
275
printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
277
- name = get_rev_name(obj);
276
+ name = get_rev_name(obj, &buf);
277
if (name)
278
printf("%s\n", name);
279
else if (allow_undefined)
@@ -283,6 +282,7 @@ static void show_name(const struct object *obj,
282
printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
283
else
284
die("cannot describe '%s'", oid_to_hex(oid));
285
+ strbuf_release(&buf);
286
}
287
288
static char const * const name_rev_usage[] = {
@@ -294,6 +294,7 @@ static char const * const name_rev_usage[] = {
294
295
static void name_rev_line(char *p, struct name_ref_data *data)
296
{
297
+ struct strbuf buf = STRBUF_INIT;
298
int forty = 0;
299
char *p_start;
300
for (p_start = p; *p; p++) {
@@ -314,7 +315,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
315
struct object *o =
316
lookup_object(sha1);
317
if (o)
317
- name = get_rev_name(o);
318
+ name = get_rev_name(o, &buf);
319
}
320
*(p+1) = c;
321
@@ -332,6 +333,8 @@ static void name_rev_line(char *p, struct name_ref_data *data)
333
/* flush */
334
if (p_start != p)
335
fwrite(p_start, p - p_start, 1, stdout);
336
+
337
+ strbuf_release(&buf);
338
}
339
340
int cmd_name_rev(int argc, const char **argv, const char *prefix)