I am trying out my hand at libfuzzer and I am facing 'heap overflow' at malloc. The code snippet is as follows
int LLVMFuzzerTestOneInput(
const unsigned char * Data,
size_t Size
) {
initialize_state_before_fuzzing();
size_t charOffset = 0;
size_t testValueSize = 0;
size_t arrayLength = 0;
size_t arrayLength2 = 0;
const size_t FIXED_BUFFER_SIZE = 4096;
if (Size == 0)
return 0;
uint8_t *testValue_1 = malloc(Size);
testValueSize = Size;
for (size_t i = 0; i < testValueSize && charOffset < Size; i++) {
testValue_1[i] = (uint8_t) Data[charOffset];
charOffset++;
}
The overflow happens when Data=""
and Size = 7
. My question is why does libfuzzer give data that is not equal to the size? How to avoid this?
Also, even if Data is NULL, why does malloc cause heap overflow?