記錄發現的一個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郵件組發了封郵件,不知是否會被回復。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM