NULL test of sh after s_malloc should be first conducted before memset(sh), possible crash if sh in memset is NULL. ---------change the original code below ---------
sds sdsnewlen(const void *init, size_t initlen) {
...
sh = s_malloc(hdrlen+initlen+1);
if (init==SDS_NOINIT)
init = NULL;
else if (!init)
memset(sh, 0, hdrlen+initlen+1);
if (sh == NULL) return NULL;
...
}
--------to this one ------------------
sds sdsnewlen(const void *init, size_t initlen) {
...
sh = s_malloc(hdrlen+initlen+1);
if (sh == NULL) return NULL; // NULL test right after s_malloc
if (init==SDS_NOINIT)
init = NULL;
else if (!init)
memset(sh, 0, hdrlen+initlen+1);
...
}
Comment From: trevor211
dup of #6369 @soloestoy
Comment From: oranagra
@trevor211 thanks. as stated in the issue linked above, this was never a problem, but it is fixed anyway already.