made RMUtilInfo strdip all its keys and values for saner memory management

master
Dvir Volk 7 years ago
parent 5ccee1e4d0
commit 74a6c8c7fb

@ -57,9 +57,11 @@ RMUtilInfo *RMUtil_GetRedisInfo(RedisModuleCtx *ctx) {
info->entries = calloc(cap, sizeof(RMUtilInfoEntry)); info->entries = calloc(cap, sizeof(RMUtilInfoEntry));
int i = 0; int i = 0;
char *text = (char *)RedisModule_StringPtrLen(RedisModule_CreateStringFromCallReply(r), NULL); size_t sz;
char *text = (char *)RedisModule_CallReplyStringPtr(r, &sz);
char *line = text; char *line = text;
while (line) { while (line && line < text + sz) {
char *line = strsep(&text, "\r\n"); char *line = strsep(&text, "\r\n");
if (line == NULL) break; if (line == NULL) break;
@ -68,8 +70,8 @@ RMUtilInfo *RMUtil_GetRedisInfo(RedisModuleCtx *ctx) {
} }
char *key = strsep(&line, ":"); char *key = strsep(&line, ":");
info->entries[i].key = key; info->entries[i].key = strdup(key);
info->entries[i].val = line; info->entries[i].val = strdup(line);
i++; i++;
if (i >= cap) { if (i >= cap) {
cap *= 2; cap *= 2;
@ -77,11 +79,14 @@ RMUtilInfo *RMUtil_GetRedisInfo(RedisModuleCtx *ctx) {
} }
} }
info->numEntries = i; info->numEntries = i;
RedisModule_FreeCallReply(r);
return info; return info;
} }
void RMUtilRedisInfo_Free(RMUtilInfo *info) { void RMUtilRedisInfo_Free(RMUtilInfo *info) {
for (int i = 0; i < info->numEntries; i++) {
free(info->entries[i].key);
free(info->entries[i].val);
}
free(info->entries); free(info->entries);
free(info); free(info);
} }

@ -79,8 +79,8 @@ void RMUtil_DefaultAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *
// A single key/value entry in a redis info map // A single key/value entry in a redis info map
typedef struct { typedef struct {
const char *key; char *key;
const char *val; char *val;
} RMUtilInfoEntry; } RMUtilInfoEntry;
// Representation of INFO command response, as a list of k/v pairs // Representation of INFO command response, as a list of k/v pairs

Loading…
Cancel
Save