wwbmmm commented on code in PR #3722:
URL: https://github.com/apache/brpc/pull/3722#discussion_r4520757163
##########
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:
[replied by brpc-oncall robot] Done: the cumulative check is now gated on
`FLAGS_redis_max_allocation_size > 0`. FYI, a non-empty bulk string was already
rejected when the flag is 0 or negative by the per-string check `len >
FLAGS_redis_max_allocation_size` a few lines above, so the gating does not
change the outcome for the disabled case; it just keeps the style consistent
with the reply parser.
##########
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:
[replied by brpc-oncall robot] Done: `ChargeReplyAllocation` now only
enforces the budget when `FLAGS_redis_max_allocation_size > 0`, which matches
the previous `FLAGS > 0` gating of the array budget. With a non-positive flag
nothing is charged against a limit (individual strings/arrays are still
rejected by their own per-item checks).
##########
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:
[replied by brpc-oncall robot] Intentional: the pre-existing per-string
checks accept a string of exactly `-redis_max_allocation_size` bytes (`len >
FLAGS`), and there is an existing unit test that asserts a simple string at the
limit parses. Charging the 8-byte aligned arena size would reject payloads at
the documented cap. The padding is at most 7 bytes per string, i.e. bounded
constant-factor overhead, so I kept charging payload bytes and documented the
reason on `ChargeReplyAllocation`.
##########
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:
[replied by brpc-oncall robot] Same reasoning as for the simple-string path:
a bulk string of exactly the limit must stay valid (the per-string check is
`len > FLAGS`), and the alignment padding is bounded by 7 bytes per string. The
helper now documents that payload bytes are charged rather than the aligned
arena size.
--
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]