X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/963238f713f02e538cf0f5851f3337173116ea39..9d7165e885b3de44577dea2917bb4f3afa2ed335:/src/sds.c diff --git a/src/sds.c b/src/sds.c index 2d063c4a..da049f6c 100644 --- a/src/sds.c +++ b/src/sds.c @@ -116,6 +116,25 @@ static sds sdsMakeRoomFor(sds s, size_t addlen) { return newsh->buf; } +/* Grow the sds to have the specified length. Bytes that were not part of + * the original length of the sds will be set to zero. */ +sds sdsgrowzero(sds s, size_t len) { + struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + size_t totlen, curlen = sh->len; + + if (len <= curlen) return s; + s = sdsMakeRoomFor(s,len-curlen); + if (s == NULL) return NULL; + + /* Make sure added region doesn't contain garbage */ + sh = (void*)(s-(sizeof(struct sdshdr))); + memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ + totlen = sh->len+sh->free; + sh->len = len; + sh->free = totlen-sh->len; + return s; +} + sds sdscatlen(sds s, void *t, size_t len) { struct sdshdr *sh; size_t curlen = sdslen(s);