Skip to content

Commit 57a4619

Browse files
committedJan 8, 2016
Fix redis error reporting
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.
1 parent 0bbbc6e commit 57a4619

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
 

‎src/database-redis.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
9292

9393
if (reply->type == REDIS_REPLY_ERROR) {
9494
warningstream << "saveBlock: saving block " << PP(pos)
95-
<< " failed: " << reply->str << std::endl;
95+
<< " failed: " << std::string(reply->str, reply->len) << std::endl;
9696
freeReplyObject(reply);
9797
return false;
9898
}
@@ -119,7 +119,7 @@ std::string Database_Redis::loadBlock(const v3s16 &pos)
119119
return str;
120120
}
121121
case REDIS_REPLY_ERROR: {
122-
std::string errstr = reply->str;
122+
std::string errstr(reply->str, reply->len);
123123
freeReplyObject(reply);
124124
errorstream << "loadBlock: loading block " << PP(pos)
125125
<< " failed: " << errstr << std::endl;
@@ -134,7 +134,7 @@ std::string Database_Redis::loadBlock(const v3s16 &pos)
134134
}
135135
errorstream << "loadBlock: loading block " << PP(pos)
136136
<< " returned invalid reply type " << reply->type
137-
<< ": " << reply->str << std::endl;
137+
<< ": " << std::string(reply->str, reply->len) << std::endl;
138138
freeReplyObject(reply);
139139
throw FileNotGoodException(std::string(
140140
"Redis command 'HGET %s %s' gave invalid reply."));
@@ -151,7 +151,7 @@ bool Database_Redis::deleteBlock(const v3s16 &pos)
151151
"Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
152152
} else if (reply->type == REDIS_REPLY_ERROR) {
153153
warningstream << "deleteBlock: deleting block " << PP(pos)
154-
<< " failed: " << reply->str << std::endl;
154+
<< " failed: " << std::string(reply->str, reply->len) << std::endl;
155155
freeReplyObject(reply);
156156
return false;
157157
}
@@ -177,7 +177,8 @@ void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
177177
break;
178178
case REDIS_REPLY_ERROR:
179179
throw FileNotGoodException(std::string(
180-
"Failed to get keys from database: ") + reply->str);
180+
"Failed to get keys from database: ") +
181+
std::string(reply->str, reply->len));
181182
}
182183
freeReplyObject(reply);
183184
}

0 commit comments

Comments
 (0)
Please sign in to comment.