Copilot commented on code in PR #3722:
URL: https://github.com/apache/brpc/pull/3722#discussion_r4512786595
##########
src/brpc/redis_command.cpp:
##########
@@ -657,6 +658,18 @@ RedisCommandConsumeState
RedisCommandParser::ConsumeImpl(butil::IOBuf& buf,
*err = PARSE_ERROR_ABSOLUTELY_WRONG;
return CONSUME_STATE_ERROR;
}
+ // Parsed arguments stay in the connection arena until the whole command
+ // has arrived, so their sum, not only each argument, must be bounded by
+ // -redis_max_allocation_size. Payload bytes are charged here; the _args
+ // descriptors are bounded by the array-count check above.
+ if ((uint64_t)_used_bytes + (uint64_t)len >
+ (uint64_t)FLAGS_redis_max_allocation_size) {
+ LOG(ERROR) << "accumulated command allocation exceeds max allocation "
+ "size! max=" << FLAGS_redis_max_allocation_size
+ << ", already=" << _used_bytes << ", requesting=" << len;
+ *err = PARSE_ERROR_ABSOLUTELY_WRONG;
+ return CONSUME_STATE_ERROR;
+ }
Review Comment:
This enforces the limit even when `FLAGS_redis_max_allocation_size` is 0
(and potentially changes behavior vs the previous `> 0` gating). With
`FLAGS_redis_max_allocation_size == 0`, any non-empty bulk string will be
rejected. Make the check conditional on `FLAGS_redis_max_allocation_size > 0`
to preserve the established 'disabled when <= 0' semantics.
##########
src/brpc/redis_reply.cpp:
##########
@@ -32,6 +32,22 @@ DEFINE_int32(redis_max_reply_depth, 128,
//BAIDU_CASSERT(sizeof(RedisReply) == 24, size_match);
const int RedisReply::npos = -1;
+// Charge `need' bytes of payload allocation to the per-reply budget kept in
+// `used_bytes'. Returns false if the budget would be exceeded, in which case
+// nothing is charged.
+static bool ChargeReplyAllocation(uint32_t* used_bytes, uint64_t need) {
+ if (FLAGS_redis_max_allocation_size < 0 ||
+ (uint64_t)*used_bytes + need >
+ (uint64_t)FLAGS_redis_max_allocation_size) {
+ LOG(ERROR) << "accumulated reply allocation exceeds max allocation "
+ "size! max=" << FLAGS_redis_max_allocation_size
+ << ", already=" << *used_bytes << ", requesting=" << need;
+ return false;
+ }
+ *used_bytes += need;
+ return true;
+}
Review Comment:
`FLAGS_redis_max_allocation_size < 0` currently forces *all* replies to fail
budget charging (returns false). Historically this flag is enforced only when
`FLAGS_redis_max_allocation_size > 0` (0/negative disables the cap). Update
`ChargeReplyAllocation` to treat `<= 0` as 'no limit' and only reject when the
flag is positive and the sum would exceed it.
##########
src/brpc/redis_reply.cpp:
##########
@@ -246,6 +267,9 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf&
buf, int depth,
buf.cutn(_data.short_str, len);
_data.short_str[len] = '\0';
} else {
+ if (!ChargeReplyAllocation(used_bytes, len)) {
+ return PARSE_ERROR_ABSOLUTELY_WRONG;
+ }
char* d = (char*)_arena->allocate((len/8 + 1)*8);
Review Comment:
Same undercounting issue as the bulk-string path: charging `len` but
allocating `(len/8 + 1) * 8` can exceed the configured budget in real memory
use. Consider charging the rounded allocation size here as well, and adjust
tests that rely on exact budget boundaries accordingly.
##########
src/brpc/redis_reply.cpp:
##########
@@ -186,6 +202,11 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf&
buf, int depth,
str.copy_to_cstr(_data.short_str, (size_t)-1L, 1/*skip fc*/);
return PARSE_OK;
}
+ // Charge the payload to the per-reply budget so that an array of
+ // individually allowed strings cannot exceed the cap in total.
+ if (!ChargeReplyAllocation(used_bytes, len)) {
+ return PARSE_ERROR_ABSOLUTELY_WRONG;
+ }
char* d = (char*)_arena->allocate((len/8 + 1)*8);
Review Comment:
The budget is charged by `len`, but the arena allocation is rounded up to
`(len/8 + 1) * 8` bytes (and effectively includes space for the terminator).
This undercounts actual allocated bytes and can allow total arena allocation to
exceed `-redis_max_allocation_size` by alignment padding across many strings.
Charge the same number of bytes that will be allocated (the rounded-up size) so
the flag matches its documented meaning.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]