@matheusbsbs / matheus-bagliano / commits / ba26ab99d4
Fork 0

mktag.c: tweak validation of tagger field and adjust test script

Update the verify_tag() function to remove an unnecessary test, and add additional check for angle brackets in the name and email field, and spaces in the email field. The timestamp and timezone sections are made more straight forward by using strspn(). Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Mar 31, 2008 at 18:25 UTC ba26ab99d4c6c6a8f939f44cf2f2c47f6fd4f0a1
2 files changed +86 -31
mktag.c
+14 -15
@@ -45,6 +45,7 @@ static int verify_tag(char *buffer, unsigned long size)
45 char type[20];
46 unsigned char sha1[20];
47 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
48 + size_t len;
49
50 if (size < 84)
51 return error("wanna fool me ? you obviously got the size wrong !");
@@ -98,18 +99,21 @@ static int verify_tag(char *buffer, unsigned long size)
99 /* Verify the tagger line */
100 tagger_line = tag_line;
101
101 - if (memcmp(tagger_line, "tagger ", 7) || (tagger_line[7] == '\n'))
102 + if (memcmp(tagger_line, "tagger ", 7))
103 return error("char" PD_FMT ": could not find \"tagger \"",
104 tagger_line - buffer);
105
106 /*
107 * Check for correct form for name and email
108 * i.e. " <" followed by "> " on _this_ line
109 + * No angle brackets within the name or email address fields.
110 + * No spaces within the email address field.
111 */
112 tagger_line += 7;
113 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
111 - strchr(tagger_line, '\n') < rb)
112 - return error("char" PD_FMT ": malformed tagger",
114 + strpbrk(tagger_line, "<>\n") != lb+1 ||
115 + strpbrk(lb+2, "><\n ") != rb)
116 + return error("char" PD_FMT ": malformed tagger field",
117 tagger_line - buffer);
118
119 /* Check for author name, at least one character, space is acceptable */
@@ -117,25 +121,20 @@ static int verify_tag(char *buffer, unsigned long size)
121 return error("char" PD_FMT ": missing tagger name",
122 tagger_line - buffer);
123
120 - /* timestamp */
124 + /* timestamp, 1 or more digits followed by space */
125 tagger_line = rb + 2;
122 - if (*tagger_line == ' ')
123 - return error("char" PD_FMT ": malformed tag timestamp",
126 + if (!(len = strspn(tagger_line, "0123456789")))
127 + return error("char" PD_FMT ": missing tag timestamp",
128 tagger_line - buffer);
125 - for (;;) {
126 - unsigned char c = *tagger_line++;
127 - if (c == ' ')
128 - break;
129 - if (isdigit(c))
130 - continue;
129 + tagger_line += len;
130 + if (*tagger_line != ' ')
131 return error("char" PD_FMT ": malformed tag timestamp",
132 tagger_line - buffer);
133 - }
133 + tagger_line++;
134
135 /* timezone, 5 digits [+-]hhmm, max. 1400 */
136 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
137 - isdigit(tagger_line[1]) && isdigit(tagger_line[2]) &&
138 - isdigit(tagger_line[3]) && isdigit(tagger_line[4]) &&
137 + strspn(tagger_line+1, "0123456789") == 4 &&
138 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
139 return error("char" PD_FMT ": malformed tag timezone",
140 tagger_line - buffer);
t/t3800-mktag.sh
+72 -16
@@ -180,7 +180,7 @@ check_verify_failure '"tagger" line label check #2' \
180 '^error: char70: could not find "tagger "$'
181
182 ############################################################
183 -# 13. detect missing tag author name
183 +# 13. disallow missing tag author name
184
185 cat >tag.sig <<EOF
186 object $head
@@ -191,11 +191,11 @@ tagger <> 0 +0000
191 This is filler
192 EOF
193
194 -check_verify_failure 'detect missing tag author name' \
194 +check_verify_failure 'disallow missing tag author name' \
195 '^error: char77: missing tagger name$'
196
197 ############################################################
198 -# 14. detect missing tag author name
198 +# 14. disallow missing tag author name
199
200 cat >tag.sig <<EOF
201 object $head
@@ -206,8 +206,8 @@ tagger T A Gger <
206
207 EOF
208
209 -check_verify_failure 'detect malformed tagger' \
210 - '^error: char77: malformed tagger$'
209 +check_verify_failure 'disallow malformed tagger' \
210 + '^error: char77: malformed tagger field$'
211
212 ############################################################
213 # 15. allow empty tag email
@@ -225,7 +225,21 @@ test_expect_success \
225 'git-mktag <tag.sig >.git/refs/tags/mytag 2>message'
226
227 ############################################################
228 -# 16. detect missing tag timestamp
228 +# 16. disallow spaces in tag email
229 +
230 +cat >tag.sig <<EOF
231 +object $head
232 +type commit
233 +tag mytag
234 +tagger T A Gger <tag ger@example.com> 0 +0000
235 +
236 +EOF
237 +
238 +check_verify_failure 'disallow spaces in tag email' \
239 + '^error: char77: malformed tagger field$'
240 +
241 +############################################################
242 +# 17. disallow missing tag timestamp
243
244 cat >tag.sig <<EOF
245 object $head
@@ -235,11 +249,11 @@ tagger T A Gger <tagger@example.com>
249
250 EOF
251
238 -check_verify_failure 'detect missing tag timestamp' \
239 - '^error: char107: malformed tag timestamp$'
252 +check_verify_failure 'disallow missing tag timestamp' \
253 + '^error: char107: missing tag timestamp$'
254
255 ############################################################
242 -# 17. detect invalid tag timestamp
256 +# 18. detect invalid tag timestamp1
257
258 cat >tag.sig <<EOF
259 object $head
@@ -249,11 +263,25 @@ tagger T A Gger <tagger@example.com> Tue Mar 25 15:47:44 2008
263
264 EOF
265
252 -check_verify_failure 'detect invalid tag timestamp' \
253 - '^error: char108: malformed tag timestamp$'
266 +check_verify_failure 'detect invalid tag timestamp1' \
267 + '^error: char107: missing tag timestamp$'
268
269 ############################################################
256 -# 18. detect invalid tag timezone
270 +# 19. detect invalid tag timestamp2
271 +
272 +cat >tag.sig <<EOF
273 +object $head
274 +type commit
275 +tag mytag
276 +tagger T A Gger <tagger@example.com> 2008-03-31T12:20:15-0500
277 +
278 +EOF
279 +
280 +check_verify_failure 'detect invalid tag timestamp2' \
281 + '^error: char111: malformed tag timestamp$'
282 +
283 +############################################################
284 +# 20. detect invalid tag timezone1
285
286 cat >tag.sig <<EOF
287 object $head
@@ -263,11 +291,39 @@ tagger T A Gger <tagger@example.com> 1206478233 GMT
291
292 EOF
293
266 -check_verify_failure 'detect invalid tag timezone' \
294 +check_verify_failure 'detect invalid tag timezone1' \
295 + '^error: char118: malformed tag timezone$'
296 +
297 +############################################################
298 +# 21. detect invalid tag timezone2
299 +
300 +cat >tag.sig <<EOF
301 +object $head
302 +type commit
303 +tag mytag
304 +tagger T A Gger <tagger@example.com> 1206478233 + 30
305 +
306 +EOF
307 +
308 +check_verify_failure 'detect invalid tag timezone2' \
309 + '^error: char118: malformed tag timezone$'
310 +
311 +############################################################
312 +# 22. detect invalid tag timezone3
313 +
314 +cat >tag.sig <<EOF
315 +object $head
316 +type commit
317 +tag mytag
318 +tagger T A Gger <tagger@example.com> 1206478233 -1430
319 +
320 +EOF
321 +
322 +check_verify_failure 'detect invalid tag timezone3' \
323 '^error: char118: malformed tag timezone$'
324
325 ############################################################
270 -# 19. detect invalid header entry
326 +# 23. detect invalid header entry
327
328 cat >tag.sig <<EOF
329 object $head
@@ -282,7 +338,7 @@ check_verify_failure 'detect invalid header entry' \
338 '^error: char124: trailing garbage in tag header$'
339
340 ############################################################
285 -# 20. create valid tag
341 +# 24. create valid tag
342
343 cat >tag.sig <<EOF
344 object $head
@@ -297,7 +353,7 @@ test_expect_success \
353 'git-mktag <tag.sig >.git/refs/tags/mytag 2>message'
354
355 ############################################################
300 -# 21. check mytag
356 +# 25. check mytag
357
358 test_expect_success \
359 'check mytag' \