Tue, 04 Oct 2022 18:55:20 +0200
fix missing zero-termination in strreplace
src/string.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/string.c Tue Oct 04 18:49:14 2022 +0200 1.2 +++ b/src/string.c Tue Oct 04 18:55:20 2022 +0200 1.3 @@ -550,8 +550,8 @@ 1.4 1.5 struct cx_strreplace_ibuf { 1.6 size_t *buf; 1.7 - unsigned int len; /* small indices */ 1.8 struct cx_strreplace_ibuf *next; 1.9 + unsigned int len; 1.10 }; 1.11 1.12 static void cx_strrepl_free_ibuf(struct cx_strreplace_ibuf *buf) { 1.13 @@ -637,7 +637,7 @@ 1.14 curbuf = curbuf->next; 1.15 } while (curbuf); 1.16 result.length = str.length + rcount * adjlen; 1.17 - result.ptr = cxMalloc(allocator, result.length); 1.18 + result.ptr = cxMalloc(allocator, result.length + 1); 1.19 if (!result.ptr) { 1.20 cx_strrepl_free_ibuf(firstbuf); 1.21 return cx_mutstrn(NULL, 0); 1.22 @@ -668,6 +668,9 @@ 1.23 } while (curbuf); 1.24 memcpy(destptr, str.ptr + srcidx, str.length - srcidx); 1.25 1.26 + /* Result is guaranteed to be zero-terminated */ 1.27 + result.ptr[result.length] = '\0'; 1.28 + 1.29 /* Free index buffer */ 1.30 cx_strrepl_free_ibuf(firstbuf); 1.31