Fix s/// tests get skipped before realloc() allocates new string memory.

(They tried to copy the unchanged data to a destination that didn't exist
yet: skip copy for null pointer and defer copy to allocation time.)
This commit is contained in:
Rob Landley 2023-02-17 15:35:18 -06:00
parent dbc6311ec7
commit abd8b1b221
2 changed files with 8 additions and 2 deletions

View File

@ -213,6 +213,8 @@ testing 's l loses missing newline' "sed -n 'N;l'" 'one\\ntwo$\n' '' 'one\ntwo'
testing 's -z l' "sed -zn 'N;l'" 'one\\000two$\0' '' 'one\0two\0'
testing 's -z l missing newline' "sed -zn 'N;l'" 'one\\000two$\0' '' 'one\0two'
testcmd 'count match' '"s/./&X/4"' '0123X45\n' '' '012345\n'
# -i with $ last line test
#echo meep | sed/sed -e '1a\' -e 'huh'

View File

@ -489,7 +489,7 @@ static void sed_line(char **pline, long plen)
// Zero length matches don't count immediately after a previous match
if (!mlen && !zmatch) {
if (rline-line == len) break;
l2[l2used++] = *rline++;
if (l2) l2[l2used++] = *rline++;
zmatch++;
continue;
} else zmatch = 0;
@ -524,7 +524,11 @@ static void sed_line(char **pline, long plen)
// Adjust allocation size of new string, copy data we know we'll keep
l2l += newlen-mlen;
if ((l2l|0xfff) > l2old) l2 = xrealloc(l2, l2old = (l2l|0xfff)+1);
if ((mlen = l2l|0xfff) > l2old) {
l2 = xrealloc(l2, ++mlen);
if (l2used && !l2old) memcpy(l2, rline-l2used, l2used);
l2old = mlen;
}
if (match[0].rm_so) {
memcpy(l2+l2used, rline, match[0].rm_so);
l2used += match[0].rm_so;