Throw exception instead of asserting in TJSONProtocol::readByte
Client: cpp
Patch: Hasnain Lakhani
diff --git a/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp b/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
index 3805869..1218190 100644
--- a/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
+++ b/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
@@ -1087,7 +1087,10 @@
uint32_t TJSONProtocol::readByte(int8_t& byte) {
auto tmp = (int16_t)byte;
uint32_t result = readJSONInteger(tmp);
- assert(tmp < 256);
+ if (tmp > 127 || tmp < -128) {
+ throw TProtocolException(TProtocolException::INVALID_DATA,
+ "Expected byte value; got " + to_string(tmp));
+ }
byte = (int8_t)tmp;
return result;
}
diff --git a/lib/cpp/test/JSONProtoTest.cpp b/lib/cpp/test/JSONProtoTest.cpp
index ef39385..1874ccd 100644
--- a/lib/cpp/test/JSONProtoTest.cpp
+++ b/lib/cpp/test/JSONProtoTest.cpp
@@ -366,3 +366,16 @@
BOOST_CHECK_THROW(ooe2.read(proto.get()),
apache::thrift::protocol::TProtocolException);
}
+
+BOOST_AUTO_TEST_CASE(test_json_invalid_byte_range) {
+ // Test case for byte values outside valid range
+ const char json_string[] = "\"3\":{\"i8\":849}";
+
+ std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
+ (uint8_t*)(json_string), sizeof(json_string)));
+ std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
+
+ OneOfEach ooe2;
+ BOOST_CHECK_THROW(ooe2.read(proto.get()),
+ apache::thrift::protocol::TProtocolException);
+}