Py学习  »  Redis

hiredis集遇到分段错误

Cobra_Fast • 4 年前 • 779 次点击  

我在试着 SET 用hiredis构造Redis:

struct StatLite
{
    uid_t uid;
    gid_t gid;
    mode_t mode;
}

bool RedisPermissionHandler::Set(std::string path, StatLite stat)
{
    redisReply *reply = (redisReply*)redisCommand(this->redis,
        "SET %b %b",
        path.c_str(), (size_t)path.length(),
        stat, (size_t)sizeof(stat));
    freeReplyObject(reply);
    return true;
}

然而,这在hiredis内部的某个地方遇到了分割错误。

this->redis , path ,和 stat 有正确的价值观。 GET 命令可以工作并传递NIL应答类型(因为Redis是空的)。

我做错什么了?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48629
 
779 次点击  
文章 [ 1 ]  |  最新文章 4 年前
tadman
Reply   •   1 楼
tadman    4 年前

这里的问题是,您指定的是原始结构,而不是指向该结构的指针:

bool RedisPermissionHandler::Set(std::string path, StatLite stat)
{
    redisReply *reply = (redisReply*)redisCommand(this->redis,
        "SET %b %b",
        path.c_str(), (size_t)path.length(),
        &stat, (size_t)sizeof(stat) // Pointer to stat!
    );

    freeReplyObject(reply);
    return true;
}

很可能司机在找 void* 特殊大小的缓冲器 stat 作为一个 空的* ,当指针被取消引用时导致segfault。