Skip to content

Commit 3901998

Browse files
fix trim function
1 parent 08c88aa commit 3901998

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

code/logic/cstring.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,16 @@ cstring fossil_io_cstring_trim_safe(ccstring str, size_t max_len) {
986986
size_t end = strnlen(copy, max_len);
987987
while (end > start && isspace((unsigned char)copy[end - 1])) end--;
988988

989-
copy[end] = '\0';
990-
return &copy[start];
989+
size_t trimmed_len = end - start;
990+
cstring trimmed = (cstring)malloc(trimmed_len + 1);
991+
if (!trimmed) {
992+
free(copy);
993+
return NULL;
994+
}
995+
memcpy(trimmed, copy + start, trimmed_len);
996+
trimmed[trimmed_len] = '\0';
997+
free(copy);
998+
return trimmed;
991999
}
9921000

9931001
cstring *fossil_io_cstring_split_safe(ccstring str, char delimiter, size_t *count, size_t max_len) {

0 commit comments

Comments
 (0)