问题
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