summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/fuzz/fuzz_nbt_reader.cpp
blob: d6b1291c6fb51cfea24110c2326636d17286c7e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <cstdint>
#include <exception>
#include <sstream>
#include <string>

#include "io/stream_reader.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
	// Avoid excessive allocations on pathological inputs
	// Very strict limit to prevent array allocation bombs
	// E.g., NBT array with count 0x92929292 would allocate 3.2GB
	constexpr size_t kMaxInputSize = 4 * 1024; // 4 KiB (was 64 KiB)
	if (!data || size == 0 || size > kMaxInputSize)
	{
		return 0;
	}

	try
	{
		// Use custom string stream without extra copies
		std::istringstream stream(std::string(reinterpret_cast<const char*>(data), size), std::ios::binary);
		nbt::io::read_compound(stream);
	}
	catch (const std::exception&)
	{
		// Expected for malformed inputs or resource exhaustion
	}
	catch (const std::bad_alloc&)
	{
		// Handle out-of-memory gracefully
		return -1;
	}

	return 0;
}