diff options
author | Michael Grunder <michael.grunder@gmail.com> | 2020-06-07 14:38:16 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-07 14:38:16 -0700 |
commit | 6448f735d5663c7c58aa269d8f53f06c4640ef5a (patch) | |
tree | a7d050ffe5f41cda3b51864a4c612abcd989850c /sds.c | |
parent | c7267235455f48e7ce4aa5cb1d0bc0cfe2b7ba09 (diff) |
sdsrange overflow fix (#830)
Fix overflow bug in `sdsrange`
Diffstat (limited to 'sds.c')
-rw-r--r-- | sds.c | 15 |
1 files changed, 11 insertions, 4 deletions
@@ -36,6 +36,7 @@ #include <string.h> #include <ctype.h> #include <assert.h> +#include <limits.h> #include "sds.h" #include "sdsalloc.h" @@ -713,15 +714,20 @@ sds sdstrim(sds s, const char *cset) { * * The string is modified in-place. * + * Return value: + * -1 (error) if sdslen(s) is larger than maximum positive ssize_t value. + * 0 on success. + * * Example: * * s = sdsnew("Hello World"); * sdsrange(s,1,-1); => "ello World" */ -void sdsrange(sds s, int start, int end) { +int sdsrange(sds s, ssize_t start, ssize_t end) { size_t newlen, len = sdslen(s); + if (len > SSIZE_MAX) return -1; - if (len == 0) return; + if (len == 0) return 0; if (start < 0) { start = len+start; if (start < 0) start = 0; @@ -732,9 +738,9 @@ void sdsrange(sds s, int start, int end) { } newlen = (start > end) ? 0 : (end-start)+1; if (newlen != 0) { - if (start >= (signed)len) { + if (start >= (ssize_t)len) { newlen = 0; - } else if (end >= (signed)len) { + } else if (end >= (ssize_t)len) { end = len-1; newlen = (start > end) ? 0 : (end-start)+1; } @@ -744,6 +750,7 @@ void sdsrange(sds s, int start, int end) { if (start && newlen) memmove(s, s+start, newlen); s[newlen] = 0; sdssetlen(s,newlen); + return 0; } /* Apply tolower() to every character of the sds string 's'. */ |