Skip to content

Commit e317cfa

Browse files
jasamplergitster
authored andcommitted
builtin-tag.c: Fix two memory leaks and minor notation changes.
A repeated call to read_sha1_file was not freing memory when the buffer was allocated but returned size was zero. Also, now the program does not allow many -F or -m options, which was a bug too because it was not freing the memory allocated for any previous -F or -m options. Tests are provided for ensuring that only one option -F or -m is given. Also, another test is shipped here, to check that "git tag" fails when a non-existing file is passed to the -F option, something that git-tag.sh allowed creating the tag with an empty message. Signed-off-by: Carlos Rica <jasampler@gmail.com> Acked-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4d87b9c commit e317cfa

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

builtin-tag.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,19 @@ static int show_reference(const char *refname, const unsigned char *sha1,
8989
printf("%-15s ", refname);
9090

9191
sp = buf = read_sha1_file(sha1, &type, &size);
92-
if (!buf || !size)
92+
if (!buf)
9393
return 0;
94+
if (!size) {
95+
free(buf);
96+
return 0;
97+
}
9498
/* skip header */
9599
while (sp + 1 < buf + size &&
96100
!(sp[0] == '\n' && sp[1] == '\n'))
97101
sp++;
98102
/* only take up to "lines" lines, and strip the signature */
99-
for (i = 0, sp += 2; i < filter->lines && sp < buf + size &&
103+
for (i = 0, sp += 2;
104+
i < filter->lines && sp < buf + size &&
100105
prefixcmp(sp, PGP_SIGNATURE "\n");
101106
i++) {
102107
if (i)
@@ -137,10 +142,10 @@ static int list_tags(const char *pattern, int lines)
137142
return 0;
138143
}
139144

140-
typedef int (*func_tag)(const char *name, const char *ref,
145+
typedef int (*each_tag_name_fn)(const char *name, const char *ref,
141146
const unsigned char *sha1);
142147

143-
static int do_tag_names(const char **argv, func_tag fn)
148+
static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
144149
{
145150
const char **p;
146151
char ref[PATH_MAX];
@@ -195,7 +200,7 @@ static ssize_t do_sign(char *buffer, size_t size, size_t max)
195200

196201
if (!*signingkey) {
197202
if (strlcpy(signingkey, git_committer_info(1),
198-
sizeof(signingkey)) >= sizeof(signingkey))
203+
sizeof(signingkey)) > sizeof(signingkey) - 1)
199204
return error("committer info too long.");
200205
bracket = strchr(signingkey, '>');
201206
if (bracket)
@@ -258,7 +263,7 @@ static void create_tag(const unsigned char *object, const char *tag,
258263
unsigned long size = 0;
259264

260265
type = sha1_object_info(object, NULL);
261-
if (type <= 0)
266+
if (type <= OBJ_NONE)
262267
die("bad object type.");
263268

264269
header_len = snprintf(header_buf, sizeof(header_buf),
@@ -271,7 +276,7 @@ static void create_tag(const unsigned char *object, const char *tag,
271276
tag,
272277
git_committer_info(1));
273278

274-
if (header_len >= sizeof(header_buf))
279+
if (header_len > sizeof(header_buf) - 1)
275280
die("tag header too big.");
276281

277282
if (!message) {
@@ -366,6 +371,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
366371
i++;
367372
if (i == argc)
368373
die("option -m needs an argument.");
374+
if (message)
375+
die("only one -F or -m option is allowed.");
369376
message = xstrdup(argv[i]);
370377
continue;
371378
}
@@ -377,6 +384,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
377384
i++;
378385
if (i == argc)
379386
die("option -F needs an argument.");
387+
if (message)
388+
die("only one -F or -m option is allowed.");
380389

381390
if (!strcmp(argv[i], "-"))
382391
fd = 0;
@@ -405,15 +414,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
405414
die("argument to option -u too long");
406415
continue;
407416
}
408-
if (!strcmp(arg, "-l")) {
417+
if (!strcmp(arg, "-l"))
409418
return list_tags(argv[i + 1], lines);
410-
}
411-
if (!strcmp(arg, "-d")) {
412-
return do_tag_names(argv + i + 1, delete_tag);
413-
}
414-
if (!strcmp(arg, "-v")) {
415-
return do_tag_names(argv + i + 1, verify_tag);
416-
}
419+
if (!strcmp(arg, "-d"))
420+
return for_each_tag_name(argv + i + 1, delete_tag);
421+
if (!strcmp(arg, "-v"))
422+
return for_each_tag_name(argv + i + 1, verify_tag);
417423
usage(builtin_tag_usage);
418424
}
419425

@@ -431,7 +437,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
431437
if (get_sha1(object_ref, object))
432438
die("Failed to resolve '%s' as a valid ref.", object_ref);
433439

434-
if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) >= sizeof(ref))
440+
if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
435441
die("tag name too long: %.*s...", 50, tag);
436442
if (check_ref_format(ref))
437443
die("'%s' is not a valid tag name.", tag);

t/t7004-tag.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,33 @@ test_expect_success 'creating an annotated tag with -F - should succeed' '
332332
git diff expect actual
333333
'
334334

335+
test_expect_success \
336+
'trying to create a tag with a non-existing -F file should fail' '
337+
! test -f nonexistingfile &&
338+
! tag_exists notag &&
339+
! git-tag -F nonexistingfile notag &&
340+
! tag_exists notag
341+
'
342+
343+
test_expect_success \
344+
'trying to create tags giving many -m or -F options should fail' '
345+
echo "message file 1" >msgfile1 &&
346+
echo "message file 2" >msgfile2 &&
347+
! tag_exists msgtag &&
348+
! git-tag -m "message 1" -m "message 2" msgtag &&
349+
! tag_exists msgtag &&
350+
! git-tag -F msgfile1 -F msgfile2 msgtag &&
351+
! tag_exists msgtag &&
352+
! git-tag -m "message 1" -F msgfile1 msgtag &&
353+
! tag_exists msgtag &&
354+
! git-tag -F msgfile1 -m "message 1" msgtag &&
355+
! tag_exists msgtag &&
356+
! git-tag -F msgfile1 -m "message 1" -F msgfile2 msgtag &&
357+
! tag_exists msgtag &&
358+
! git-tag -m "message 1" -F msgfile1 -m "message 2" msgtag &&
359+
! tag_exists msgtag
360+
'
361+
335362
# blank and empty messages:
336363

337364
get_tag_header empty-annotated-tag $commit commit $time >expect
@@ -648,6 +675,14 @@ test_expect_success 'creating a signed tag with -F - should succeed' '
648675
git diff expect actual
649676
'
650677

678+
test_expect_success \
679+
'trying to create a signed tag with non-existing -F file should fail' '
680+
! test -f nonexistingfile &&
681+
! tag_exists nosigtag &&
682+
! git-tag -s -F nonexistingfile nosigtag &&
683+
! tag_exists nosigtag
684+
'
685+
651686
test_expect_success 'verifying a signed tag should succeed' \
652687
'git-tag -v signed-tag'
653688

0 commit comments

Comments
 (0)