Skip to content

Commit

Permalink
Fix memory leak if a RedisCommand object were to be reused. That is, …
Browse files Browse the repository at this point in the history
…a memory leak would have occured if the methods format() or formatArgv() were to be used more than once on the same object. This change simply ensures that currently allocated memory gets freed before new memory gets allocated. (sonic-net#392)
  • Loading branch information
martin-belanger authored Oct 8, 2020
1 parent 3751c81 commit bf8da3e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions common/rediscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ RedisCommand::~RedisCommand()

void RedisCommand::format(const char *fmt, ...)
{
if (temp != nullptr)
{
redisFreeCommand(temp);
temp = nullptr;
}

va_list ap;
va_start(ap, fmt);
int len = redisvFormatCommand(&temp, fmt, ap);
Expand All @@ -31,6 +37,12 @@ void RedisCommand::format(const char *fmt, ...)

void RedisCommand::formatArgv(int argc, const char **argv, const size_t *argvlen)
{
if (temp != nullptr)
{
redisFreeCommand(temp);
temp = nullptr;
}

int len = redisFormatCommandArgv(&temp, argc, argv, argvlen);
if (len == -1) {
throw std::bad_alloc();
Expand Down

0 comments on commit bf8da3e

Please sign in to comment.