deserialization of pod struct stored in redis fails if key is defined as std::string

244 views Asked by At

Storing of POD struct in redis works fine with const char * but doesn't if std::string is involved.

const char * example

#include <hiredis/hiredis.h>
#include <string.h>
#include <string>
#include <iostream>

using namespace std;

struct Test
{
  const uint32_t id;
  const char *name;
};
int main() {
  redisContext *context = redisConnect("127.0.0.1", 6379);
  if (context == NULL || context->err) {
      if (context) {
          printf("Error: %s\n", context->errstr);
      } else {
          printf("Can't allocate redis context\n");
      }
      exit(EXIT_FAILURE);
  }

  Test obj = {(uint32_t) 123, "foo bar"};

  const size_t size = 2 * sizeof(uint32_t) + (strlen(obj.name) + 1);

  cout << "object{id: " << obj.id << ", name:' " << obj.name << "'}; size: " << size << endl;

  redisReply *reply = 0;

  const char *key = strdup("some-key");
  reply = (redisReply *) redisCommand(context, "SET %b %b", key, strlen(key), &obj, size);

  if (!reply)
      return REDIS_ERR;

  freeReplyObject(reply);

  reply = (redisReply *) redisCommand(context, "GET %s", key);
  if (!reply)
      return REDIS_ERR;

  Test *res = (struct Test*) reply->str;
  cout << "result{id: " << res->id << ", name:' " << res->name << "'}; size: " << size << endl;

  freeReplyObject(reply);
  redisFree(context);
}

If I replace the lines:

  const char *key = strdup("some-key");
  reply = (redisReply *) redisCommand(context, "SET %b %b", key, strlen(key), &obj, size);

with

std::string key("some-key");
reply = (redisReply *) redisCommand(context, "SET %b %b", key.c_str(), key.size(), &obj, size);

the execution always ends in Segmentation fault.

I can't solve the issue by myself and I would really appreciate any help.

1

There are 1 answers

3
palik On

OK, I got it: replacement of second redisCommand statement

reply = (redisReply *) redisCommand(context, "GET %b", key.c_str(), key.size());

solved the issue.