Skip to content

Commit 349362c

Browse files
jrngitster
authored andcommitted
xdiff: cast arguments for ctype functions to unsigned char
The ctype functions isspace(), isalnum(), et al take an integer argument representing an unsigned character, or -1 for EOF. On platforms with a signed char, it is unsafe to pass a char to them without casting it to unsigned char first. Most of git is already shielded against this by the ctype implementation in git-compat-util.h, but xdiff, which uses libc ctype.h, ought to be fixed. Noticed-by: der Mouse <[email protected]> Reported-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9173912 commit 349362c

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

xdiff/xmacros.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define XDL_MAX(a, b) ((a) > (b) ? (a): (b))
3131
#define XDL_ABS(v) ((v) >= 0 ? (v): -(v))
3232
#define XDL_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
33+
#define XDL_ISSPACE(c) (isspace((unsigned char)(c)))
3334
#define XDL_ADDBITS(v,b) ((v) + ((v) >> (b)))
3435
#define XDL_MASKBITS(b) ((1UL << (b)) - 1)
3536
#define XDL_HASHLONG(v,b) (XDL_ADDBITS((unsigned long)(v), b) & XDL_MASKBITS(b))

xdiff/xmerge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
336336
static int line_contains_alnum(const char *ptr, long size)
337337
{
338338
while (size--)
339-
if (isalnum(*(ptr++)))
339+
if (isalnum((unsigned char)*(ptr++)))
340340
return 1;
341341
return 0;
342342
}

xdiff/xutils.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,18 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
211211
if (l1[i1++] != l2[i2++])
212212
return 0;
213213
skip_ws:
214-
while (i1 < s1 && isspace(l1[i1]))
214+
while (i1 < s1 && XDL_ISSPACE(l1[i1]))
215215
i1++;
216-
while (i2 < s2 && isspace(l2[i2]))
216+
while (i2 < s2 && XDL_ISSPACE(l2[i2]))
217217
i2++;
218218
}
219219
} else if (flags & XDF_IGNORE_WHITESPACE_CHANGE) {
220220
while (i1 < s1 && i2 < s2) {
221-
if (isspace(l1[i1]) && isspace(l2[i2])) {
221+
if (XDL_ISSPACE(l1[i1]) && XDL_ISSPACE(l2[i2])) {
222222
/* Skip matching spaces and try again */
223-
while (i1 < s1 && isspace(l1[i1]))
223+
while (i1 < s1 && XDL_ISSPACE(l1[i1]))
224224
i1++;
225-
while (i2 < s2 && isspace(l2[i2]))
225+
while (i2 < s2 && XDL_ISSPACE(l2[i2]))
226226
i2++;
227227
continue;
228228
}
@@ -241,13 +241,13 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
241241
* while there still are characters remaining on both lines.
242242
*/
243243
if (i1 < s1) {
244-
while (i1 < s1 && isspace(l1[i1]))
244+
while (i1 < s1 && XDL_ISSPACE(l1[i1]))
245245
i1++;
246246
if (s1 != i1)
247247
return 0;
248248
}
249249
if (i2 < s2) {
250-
while (i2 < s2 && isspace(l2[i2]))
250+
while (i2 < s2 && XDL_ISSPACE(l2[i2]))
251251
i2++;
252252
return (s2 == i2);
253253
}
@@ -260,10 +260,10 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
260260
char const *ptr = *data;
261261

262262
for (; ptr < top && *ptr != '\n'; ptr++) {
263-
if (isspace(*ptr)) {
263+
if (XDL_ISSPACE(*ptr)) {
264264
const char *ptr2 = ptr;
265265
int at_eol;
266-
while (ptr + 1 < top && isspace(ptr[1])
266+
while (ptr + 1 < top && XDL_ISSPACE(ptr[1])
267267
&& ptr[1] != '\n')
268268
ptr++;
269269
at_eol = (top <= ptr + 1 || ptr[1] == '\n');

0 commit comments

Comments
 (0)