Redis
hiredis客户端
问题复现:使用hiredis的API写一个测试函数,如下图所示。我们在使用snprintf格式化一条入库命令后,使用redisCommand接口入库。编译运行。
我们可以看到程序运行的结果。函数返回的reply指针会NULL,说明函数没有正确的执行。
我们连接redis数据库,使用keys命令查看是否有这个test:storage key存在。从下图中我们可以看到keys并不存在。这种结果和我们预期的结果并不一样。
在代码中,我们使用reply = redisCommand(c, 'HMSET t:storage aaa %s bbb %s time %s', 'niutest@123', 'niutest/@%2018', '2018');直接来做入库。重新编译执行代码后,在数据库中查看可以看到我们保存的key。(图中的t:storage是在第一张图的代码中写的不一致)
为什么会出现上述的这种情况呢?当我们去掉bbb的value中的%的时候,重新编译入库,发现是可以成功的!!!在对这一现象进行深入分析,我们通过查看hiredis源码发现,在redisvFormatCommand这个函数中,如果在格式化的format中存在%号,那么就会执行sdscat函数。这个函数的功能我们引用原文。“Append the specified null termianted C string to the sds string 's'.”“After the call, the passed sds string is no longer valid and all the references must be substituted with the new pointer returned by the call.”大意就是在%后面的内容会使用格式化串的内容替换。而我们第一次直接格式化完成后导致函数在遇到%的时候,无法执行替换,所以会执行失败。
在遇到问题,分析问题的时候,追本溯源,将是解决问题最快速的方法。