Skip to content

Commit 7c44a9d

Browse files
Merge pull request #1045 from Nordix/sds-updates
Update hiredis sds with improvements found in redis
2 parents f8de9a4 + 00b8268 commit 7c44a9d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

sds.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ sds sdsnewlen(const void *init, size_t initlen) {
9090
int hdrlen = sdsHdrSize(type);
9191
unsigned char *fp; /* flags pointer. */
9292

93+
if (hdrlen+initlen+1 <= initlen) return NULL; /* Catch size_t overflow */
9394
sh = s_malloc(hdrlen+initlen+1);
9495
if (sh == NULL) return NULL;
9596
if (!init)
@@ -174,7 +175,7 @@ void sdsfree(sds s) {
174175
* the output will be "6" as the string was modified but the logical length
175176
* remains 6 bytes. */
176177
void sdsupdatelen(sds s) {
177-
int reallen = strlen(s);
178+
size_t reallen = strlen(s);
178179
sdssetlen(s, reallen);
179180
}
180181

@@ -196,7 +197,7 @@ void sdsclear(sds s) {
196197
sds sdsMakeRoomFor(sds s, size_t addlen) {
197198
void *sh, *newsh;
198199
size_t avail = sdsavail(s);
199-
size_t len, newlen;
200+
size_t len, newlen, reqlen;
200201
char type, oldtype = s[-1] & SDS_TYPE_MASK;
201202
int hdrlen;
202203

@@ -205,7 +206,8 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
205206

206207
len = sdslen(s);
207208
sh = (char*)s-sdsHdrSize(oldtype);
208-
newlen = (len+addlen);
209+
reqlen = newlen = (len+addlen);
210+
if (newlen <= len) return NULL; /* Catch size_t overflow */
209211
if (newlen < SDS_MAX_PREALLOC)
210212
newlen *= 2;
211213
else
@@ -219,6 +221,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
219221
if (type == SDS_TYPE_5) type = SDS_TYPE_8;
220222

221223
hdrlen = sdsHdrSize(type);
224+
if (hdrlen+newlen+1 <= reqlen) return NULL; /* Catch size_t overflow */
222225
if (oldtype==type) {
223226
newsh = s_realloc(sh, hdrlen+newlen+1);
224227
if (newsh == NULL) return NULL;
@@ -580,7 +583,7 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
580583
*/
581584
sds sdscatfmt(sds s, char const *fmt, ...) {
582585
const char *f = fmt;
583-
int i;
586+
long i;
584587
va_list ap;
585588

586589
va_start(ap,fmt);
@@ -755,14 +758,14 @@ int sdsrange(sds s, ssize_t start, ssize_t end) {
755758

756759
/* Apply tolower() to every character of the sds string 's'. */
757760
void sdstolower(sds s) {
758-
int len = sdslen(s), j;
761+
size_t len = sdslen(s), j;
759762

760763
for (j = 0; j < len; j++) s[j] = tolower(s[j]);
761764
}
762765

763766
/* Apply toupper() to every character of the sds string 's'. */
764767
void sdstoupper(sds s) {
765-
int len = sdslen(s), j;
768+
size_t len = sdslen(s), j;
766769

767770
for (j = 0; j < len; j++) s[j] = toupper(s[j]);
768771
}

0 commit comments

Comments
 (0)