Skip to content

Commit 865432f

Browse files
authored
fix: Improve bounds checking on integer conversions (#8619)
This change improves the handling of values which could cause int32 values of over/underflow leading to undefined behavior. TRI-557
1 parent 3ef6062 commit 865432f

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/http_server.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,19 @@ ReadDataFromJsonHelper(
603603
break;
604604
}
605605
case TRITONSERVER_TYPE_BYTES: {
606-
const char* cstr;
607-
size_t len = 0;
606+
const char* cstr{nullptr};
607+
size_t len{0};
608608
RETURN_IF_ERR(tensor_data.AsString(&cstr, &len));
609-
// Quick sanity check to ensure we don't write beyond `expected_cnt`.
610-
int32_t actual_cnt = *counter + len + sizeof(uint32_t);
611-
if (actual_cnt < 0) {
609+
if (len > INT64_MAX) {
612610
return TRITONSERVER_ErrorNew(
613611
TRITONSERVER_ERROR_INTERNAL,
614-
"Unable to parse 'data' field: string length is negative");
612+
"Tensor size is too large to be processed");
615613
}
616-
if (static_cast<int64_t>(actual_cnt) > expected_cnt) {
614+
// Quick sanity check to ensure we don't write beyond `expected_cnt`.
615+
int64_t actual_cnt = static_cast<int64_t>(*counter) +
616+
static_cast<int64_t>(len) +
617+
static_cast<int64_t>(sizeof(uint32_t));
618+
if (actual_cnt < 0 || actual_cnt > expected_cnt) {
617619
return TRITONSERVER_ErrorNew(
618620
TRITONSERVER_ERROR_INTERNAL,
619621
"Shape does not match true shape of 'data' field");

0 commit comments

Comments
 (0)