@matheusbsbs / matheus-bagliano / commits / 31d713d069
Fork 0

mktag: avoid %td in format string

Since v1.7.0-rc0~34 (make "mktag" a built-in, 2010-01-22), git mktag uses the C99-style %td format to print ptrdiff_t values. It falls back to %d when NO_C99_FORMAT is set, on the assumption that pre-C99 systems probably are using 32-bit pointers. But many modern systems are 64-bit and * sometimes one wants to test the NO_C99_FORMAT fallbacks using a modern development platform; * some platforms (I'm looking at you, msvc) have not gotten with the program and are still C89-only. These ptrdiff_t values are offsets from the beginning of a buffer, so a size_t or uintmax_t would work about as well. Use the latter so we can take advantage of the PRIuMAX fallback in git-compat-util.h, even on C99-challenged systems with 64-bit pointers. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Mar 16, 2011 at 00:14 UTC 31d713d069f6586c3b18aa721ccd668fa994219c
1 file changed +22 -26
builtin/mktag.c
+22 -26
@@ -35,12 +35,6 @@ static int verify_object(const unsigned char *sha1, const char *expected_type)
35 return ret;
36 }
37
38 -#ifdef NO_C99_FORMAT
39 -#define PD_FMT "%d"
40 -#else
41 -#define PD_FMT "%td"
42 -#endif
43 -
38 static int verify_tag(char *buffer, unsigned long size)
39 {
40 int typelen;
@@ -70,15 +64,18 @@ static int verify_tag(char *buffer, unsigned long size)
64 /* Verify tag-line */
65 tag_line = strchr(type_line, '\n');
66 if (!tag_line)
73 - return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
67 + return error("char%"PRIuMAX": could not find next \"\\n\"",
68 + (uintmax_t) (type_line - buffer));
69 tag_line++;
70 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
76 - return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
71 + return error("char%"PRIuMAX": no \"tag \" found",
72 + (uintmax_t) (tag_line - buffer));
73
74 /* Get the actual type */
75 typelen = tag_line - type_line - strlen("type \n");
76 if (typelen >= sizeof(type))
81 - return error("char" PD_FMT ": type too long", type_line+5 - buffer);
77 + return error("char%"PRIuMAX": type too long",
78 + (uintmax_t) (type_line+5 - buffer));
79
80 memcpy(type, type_line+5, typelen);
81 type[typelen] = 0;
@@ -95,15 +92,16 @@ static int verify_tag(char *buffer, unsigned long size)
92 break;
93 if (c > ' ')
94 continue;
98 - return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
95 + return error("char%"PRIuMAX": could not verify tag name",
96 + (uintmax_t) (tag_line - buffer));
97 }
98
99 /* Verify the tagger line */
100 tagger_line = tag_line;
101
102 if (memcmp(tagger_line, "tagger ", 7))
105 - return error("char" PD_FMT ": could not find \"tagger \"",
106 - tagger_line - buffer);
103 + return error("char%"PRIuMAX": could not find \"tagger \"",
104 + (uintmax_t) (tagger_line - buffer));
105
106 /*
107 * Check for correct form for name and email
@@ -115,44 +113,42 @@ static int verify_tag(char *buffer, unsigned long size)
113 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
114 strpbrk(tagger_line, "<>\n") != lb+1 ||
115 strpbrk(lb+2, "><\n ") != rb)
118 - return error("char" PD_FMT ": malformed tagger field",
119 - tagger_line - buffer);
116 + return error("char%"PRIuMAX": malformed tagger field",
117 + (uintmax_t) (tagger_line - buffer));
118
119 /* Check for author name, at least one character, space is acceptable */
120 if (lb == tagger_line)
123 - return error("char" PD_FMT ": missing tagger name",
124 - tagger_line - buffer);
121 + return error("char%"PRIuMAX": missing tagger name",
122 + (uintmax_t) (tagger_line - buffer));
123
124 /* timestamp, 1 or more digits followed by space */
125 tagger_line = rb + 2;
126 if (!(len = strspn(tagger_line, "0123456789")))
129 - return error("char" PD_FMT ": missing tag timestamp",
130 - tagger_line - buffer);
127 + return error("char%"PRIuMAX": missing tag timestamp",
128 + (uintmax_t) (tagger_line - buffer));
129 tagger_line += len;
130 if (*tagger_line != ' ')
133 - return error("char" PD_FMT ": malformed tag timestamp",
134 - tagger_line - buffer);
131 + return error("char%"PRIuMAX": malformed tag timestamp",
132 + (uintmax_t) (tagger_line - buffer));
133 tagger_line++;
134
135 /* timezone, 5 digits [+-]hhmm, max. 1400 */
136 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
137 strspn(tagger_line+1, "0123456789") == 4 &&
138 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
141 - return error("char" PD_FMT ": malformed tag timezone",
142 - tagger_line - buffer);
139 + return error("char%"PRIuMAX": malformed tag timezone",
140 + (uintmax_t) (tagger_line - buffer));
141 tagger_line += 6;
142
143 /* Verify the blank line separating the header from the body */
144 if (*tagger_line != '\n')
147 - return error("char" PD_FMT ": trailing garbage in tag header",
148 - tagger_line - buffer);
145 + return error("char%"PRIuMAX": trailing garbage in tag header",
146 + (uintmax_t) (tagger_line - buffer));
147
148 /* The actual stuff afterwards we don't care about.. */
149 return 0;
150 }
151
154 -#undef PD_FMT
155 -
152 int cmd_mktag(int argc, const char **argv, const char *prefix)
153 {
154 struct strbuf buf = STRBUF_INIT;