diff --git a/src/lib/sds_extras.c b/src/lib/sds_extras.c index 4c0c779fb..0ab830471 100644 --- a/src/lib/sds_extras.c +++ b/src/lib/sds_extras.c @@ -320,8 +320,9 @@ sds sds_catjsonchar(sds s, const char c) { */ sds sds_catchar(sds s, const char c) { // Make sure there is always space for at least 1 char. - if (sdsavail(s) == 0) { - s = sdsMakeRoomFor(s, 1); + s = sdsMakeRoomFor(s, 1); + if (s == NULL) { + return NULL; } size_t i = sdslen(s); s[i++] = c; diff --git a/test/tests/test_sds_extras.c b/test/tests/test_sds_extras.c index 1679f4b8e..408935ae8 100644 --- a/test/tests/test_sds_extras.c +++ b/test/tests/test_sds_extras.c @@ -142,6 +142,12 @@ UTEST(sds_extras, test_sds_catchar) { s = sds_catchar(s, 'a'); ASSERT_STREQ("a", s); ASSERT_EQ(strlen(s), sdslen(s)); + + s = sdscat(s, "bcdefghijklmnopqrstuvwxy"); + s = sds_catchar(s, 'z'); + ASSERT_STREQ("abcdefghijklmnopqrstuvwxyz", s); + ASSERT_EQ(strlen(s), sdslen(s)); + sdsfree(s); }