antirez / sds

Simple Dynamic Strings library for C
BSD 2-Clause "Simplified" License
4.93k stars 480 forks source link

Fix check for s_malloc #65

Closed michaelpmoran closed 5 years ago

michaelpmoran commented 8 years ago

Line 91 makes a call to s_malloc (which is defined as malloc in sdsalloc.h) and assigns the return value to sh. The check for whether this call to s_malloc succeeded is not until Line 94. On Lines 92-93, if init is NULL, then an attempt to zero out the memory area pointed to by sh is made. If s_malloc fails (and thus returns NULL), memset will be called with a NULL address (because sh is NULL), resulting in a seg fault. This possible seg fault can be prevented by moving the check on Line 94 to Line 92, after the call to s_malloc.