Skip to content

Commit

Permalink
Fix redis error reporting
Browse files Browse the repository at this point in the history
Previously, we assumed that reply->str was NULL
terminated. However, this turned out to be not true,
as users reported crashes in strlen connected to
where reply->str was appended to an std::string.

Use the method recomended by the docs, to read the
length separately.
  • Loading branch information
est31 committed Jan 8, 2016
1 parent 0bbbc6e commit 57a4619
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/database-redis.cpp
Expand Up @@ -92,7 +92,7 @@ bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)

if (reply->type == REDIS_REPLY_ERROR) {
warningstream << "saveBlock: saving block " << PP(pos)
<< " failed: " << reply->str << std::endl;
<< " failed: " << std::string(reply->str, reply->len) << std::endl;
freeReplyObject(reply);
return false;
}
Expand All @@ -119,7 +119,7 @@ std::string Database_Redis::loadBlock(const v3s16 &pos)
return str;
}
case REDIS_REPLY_ERROR: {
std::string errstr = reply->str;
std::string errstr(reply->str, reply->len);
freeReplyObject(reply);
errorstream << "loadBlock: loading block " << PP(pos)
<< " failed: " << errstr << std::endl;
Expand All @@ -134,7 +134,7 @@ std::string Database_Redis::loadBlock(const v3s16 &pos)
}
errorstream << "loadBlock: loading block " << PP(pos)
<< " returned invalid reply type " << reply->type
<< ": " << reply->str << std::endl;
<< ": " << std::string(reply->str, reply->len) << std::endl;
freeReplyObject(reply);
throw FileNotGoodException(std::string(
"Redis command 'HGET %s %s' gave invalid reply."));
Expand All @@ -151,7 +151,7 @@ bool Database_Redis::deleteBlock(const v3s16 &pos)
"Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
} else if (reply->type == REDIS_REPLY_ERROR) {
warningstream << "deleteBlock: deleting block " << PP(pos)
<< " failed: " << reply->str << std::endl;
<< " failed: " << std::string(reply->str, reply->len) << std::endl;
freeReplyObject(reply);
return false;
}
Expand All @@ -177,7 +177,8 @@ void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
break;
case REDIS_REPLY_ERROR:
throw FileNotGoodException(std::string(
"Failed to get keys from database: ") + reply->str);
"Failed to get keys from database: ") +
std::string(reply->str, reply->len));
}
freeReplyObject(reply);
}
Expand Down

0 comments on commit 57a4619

Please sign in to comment.