From 00b82683bbec3652f836518c30fecdb0f5a3ea62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Svensson?= Date: Wed, 2 Feb 2022 10:44:52 +0100 Subject: [PATCH] Handle overflows as errors instead of asserting --- sds.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sds.c b/sds.c index 01b00f3..a20ba19 100644 --- a/sds.c +++ b/sds.c @@ -90,7 +90,7 @@ sds sdsnewlen(const void *init, size_t initlen) { int hdrlen = sdsHdrSize(type); unsigned char *fp; /* flags pointer. */ - assert(initlen + hdrlen + 1 > initlen); /* Catch size_t overflow */ + if (hdrlen+initlen+1 <= initlen) return NULL; /* Catch size_t overflow */ sh = s_malloc(hdrlen+initlen+1); if (sh == NULL) return NULL; if (!init) @@ -207,7 +207,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { len = sdslen(s); sh = (char*)s-sdsHdrSize(oldtype); reqlen = newlen = (len+addlen); - assert(newlen > len); /* Catch size_t overflow */ + if (newlen <= len) return NULL; /* Catch size_t overflow */ if (newlen < SDS_MAX_PREALLOC) newlen *= 2; else @@ -221,7 +221,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { if (type == SDS_TYPE_5) type = SDS_TYPE_8; hdrlen = sdsHdrSize(type); - assert(hdrlen + newlen + 1 > reqlen); /* Catch size_t overflow */ + if (hdrlen+newlen+1 <= reqlen) return NULL; /* Catch size_t overflow */ if (oldtype==type) { newsh = s_realloc(sh, hdrlen+newlen+1); if (newsh == NULL) return NULL;