Skip to content

Commit 2b82e9a

Browse files
fam007eCarterLi
authored andcommitted
Fix potential use-after-free and overlapping memory copy in ffStrbufSetNS
1 parent 5fe2cc6 commit 2b82e9a

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/common/impl/FFstrbuf.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,17 @@ void ffStrbufSetNS(FFstrbuf* strbuf, uint32_t length, const char* value) {
248248
assert(value != NULL);
249249

250250
if (strbuf->allocated < length + 1) {
251+
char* newBuf = malloc(sizeof(char) * (length + 1));
252+
memcpy(newBuf, value, length);
251253
if (strbuf->allocated > 0) {
252254
free(strbuf->chars);
253255
}
256+
strbuf->chars = newBuf;
254257
strbuf->allocated = length + 1;
255-
strbuf->chars = malloc(sizeof(char) * strbuf->allocated);
258+
} else {
259+
memmove(strbuf->chars, value, length);
256260
}
257261

258-
memcpy(strbuf->chars, value, length);
259262
strbuf->length = length;
260263
strbuf->chars[length] = '\0';
261264
}

0 commit comments

Comments
 (0)