记录发现的一个hiredis的bug
hiredis是redis官方提供的c客戶端庫(kù)。在讀代碼的過(guò)程中,發(fā)現(xiàn)了一個(gè)bug,記錄一下。
hiredis里定義了一個(gè)上下文結(jié)構(gòu)(struct redisContext),代碼如下(deps/hiredis/hiredis.h):
https://github.com/antirez/hiredis/blob/master/hiredis.h
157 /* Context for a connection to Redis */ 158 typedef struct redisContext { 159 int err; /* Error flags, 0 when there is no error */ 160 char errstr[128]; /* String representation of error when applicable */ 161 int fd; 162 int flags; 163 char *obuf; /* Write buffer */ 164 redisReader *reader; /* Protocol reader */ 165 } redisContext;針對(duì)這個(gè)結(jié)構(gòu),有一個(gè)對(duì)應(yīng)的free function,代碼如下(deps/hiredis/hiredis.c):
https://github.com/antirez/hiredis/blob/master/hiredis.c
1004 void redisFree(redisContext *c) { 1005 if (c->fd > 0) 1006 close(c->fd); 1007 if (c->obuf != NULL) 1008 sdsfree(c->obuf); 1009 if (c->reader != NULL) 1010 redisReaderFree(c->reader); 1011 free(c); 1012 }對(duì)照下,可以看到,定義中obuf成員使用的是char* ,而在釋放操作時(shí),卻是按照sds結(jié)構(gòu)(redis自己定義的類string數(shù)據(jù)結(jié)構(gòu))進(jìn)行的釋放。
如果再看下sdsfree的函數(shù)(deps/hiredis/sds.c),就能看到可能造成的災(zāi)難性后果:
https://github.com/antirez/hiredis/blob/master/sds.c
76 void sdsfree(sds s) {77 if (s == NULL) return;78 free(s-sizeof(struct sdshdr));79 }如上。在hiredis的github中,錯(cuò)誤仍可以看到。
?
剛給redis DB郵件組發(fā)了封郵件,不知是否會(huì)被回復(fù)。
總結(jié)
以上是生活随笔為你收集整理的记录发现的一个hiredis的bug的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Json对象转换
- 下一篇: javascript-按圆形排列DIV元