ip += seqHSize;
srcSize -= seqHSize;
- RETURN_ERROR_IF(dst == NULL && nbSeq > 0, dstSize_tooSmall, "NULL not handled");
+ RETURN_ERROR_IF((dst == NULL || dstCapacity == 0) && nbSeq > 0, dstSize_tooSmall, "NULL not handled");
+ RETURN_ERROR_IF(MEM_64bits() && sizeof(size_t) == sizeof(void*) && (size_t)(-1) - (size_t)dst < (size_t)(1 << 20), dstSize_tooSmall,
+ "invalid dst");
/* If we could potentially have long offsets, or we might want to use the prefetch decoder,
* compute information about the share of long offsets, and the maximum nbAdditionalBits.
* decompression function to ensure the decompressor never crashes.
*/
+#include "fuzz_data_producer.h"
#define ZSTD_STATIC_LINKING_ONLY
#include <stddef.h>
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
{
size_t const neededBufSize = ZSTD_BLOCKSIZE_MAX;
+ FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
/* Allocate all buffers and contexts if not already allocated */
if (neededBufSize > bufSize) {
free(rBuf);
- rBuf = FUZZ_malloc(neededBufSize);
+ rBuf = FUZZ_malloc_rand(neededBufSize, producer);
bufSize = neededBufSize;
}
if (!dctx) {
ZSTD_decompressBegin(dctx);
ZSTD_decompressBlock(dctx, rBuf, neededBufSize, src, size);
+ FUZZ_dataProducer_free(producer);
+
#ifndef STATEFUL_FUZZING
ZSTD_freeDCtx(dctx); dctx = NULL;
#endif
* You may select, at your option, one of the above-listed licenses.
*/
+#include "fuzz_helpers.h"
#include "fuzz_data_producer.h"
struct FUZZ_dataProducer_s{
#include <stdio.h>
#include <stdlib.h>
-#include "fuzz_helpers.h"
/* Struct used for maintaining the state of the data */
typedef struct FUZZ_dataProducer_s FUZZ_dataProducer_t;
return NULL;
}
+void* FUZZ_malloc_rand(size_t size, FUZZ_dataProducer_t *producer)
+{
+ if (size > 0) {
+ void* const mem = malloc(size);
+ FUZZ_ASSERT(mem);
+ return mem;
+ } else {
+ uintptr_t ptr = 0;
+ /* Add +- 1M 50% of the time */
+ if (FUZZ_dataProducer_uint32Range(producer, 0, 1))
+ FUZZ_dataProducer_int32Range(producer, -1000000, 1000000);
+ return (void*)ptr;
+ }
+
+}
+
int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size)
{
if (size == 0) {
#include "fuzz.h"
#include "xxhash.h"
#include "zstd.h"
+#include "fuzz_data_producer.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
*/
void* FUZZ_malloc(size_t size);
+/**
+ * malloc except returns random pointer for zero sized data and FUZZ_ASSERT
+ * that malloc doesn't fail.
+ */
+void* FUZZ_malloc_rand(size_t size, FUZZ_dataProducer_t *producer);
+
/**
* memcmp but accepts NULL.
*/