hiredis SET runs into segmentation fault

走远了吗. 提交于 2021-01-28 18:29:05

问题


I'm trying to SET a struct into Redis with hiredis:

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;
}

However this runs into a segmentation fault somewhere inside hiredis.

this->redis, path, and stat have proper values. GET commands work and deliver a NIL reply type (since Redis is empty).

What am I doing wrong?


回答1:


The trouble here is that you're specifying a raw structure instead of a pointer to the structure:

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;
}

It's probable that the driver was looking for a void* buffer of a particular size and treated stat as a void*, causing a segfault when that pointer got de-referenced.



来源:https://stackoverflow.com/questions/58686351/hiredis-set-runs-into-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!