Check dictionary sizes to avoid any heap overflows
Tested:
Unit tested
Signed-off-by: Kasun Athukorala <kasunath@google.com>
Change-Id: I34972b7f4daf0e818461ef2a4842966a247fc20e
diff --git a/test/include/bej_common_test.hpp b/test/include/bej_common_test.hpp
index 74d8c83..9d6165d 100644
--- a/test/include/bej_common_test.hpp
+++ b/test/include/bej_common_test.hpp
@@ -28,8 +28,11 @@
{
nlohmann::json expectedJson;
const uint8_t* schemaDictionary;
+ uint32_t schemaDictionarySize;
const uint8_t* annotationDictionary;
+ uint32_t annotationDictionarySize;
const uint8_t* errorDictionary;
+ uint32_t errorDictionarySize;
std::span<const uint8_t> encodedStream;
};
@@ -65,15 +68,19 @@
jsonInput >> expJson;
static uint8_t schemaDictBuffer[maxBufferSize];
- if (readBinaryFile(files.schemaDictionaryFile,
- std::span(schemaDictBuffer, maxBufferSize)) == 0)
+ uint32_t schemaDictSize = 0;
+ if ((schemaDictSize =
+ readBinaryFile(files.schemaDictionaryFile,
+ std::span(schemaDictBuffer, maxBufferSize))) == 0)
{
return std::nullopt;
}
static uint8_t annoDictBuffer[maxBufferSize];
- if (readBinaryFile(files.annotationDictionaryFile,
- std::span(annoDictBuffer, maxBufferSize)) == 0)
+ uint32_t annoDictSize = 0;
+ if ((annoDictSize =
+ readBinaryFile(files.annotationDictionaryFile,
+ std::span(annoDictBuffer, maxBufferSize))) == 0)
{
return std::nullopt;
}
@@ -87,10 +94,12 @@
}
static uint8_t errorDict[maxBufferSize];
+ uint32_t errorDictSize = 0;
if (readErrorDictionary)
{
- if (readBinaryFile(files.errorDictionaryFile,
- std::span(errorDict, maxBufferSize)) == 0)
+ if ((errorDictSize =
+ readBinaryFile(files.errorDictionaryFile,
+ std::span(errorDict, maxBufferSize))) == 0)
{
return std::nullopt;
}
@@ -99,8 +108,11 @@
BejTestInputs inputs = {
.expectedJson = expJson,
.schemaDictionary = schemaDictBuffer,
+ .schemaDictionarySize = schemaDictSize,
.annotationDictionary = annoDictBuffer,
+ .annotationDictionarySize = annoDictSize,
.errorDictionary = errorDict,
+ .errorDictionarySize = errorDictSize,
.encodedStream = std::span(encBuffer, encLen),
};
return inputs;