path: root/xdiff-interface.c
diff options
authorJeff King <>2020-01-25 05:39:29 (GMT)
committerJunio C Hamano <>2020-01-29 07:13:25 (GMT)
commit3cd309c16f3b9370a90d2f4eb69002473020412a (patch)
tree465de16ad57bb5477586d3b7b5ebdf600ddd26b3 /xdiff-interface.c
parentd20bc01a51f7899deb7a04bdd7c2495789185297 (diff)
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 <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'xdiff-interface.c')
1 files changed, 6 insertions, 2 deletions
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 99661d4..4d20069 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -250,9 +250,13 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
ALLOC_ARRAY(regs->array, regs->nr);
for (i = 0; i < regs->nr; i++) {
struct ff_reg *reg = regs->array + i;
- const char *ep = strchr(value, '\n'), *expression;
+ const char *ep, *expression;
char *buffer = NULL;
+ if (!value)
+ BUG("mismatch between line count and parsing");
+ ep = strchr(value, '\n');
reg->negate = (*value == '!');
if (reg->negate && i == regs->nr - 1)
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)
if (regcomp(&reg->re, expression, cflags))
die("Invalid regexp to look for hunk header: %s", expression);
- value = ep + 1;
+ value = ep ? ep + 1 : NULL;