@@ -437,16 +437,26 @@ AllocEVBuffer(const size_t byte_size, evbuffer** evb, void** base)
437437// Recursively adds to byte_size from multi dimensional data input
438438TRITONSERVER_Error*
439439JsonBytesArrayByteSize (
440- triton::common::TritonJson::Value& tensor_data, size_t * byte_size)
440+ triton::common::TritonJson::Value& tensor_data, size_t * byte_size,
441+ int current_depth = 0 )
441442{
443+ if (current_depth >= HTTP_MAX_JSON_NESTING_DEPTH) {
444+ return TRITONSERVER_ErrorNew (
445+ TRITONSERVER_ERROR_INVALID_ARG,
446+ (" JSON nesting depth exceeds maximum allowed "
447+ " limit (" +
448+ std::to_string (HTTP_MAX_JSON_NESTING_DEPTH) + " )" )
449+ .c_str ());
450+ }
451+
442452 *byte_size = 0 ;
443453 // Recurse if not last dimension...
444454 if (tensor_data.IsArray ()) {
445455 for (size_t i = 0 ; i < tensor_data.ArraySize (); i++) {
446456 triton::common::TritonJson::Value el;
447457 RETURN_IF_ERR (tensor_data.At (i, &el));
448458 size_t byte_size_;
449- RETURN_IF_ERR (JsonBytesArrayByteSize (el, &byte_size_));
459+ RETURN_IF_ERR (JsonBytesArrayByteSize (el, &byte_size_, current_depth + 1 ));
450460 *byte_size += byte_size_;
451461 }
452462 } else {
@@ -466,20 +476,29 @@ TRITONSERVER_Error*
466476ReadDataFromJsonHelper (
467477 char * base, const TRITONSERVER_DataType dtype,
468478 triton::common::TritonJson::Value& tensor_data, int * counter,
469- int64_t expected_cnt)
479+ int64_t expected_cnt, int current_depth = 0 )
470480{
471481 // FIXME should move 'switch' statement outside the recursive function and
472482 // pass in a read data callback once data type is confirmed.
473483 // Currently 'switch' is performed on each element even through all elements
474484 // have the same data type.
475485
486+ if (current_depth >= HTTP_MAX_JSON_NESTING_DEPTH) {
487+ return TRITONSERVER_ErrorNew (
488+ TRITONSERVER_ERROR_INVALID_ARG,
489+ (" JSON nesting depth exceeds maximum allowed "
490+ " limit (" +
491+ std::to_string (HTTP_MAX_JSON_NESTING_DEPTH) + " )" )
492+ .c_str ());
493+ }
494+
476495 // Recurse on array element if not last dimension...
477496 if (tensor_data.IsArray ()) {
478497 for (size_t i = 0 ; i < tensor_data.ArraySize (); i++) {
479498 triton::common::TritonJson::Value el;
480499 RETURN_IF_ERR (tensor_data.At (i, &el));
481- RETURN_IF_ERR (
482- ReadDataFromJsonHelper ( base, dtype, el, counter, expected_cnt));
500+ RETURN_IF_ERR (ReadDataFromJsonHelper (
501+ base, dtype, el, counter, expected_cnt, current_depth + 1 ));
483502 }
484503 } else {
485504 // Check if writing to 'serialized' is overrunning the expected byte_size
0 commit comments