mirror of
https://github.com/fluencelabs/redis
synced 2025-04-07 18:08:04 +00:00
Fix SDS type 5 sdsIncrLen() bug and added test.
Thanks to @oranagra for spotting this error.
This commit is contained in:
parent
3da97ea67f
commit
cf68f4ee6a
42
src/sds.c
42
src/sds.c
@ -305,8 +305,8 @@ void sdsIncrLen(sds s, int incr) {
|
|||||||
unsigned char *fp = ((unsigned char*)s)-1;
|
unsigned char *fp = ((unsigned char*)s)-1;
|
||||||
unsigned char oldlen = SDS_TYPE_5_LEN(flags);
|
unsigned char oldlen = SDS_TYPE_5_LEN(flags);
|
||||||
assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));
|
assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));
|
||||||
*fp = SDS_TYPE_5 | ((oldlen+1) << SDS_TYPE_BITS);
|
*fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS);
|
||||||
len = oldlen+1;
|
len = oldlen+incr;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDS_TYPE_8: {
|
case SDS_TYPE_8: {
|
||||||
@ -1193,28 +1193,40 @@ int sdsTest(void) {
|
|||||||
test_cond("sdscatrepr(...data...)",
|
test_cond("sdscatrepr(...data...)",
|
||||||
memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)
|
memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)
|
||||||
|
|
||||||
#if 0
|
|
||||||
{
|
{
|
||||||
unsigned int oldfree;
|
unsigned int oldfree;
|
||||||
|
char *p;
|
||||||
|
int step = 10, j, i;
|
||||||
|
|
||||||
sdsfree(x);
|
sdsfree(x);
|
||||||
sdsfree(y);
|
sdsfree(y);
|
||||||
x = sdsnew("0");
|
x = sdsnew("0");
|
||||||
sh = (void*) (x-(sizeof(struct sdshdr)));
|
test_cond("sdsnew() free/len buffers", sdslen(x) == 1 && sdsavail(x) == 0);
|
||||||
test_cond("sdsnew() free/len buffers", sh->len == 1 && sh->free == 0);
|
|
||||||
x = sdsMakeRoomFor(x,1);
|
/* Run the test a few times in order to hit the first two
|
||||||
sh = (void*) (x-(sizeof(struct sdshdr)));
|
* SDS header types. */
|
||||||
test_cond("sdsMakeRoomFor()", sh->len == 1 && sh->free > 0);
|
for (i = 0; i < 10; i++) {
|
||||||
oldfree = sh->free;
|
int oldlen = sdslen(x);
|
||||||
x[1] = '1';
|
x = sdsMakeRoomFor(x,step);
|
||||||
sdsIncrLen(x,1);
|
int type = x[-1]&SDS_TYPE_MASK;
|
||||||
test_cond("sdsIncrLen() -- content", x[0] == '0' && x[1] == '1');
|
|
||||||
test_cond("sdsIncrLen() -- len", sh->len == 2);
|
test_cond("sdsMakeRoomFor() len", sdslen(x) == oldlen);
|
||||||
test_cond("sdsIncrLen() -- free", sh->free == oldfree-1);
|
if (type != SDS_TYPE_5) {
|
||||||
|
test_cond("sdsMakeRoomFor() free", sdsavail(x) >= step);
|
||||||
|
oldfree = sdsavail(x);
|
||||||
|
}
|
||||||
|
p = x+oldlen;
|
||||||
|
for (j = 0; j < step; j++) {
|
||||||
|
p[j] = 'A'+j;
|
||||||
|
}
|
||||||
|
sdsIncrLen(x,step);
|
||||||
|
}
|
||||||
|
test_cond("sdsMakeRoomFor() content",
|
||||||
|
memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",x,101) == 0);
|
||||||
|
test_cond("sdsMakeRoomFor() final length",sdslen(x)==101);
|
||||||
|
|
||||||
sdsfree(x);
|
sdsfree(x);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
test_report()
|
test_report()
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user