Hiredis是Redis官方推出的一个用于链接redis数据库的极简C库git
GitHub地址:https://github.com/redis/hiredis ,测试用的 版本是v1.0.0github
redis和hiredis,官方并无提供windows版本,在GitHub的说明中也没有windows平台下使用的相关的介绍redis
1> githib下载v1.0.0版本 地址:https://github.com/redis/hiredis/tree/v1.0.0数据库
2>vs建立静态库工程,添加hiredis目录下*.c *.h文件到vs工程,编译工程windows
1.编译问题,sds.h文件编译报错,在预编译定义中添加inline=_inline 安全
2.openssl库依赖,hiredis支持创建ssl协议的安全链接,须要OpenSSL依赖库,在此也能够跳过此功能,在vs工程中移除ssl.c,不编译便可函数
3.hiredis.c文件中"%zu" 修改成 "%lu",vs 不支持%zu size_t 格式化输出测试
4.hiredis.c文件中ui
cmd = hi_malloc(totlen+1); 申请的内存块会被下面的for循环写越界,致使内存溢出,在函数外层,释放cmd内存的时候崩溃掉,
修改方案为"cmd = hi_malloc(totlen+1+argc*3);"
1 /* Build the command at protocol level */ 2 cmd = hi_malloc(totlen+1); 3 if (cmd == NULL) 4 return -1; 5 6 pos = sprintf(cmd,"*%d\r\n",argc); 7 for (j = 0; j < argc; j++) { 8 len = argvlen ? argvlen[j] : strlen(argv[j]); 9 pos += sprintf(cmd+pos,"$%zu\r\n",len); 10 memcpy(cmd+pos,argv[j],len); 11 pos += len; 12 cmd[pos++] = '\r'; 13 cmd[pos++] = '\n'; 14 } 15 assert(pos == totlen); 16 cmd[pos] = '\0';