记录发现的一个hiredis的bug
生活随笔
收集整理的這篇文章主要介紹了
记录发现的一个hiredis的bug
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
hiredis是redis官方提供的c客戶端庫。在讀代碼的過程中,發現了一個bug,記錄一下。
hiredis里定義了一個上下文結構(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;針對這個結構,有一個對應的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 }對照下,可以看到,定義中obuf成員使用的是char* ,而在釋放操作時,卻是按照sds結構(redis自己定義的類string數據結構)進行的釋放。
如果再看下sdsfree的函數(deps/hiredis/sds.c),就能看到可能造成的災難性后果:
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中,錯誤仍可以看到。
?
剛給redis DB郵件組發了封郵件,不知是否會被回復。
總結
以上是生活随笔為你收集整理的记录发现的一个hiredis的bug的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Json对象转换
- 下一篇: 走进移动支付:开启物联网时代的商务之门