Skip to content

Commit d0f8f8f

Browse files
committed
Revert "Add redis allocation size limit (#3035)"
This reverts commit 75341f8.
1 parent dbc54b7 commit d0f8f8f

3 files changed

Lines changed: 10 additions & 119 deletions

File tree

src/brpc/redis_command.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "butil/logging.h"
2222
#include "brpc/log.h"
2323
#include "brpc/redis_command.h"
24-
#include "gflags/gflags.h"
2524

2625
namespace {
2726

@@ -31,8 +30,6 @@ const size_t CTX_WIDTH = 5;
3130

3231
namespace brpc {
3332

34-
DECLARE_int32(redis_max_allocation_size);
35-
3633
// Much faster than snprintf(..., "%lu", d);
3734
inline size_t AppendDecimal(char* outbuf, unsigned long d) {
3835
char buf[24]; // enough for decimal 64-bit integers
@@ -459,12 +456,6 @@ ParseError RedisCommandParser::Consume(butil::IOBuf& buf,
459456
return PARSE_ERROR_ABSOLUTELY_WRONG;
460457
}
461458
if (!_parsing_array) {
462-
if (value > (int64_t)(FLAGS_redis_max_allocation_size / sizeof(butil::StringPiece))) {
463-
LOG(ERROR) << "command array size exceeds limit! max="
464-
<< (FLAGS_redis_max_allocation_size / sizeof(butil::StringPiece))
465-
<< ", actually=" << value;
466-
return PARSE_ERROR_ABSOLUTELY_WRONG;
467-
}
468459
buf.pop_front(crlf_pos + 2/*CRLF*/);
469460
_parsing_array = true;
470461
_length = value;
@@ -479,9 +470,9 @@ ParseError RedisCommandParser::Consume(butil::IOBuf& buf,
479470
LOG(ERROR) << "string in command is nil!";
480471
return PARSE_ERROR_ABSOLUTELY_WRONG;
481472
}
482-
if (len > FLAGS_redis_max_allocation_size) {
483-
LOG(ERROR) << "command string exceeds max allocation size! max="
484-
<< FLAGS_redis_max_allocation_size << ", actually=" << len;
473+
if (len > (int64_t)std::numeric_limits<uint32_t>::max()) {
474+
LOG(ERROR) << "string in command is too long! max length=2^32-1,"
475+
" actually=" << len;
485476
return PARSE_ERROR_ABSOLUTELY_WRONG;
486477
}
487478
if (buf.size() < crlf_pos + 2 + (size_t)len + 2/*CRLF*/) {

src/brpc/redis_reply.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@
2020
#include "butil/logging.h"
2121
#include "butil/string_printf.h"
2222
#include "brpc/redis_reply.h"
23-
#include "gflags/gflags.h"
2423

2524
namespace brpc {
2625

27-
DEFINE_int32(redis_max_allocation_size, 64 * 1024 * 1024,
28-
"Maximum memory allocation size in bytes for a single redis request or reply (64MB by default)");
29-
3026
//BAIDU_CASSERT(sizeof(RedisReply) == 24, size_match);
3127
const int RedisReply::npos = -1;
3228

@@ -179,9 +175,9 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf& buf) {
179175
_data.integer = 0;
180176
return PARSE_OK;
181177
}
182-
if (len > FLAGS_redis_max_allocation_size) {
183-
LOG(ERROR) << "bulk string exceeds max allocation size! max="
184-
<< FLAGS_redis_max_allocation_size << ", actually=" << len;
178+
if (len > (int64_t)std::numeric_limits<uint32_t>::max()) {
179+
LOG(ERROR) << "bulk string is too long! max length=2^32-1,"
180+
" actually=" << len;
185181
return PARSE_ERROR_ABSOLUTELY_WRONG;
186182
}
187183
// We provide c_str(), thus even if bulk string is started with
@@ -233,14 +229,13 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf& buf) {
233229
_data.array.replies = NULL;
234230
return PARSE_OK;
235231
}
236-
int64_t array_size = sizeof(RedisReply) * count;
237-
if (array_size > FLAGS_redis_max_allocation_size) {
238-
LOG(ERROR) << "array allocation exceeds max allocation size! max="
239-
<< FLAGS_redis_max_allocation_size << ", actually=" << array_size;
232+
if (count > (int64_t)std::numeric_limits<uint32_t>::max()) {
233+
LOG(ERROR) << "Too many sub replies! max count=2^32-1,"
234+
" actually=" << count;
240235
return PARSE_ERROR_ABSOLUTELY_WRONG;
241236
}
242237
// FIXME(gejun): Call allocate_aligned instead.
243-
RedisReply* subs = (RedisReply*)_arena->allocate(array_size);
238+
RedisReply* subs = (RedisReply*)_arena->allocate(sizeof(RedisReply) * count);
244239
if (subs == NULL) {
245240
LOG(FATAL) << "Fail to allocate RedisReply[" << count << "]";
246241
return PARSE_ERROR_ABSOLUTELY_WRONG;

test/brpc_redis_unittest.cpp

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626
#include <brpc/server.h>
2727
#include <brpc/redis_command.h>
2828
#include <gtest/gtest.h>
29-
#include <gflags/gflags.h>
3029

3130
namespace brpc {
3231
DECLARE_int32(idle_timeout_second);
33-
DECLARE_int32(redis_max_allocation_size);
3432
}
3533

3634
int main(int argc, char* argv[]) {
@@ -1352,97 +1350,4 @@ TEST_F(RedisTest, server_handle_pipeline) {
13521350
ASSERT_STREQ(response.reply(7).c_str(), "world");
13531351
}
13541352

1355-
TEST_F(RedisTest, memory_allocation_limits) {
1356-
int32_t original_limit = brpc::FLAGS_redis_max_allocation_size;
1357-
brpc::FLAGS_redis_max_allocation_size = 1024;
1358-
1359-
butil::Arena arena;
1360-
1361-
// Test redis_reply.cpp limits
1362-
{
1363-
// Test bulk string exceeding limit
1364-
butil::IOBuf buf;
1365-
std::string large_string = "*1\r\n$2000\r\n";
1366-
large_string.append(2000, 'a');
1367-
large_string.append("\r\n");
1368-
buf.append(large_string);
1369-
1370-
brpc::RedisReply reply(&arena);
1371-
brpc::ParseError err = reply.ConsumePartialIOBuf(buf);
1372-
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
1373-
}
1374-
1375-
{
1376-
// Test array allocation exceeding limit
1377-
butil::IOBuf buf;
1378-
int32_t large_count = brpc::FLAGS_redis_max_allocation_size / sizeof(brpc::RedisReply) + 1;
1379-
std::string large_array = "*" + std::to_string(large_count) + "\r\n";
1380-
buf.append(large_array);
1381-
1382-
brpc::RedisReply reply(&arena);
1383-
brpc::ParseError err = reply.ConsumePartialIOBuf(buf);
1384-
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
1385-
}
1386-
1387-
// Test redis_command.cpp limits
1388-
{
1389-
// Test command string exceeding limit
1390-
brpc::RedisCommandParser parser;
1391-
butil::IOBuf buf;
1392-
std::string large_cmd = "*2\r\n$3\r\nget\r\n$2000\r\n";
1393-
large_cmd.append(2000, 'b');
1394-
large_cmd.append("\r\n");
1395-
buf.append(large_cmd);
1396-
1397-
std::vector<butil::StringPiece> args;
1398-
brpc::ParseError err = parser.Consume(buf, &args, &arena);
1399-
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
1400-
}
1401-
1402-
{
1403-
// Test command array size exceeding limit
1404-
brpc::RedisCommandParser parser;
1405-
butil::IOBuf buf;
1406-
int32_t large_array_size = brpc::FLAGS_redis_max_allocation_size / sizeof(butil::StringPiece) + 1;
1407-
std::string large_array_cmd = "*" + std::to_string(large_array_size) + "\r\n";
1408-
buf.append(large_array_cmd);
1409-
1410-
std::vector<butil::StringPiece> args;
1411-
brpc::ParseError err = parser.Consume(buf, &args, &arena);
1412-
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
1413-
}
1414-
1415-
// Test valid cases within limits
1416-
{
1417-
// Test small bulk string should work
1418-
butil::IOBuf buf;
1419-
std::string small_string = "*1\r\n$10\r\nhelloworld\r\n";
1420-
buf.append(small_string);
1421-
1422-
brpc::RedisReply reply(&arena);
1423-
brpc::ParseError err = reply.ConsumePartialIOBuf(buf);
1424-
ASSERT_EQ(brpc::PARSE_OK, err);
1425-
ASSERT_TRUE(reply.is_array());
1426-
ASSERT_EQ(1, (int)reply.size());
1427-
ASSERT_STREQ("helloworld", reply[0].c_str());
1428-
}
1429-
1430-
{
1431-
// Test small command should work
1432-
brpc::RedisCommandParser parser;
1433-
butil::IOBuf buf;
1434-
std::string small_cmd = "*2\r\n$3\r\nget\r\n$5\r\nmykey\r\n";
1435-
buf.append(small_cmd);
1436-
1437-
std::vector<butil::StringPiece> args;
1438-
brpc::ParseError err = parser.Consume(buf, &args, &arena);
1439-
ASSERT_EQ(brpc::PARSE_OK, err);
1440-
ASSERT_EQ(2, (int)args.size());
1441-
ASSERT_EQ("get", args[0].as_string());
1442-
ASSERT_EQ("mykey", args[1].as_string());
1443-
}
1444-
1445-
brpc::FLAGS_redis_max_allocation_size = original_limit;
1446-
}
1447-
14481353
} //namespace

0 commit comments

Comments
 (0)