fix sigfault on older c compilers

This commit is contained in:
Pascal Kuthe 2022-11-14 13:43:42 +01:00 committed by Holger Vogt
parent 48b51907fa
commit 098ec89294
1 changed files with 10 additions and 9 deletions

View File

@ -606,18 +606,19 @@ static char *cat2strings(char *s1, char *s2, bool spa)
else if (s1 == NULL || *s1 == '\0') { else if (s1 == NULL || *s1 == '\0') {
return copy(s2); return copy(s2);
} }
size_t lges = strlen(s1) + strlen(s2) + 2; size_t l1 = strlen(s1);
char *nextstr; size_t l2 = strlen(s2);
char *strsum = TMALLOC(char, lges); char *strsum = TMALLOC(char, l1 + l2 + 2);
if (spa) { if (spa) {
nextstr = memccpy(strsum, s1, '\0', lges); memcpy(strsum, s1, l1);
*(nextstr - 1) = ' '; memcpy(strsum + l1 + 1, s2, l2);
nextstr = memccpy(nextstr, s2, '\0', lges); strsum[l1] = ' ';
strsum[l1 + l2 + 1] = '\0';
} }
else { else {
nextstr = (char*)memccpy(strsum, s1, '\0', lges) - 1; memcpy(strsum, s1, l1);
nextstr = (char*)memccpy(nextstr, s2, '\0', lges) - 1; memcpy(strsum + l1, s2, l2);
*nextstr = '\0'; strsum[l1 + l2] = '\0';
} }
return strsum; return strsum;
} }