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ù)。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
