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>
Carlos Rica committed
Jul 21, 2007 at 14:13 UTC
e317cfafd247b279055e9ee64a6a982043bd06e7
2 files changed
+57
-16
builtin-tag.c
+22
-16
@@ -89,14 +89,19 @@ static int show_reference(const char *refname, const unsigned char *sha1,
89
printf("%-15s ", refname);
90
91
sp = buf = read_sha1_file(sha1, &type, &size);
92
- if (!buf || !size)
92
+ if (!buf)
93
return 0;
94
+ if (!size) {
95
+ free(buf);
96
+ return 0;
97
+ }
98
/* skip header */
99
while (sp + 1 < buf + size &&
100
!(sp[0] == '\n' && sp[1] == '\n'))
101
sp++;
102
/* 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 &&
105
prefixcmp(sp, PGP_SIGNATURE "\n");
106
i++) {
107
if (i)
@@ -137,10 +142,10 @@ static int list_tags(const char *pattern, int lines)
142
return 0;
143
}
144
140
-typedef int (*func_tag)(const char *name, const char *ref,
145
+typedef int (*each_tag_name_fn)(const char *name, const char *ref,
146
const unsigned char *sha1);
147
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)
149
{
150
const char **p;
151
char ref[PATH_MAX];
@@ -195,7 +200,7 @@ static ssize_t do_sign(char *buffer, size_t size, size_t max)
200
201
if (!*signingkey) {
202
if (strlcpy(signingkey, git_committer_info(1),
198
- sizeof(signingkey)) >= sizeof(signingkey))
203
+ sizeof(signingkey)) > sizeof(signingkey) - 1)
204
return error("committer info too long.");
205
bracket = strchr(signingkey, '>');
206
if (bracket)
@@ -258,7 +263,7 @@ static void create_tag(const unsigned char *object, const char *tag,
263
unsigned long size = 0;
264
265
type = sha1_object_info(object, NULL);
261
- if (type <= 0)
266
+ if (type <= OBJ_NONE)
267
die("bad object type.");
268
269
header_len = snprintf(header_buf, sizeof(header_buf),
@@ -271,7 +276,7 @@ static void create_tag(const unsigned char *object, const char *tag,
276
tag,
277
git_committer_info(1));
278
274
- if (header_len >= sizeof(header_buf))
279
+ if (header_len > sizeof(header_buf) - 1)
280
die("tag header too big.");
281
282
if (!message) {
@@ -366,6 +371,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
371
i++;
372
if (i == argc)
373
die("option -m needs an argument.");
374
+ if (message)
375
+ die("only one -F or -m option is allowed.");
376
message = xstrdup(argv[i]);
377
continue;
378
}
@@ -377,6 +384,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
384
i++;
385
if (i == argc)
386
die("option -F needs an argument.");
387
+ if (message)
388
+ die("only one -F or -m option is allowed.");
389
390
if (!strcmp(argv[i], "-"))
391
fd = 0;
@@ -405,15 +414,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
414
die("argument to option -u too long");
415
continue;
416
}
408
- if (!strcmp(arg, "-l")) {
417
+ if (!strcmp(arg, "-l"))
418
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);
423
usage(builtin_tag_usage);
424
}
425
@@ -431,7 +437,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
437
if (get_sha1(object_ref, object))
438
die("Failed to resolve '%s' as a valid ref.", object_ref);
439
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)
441
die("tag name too long: %.*s...", 50, tag);
442
if (check_ref_format(ref))
443
die("'%s' is not a valid tag name.", tag);
t/t7004-tag.sh
+35
@@ -332,6 +332,33 @@ test_expect_success 'creating an annotated tag with -F - should succeed' '
332
git diff expect actual
333
'
334
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
+
362
# blank and empty messages:
363
364
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' '
675
git diff expect actual
676
'
677
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
+
686
test_expect_success 'verifying a signed tag should succeed' \
687
'git-tag -v signed-tag'
688