From d2eb3b300b46c14858c515a7460576be1b1dbe54 Mon Sep 17 00:00:00 2001 From: IIOL Date: Sat, 4 Jan 2020 03:49:37 +0300 Subject: [PATCH] Fixed sdscat*() bug: read memory error --- sds.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sds.c b/sds.c index 1189716..37f9da6 100644 --- a/sds.c +++ b/sds.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "sds.h" #include "sdsalloc.h" @@ -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;