Skip to content

Commit

Permalink
Fixed sdscat*() bug: read memory error
Browse files Browse the repository at this point in the history
  • Loading branch information
iiol committed Jan 4, 2020
1 parent 78df725 commit d2eb3b3
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <ctype.h>
#include <assert.h>
#include <limits.h>
#include <alloca.h>
#include "sds.h"
#include "sdsalloc.h"

Expand Down Expand Up @@ -396,10 +397,15 @@ sds sdsgrowzero(sds s, size_t len) {
* references must be substituted with the new pointer returned by the call. */
sds sdscatlen(sds s, const void *t, size_t len) {
size_t curlen = sdslen(s);
char *buf = (char*)t;

if ((s >= buf && s < buf + len) || (buf >= s && buf < s + curlen)) {
buf = alloca(len);
memcpy(buf, t, len);
}
s = sdsMakeRoomFor(s,len);
if (s == NULL) return NULL;
memcpy(s+curlen, t, len);
memcpy(s+curlen, buf, len);
sdssetlen(s, curlen+len);
s[curlen+len] = '\0';
return s;
Expand Down

0 comments on commit d2eb3b3

Please sign in to comment.