Avoid undefined behaviour in DOH Replace() function

If the source and replacement strings were the same length, the code
was performing undefined pointer arithmetic involving a NULL pointer.
I'm not aware of any observable effects of this in practice, but it's
potentially problematic.  It's detected by ubsan, for example when
running `make check-python-test-suite`:

DOH/string.c:839:4: runtime error: applying non-zero offset to non-null pointer 0x602000001558 produced null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior DOH/string.c:839:4 in
This commit is contained in:
Olly Betts 2020-09-04 10:44:49 +12:00
commit 975f8fcfdb

View file

@ -836,7 +836,9 @@ static int replace_simple(String *str, char *token, char *rep, int flags, int co
memmove(t, s, (str->str + str->len) - s + 1);
}
} else {
t += (c - s);
if (c) {
t += (c - s);
}
}
s = c;
ic--;