Skip to content

Commit a9923b5

Browse files
committed
Add Integer Overflow Sanity Check
This change adds a sanity check prior to a static cast of `int32_t` to `int64_t` to use as an offset. While the current code cannot enter this state, this protects against future changes.
1 parent ebf8bff commit a9923b5

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/http_server.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,14 @@ ReadDataFromJsonHelper(
606606
const char* cstr;
607607
size_t len = 0;
608608
RETURN_IF_ERR(tensor_data.AsString(&cstr, &len));
609-
if (static_cast<int64_t>(*counter + len + sizeof(uint32_t)) >
610-
expected_cnt) {
609+
// Quick sanity check to ensure we don't write beyond `expected_cnt`.
610+
int32_t value = *counter + len + sizeof(uint32_t);
611+
if (len < 0 || value < 0) {
612+
return TRITONSERVER_ErrorNew(
613+
TRITONSERVER_ERROR_INTERNAL,
614+
"Unable to parse 'data' field: string length is negative");
615+
}
616+
if (static_cast<int64_t>(value) > expected_cnt) {
611617
return TRITONSERVER_ErrorNew(
612618
TRITONSERVER_ERROR_INTERNAL,
613619
"Shape does not match true shape of 'data' field");

0 commit comments

Comments
 (0)