From 4ca9c3f8fa375b9cb4ce879872f5ce4b879a8b0a Mon Sep 17 00:00:00 2001 From: Jake Jendzurski Date: Mon, 13 Jul 2026 13:27:17 -0400 Subject: [PATCH] added fuzztest for blobutil readNBytes Signed-off-by: Jake Jendzurski --- .../s_bmqfuzz/package/s_bmqfuzz.mem | 1 + ..._bmqfuzz_bmqu_blobutil_readnbytes.fuzz.cpp | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/standalones/s_bmqfuzz/s_bmqfuzz_bmqu_blobutil_readnbytes.fuzz.cpp diff --git a/src/standalones/s_bmqfuzz/package/s_bmqfuzz.mem b/src/standalones/s_bmqfuzz/package/s_bmqfuzz.mem index 2fdd618968..30375f196a 100644 --- a/src/standalones/s_bmqfuzz/package/s_bmqfuzz.mem +++ b/src/standalones/s_bmqfuzz/package/s_bmqfuzz.mem @@ -1,4 +1,5 @@ s_bmqfuzz_bmqt_uri.fuzz +s_bmqfuzz_bmqu_blobutil_readnbytes.fuzz s_bmqfuzz_bmqu_stringutil_match.fuzz s_bmqfuzz_eval.fuzz s_bmqfuzz_parseutil.fuzz diff --git a/src/standalones/s_bmqfuzz/s_bmqfuzz_bmqu_blobutil_readnbytes.fuzz.cpp b/src/standalones/s_bmqfuzz/s_bmqfuzz_bmqu_blobutil_readnbytes.fuzz.cpp new file mode 100644 index 0000000000..a2754bf766 --- /dev/null +++ b/src/standalones/s_bmqfuzz/s_bmqfuzz_bmqu_blobutil_readnbytes.fuzz.cpp @@ -0,0 +1,69 @@ +// Copyright 2026 Bloomberg Finance L.P. +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include +#include +#include + +using namespace BloombergLP; + +namespace { + +const int k_MAX_BUFFERS = 32; +const int k_MAX_BUF_SIZE = 512; +const int k_MAX_LENGTH = 64 * 1024; + +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + FuzzedDataProvider provider(data, size); + + bslma::Allocator* alloc = bslma::Default::allocator(); + bdlbb::Blob blob(alloc); + + const int numBuffers = provider.ConsumeIntegralInRange(0, k_MAX_BUFFERS); + for (int i = 0; i < numBuffers; ++i) { + const int bufSize = provider.ConsumeIntegralInRange(1, k_MAX_BUF_SIZE); + bsl::shared_ptr bufData( + static_cast(alloc->allocate(bufSize)), + alloc); + const std::vector bytes = provider.ConsumeBytes( + bufSize); + bsl::memcpy(bufData.get(), bytes.data(), bytes.size()); + blob.appendDataBuffer(bdlbb::BlobBuffer(bufData, bufSize)); + } + + blob.setLength(provider.ConsumeIntegralInRange(0, blob.length())); + + const int startBuffer = provider.ConsumeIntegralInRange(-2, + numBuffers + 2); + const int startByte = provider.ConsumeIntegralInRange(-2, + k_MAX_BUF_SIZE + 2); + const int length = provider.ConsumeIntegralInRange(0, k_MAX_LENGTH); + bsl::vector out(length > 0 ? length : 1, alloc); + + bmqu::BlobUtil::readNBytes(out.data(), + blob, + bmqu::BlobPosition(startBuffer, startByte), + length); + + return 0; +}