Skip to content

Commit 3cd309c

Browse files
peffgitster
authored andcommitted
xdiff: avoid computing non-zero offset from NULL pointer
As with the previous commit, clang-11's UBSan complains about computing offsets from a NULL pointer, causing some tests to fail. In this case, though, we're actually computing a non-zero offset, which is even more dubious. From t7810: xdiff-interface.c:268:14: runtime error: applying non-zero offset 1 to null pointer ... not ok 131 - grep -p with userdiff The problem is our parsing of the funcname config. We count the number of lines in the string, allocate an array, and then loop over our allocated entries, parsing each line and moving our cursor to one past the trailing newline for the next iteration. But the final line will not generally have a trailing newline (since it's a config value), and hence we go to one past NULL. In practice this is OK, since our loop should terminate before we look at the value. But even computing such an invalid pointer technically violates the standard. We can fix it by leaving the pointer at NULL if we're at the end, rather than one-past. And while we're thinking about it, we can also document the variant by asserting that our initial line-count matches the second-pass of parsing. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d20bc01 commit 3cd309c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

xdiff-interface.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,13 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
250250
ALLOC_ARRAY(regs->array, regs->nr);
251251
for (i = 0; i < regs->nr; i++) {
252252
struct ff_reg *reg = regs->array + i;
253-
const char *ep = strchr(value, '\n'), *expression;
253+
const char *ep, *expression;
254254
char *buffer = NULL;
255255

256+
if (!value)
257+
BUG("mismatch between line count and parsing");
258+
ep = strchr(value, '\n');
259+
256260
reg->negate = (*value == '!');
257261
if (reg->negate && i == regs->nr - 1)
258262
die("Last expression must not be negated: %s", value);
@@ -265,7 +269,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
265269
if (regcomp(&reg->re, expression, cflags))
266270
die("Invalid regexp to look for hunk header: %s", expression);
267271
free(buffer);
268-
value = ep + 1;
272+
value = ep ? ep + 1 : NULL;
269273
}
270274
}
271275

0 commit comments

Comments
 (0)