Copilot commented on code in PR #3854:
URL: https://github.com/apache/avro/pull/3854#discussion_r3564611109
##########
lang/c++/impl/DataFile.cc:
##########
@@ -568,6 +597,18 @@ void DataFileReaderBase::readDataBlock() {
int b4 = compressed_[len - 1] & 0xFF;
checksum = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4);
+ {
+ // Reject an over-large block before allocating for it, based on
the
+ // uncompressed length declared in the Snappy block header.
+ size_t declared = 0;
+ if (snappy::GetUncompressedLength(reinterpret_cast<const char
*>(compressed_.data()),
+ len - 4, &declared) &&
+ declared > maxDecompressLength()) {
+ throw Exception(
+ "Decompressed block size {} exceeds the maximum allowed of
{} bytes",
+ declared, maxDecompressLength());
+ }
+ }
Review Comment:
In the Snappy path, `maxDecompressLength()` is called multiple times inside
the same check/throw. Since it reads/parses the environment each time, it’s
better to compute it once and use the same value for both the comparison and
the exception message.
##########
lang/c++/test/DecompressionLimitTests.cc:
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * https://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.
+ */
+
+// AVRO-4285: a data-file block is decompressed according to the file's codec.
A
+// block with a very high compression ratio (or a malformed block) can expand
to
+// far more memory than its compressed size. Reading such a block must be
+// rejected once its decompressed size would exceed the configured maximum,
which
+// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH.
+
+#include <cstdlib>
+#include <filesystem>
+#include <sstream>
+#include <string>
+
+#include <boost/test/included/unit_test.hpp>
+
+#include "Compiler.hh"
+#include "DataFile.hh"
+#include "Exception.hh"
+#include "ValidSchema.hh"
+
+namespace avro {
+
+static void setDecompressLimit(const char *value) {
+#ifdef _WIN32
+ _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value);
+#else
+ setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1);
+#endif
+}
+
+static ValidSchema stringSchema() {
+ std::istringstream iss("\"string\"");
+ ValidSchema vs;
+ compileJsonSchema(iss, vs);
+ return vs;
+}
+
+static std::string tempFile(const char *name) {
+ return (std::filesystem::temp_directory_path() / name).string();
+}
+
+// Write a single, highly compressible value with the given codec, then read it
+// back with a small decompression limit and confirm the read is rejected.
+static void checkCodecRejectsOversized(Codec codec, const char *name) {
+ ValidSchema schema = stringSchema();
+ std::string path = tempFile(name);
+ std::string big(4 * 1024 * 1024, 'a'); // 4 MiB, compresses tiny
+
+ // Let any writer exception propagate: a failure here is a real problem
+ // (e.g. permissions or I/O), not a reason to silently skip. Codecs that
are
+ // not compiled in are excluded via the #ifdef guards on the callers below.
+ {
+ DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 *
1024, codec);
+ writer.write(big);
+ writer.close();
+ }
+
+ setDecompressLimit("1048576"); // 1 MiB, smaller than the 4 MiB block
+
+ bool rejected = false;
+ try {
+ DataFileReader<std::string> reader(path.c_str(), schema);
+ std::string out;
+ reader.read(out); // triggers block decompression
+ } catch (const Exception &) {
+ rejected = true;
+ }
+ std::filesystem::remove(path);
+ BOOST_CHECK_MESSAGE(rejected, std::string("codec not bounded: ") + name);
+}
Review Comment:
This test sets `AVRO_MAX_DECOMPRESS_LENGTH` but never restores the previous
value. Because Boost unit tests run in a single process, this can leak into
later tests and cause unrelated failures depending on execution order or an
existing user-provided env var.
##########
lang/c++/impl/DataFile.cc:
##########
@@ -55,6 +58,32 @@ const size_t maxSyncInterval = 1u << 30;
// Recommended by https://www.zlib.net/zlib_how.html
const size_t zlibBufGrowSize = 128 * 1024;
+// Default upper bound, in bytes, on the size a single data-file block may
+// decompress to. A block with a very high compression ratio (or a malformed
+// block) can otherwise expand to far more memory than its compressed size.
+// Mirrors the Java SDK's decompression limit (AVRO-4247). Overridable with the
+// AVRO_MAX_DECOMPRESS_LENGTH environment variable.
+const size_t defaultMaxDecompressLength = static_cast<size_t>(200) * 1024 *
1024; // 200 MiB
+
+size_t maxDecompressLength() {
+ const char *env = std::getenv("AVRO_MAX_DECOMPRESS_LENGTH");
+ if (env != nullptr && *env != '\0') {
+ errno = 0;
+ char *end = nullptr;
+ unsigned long long value = std::strtoull(env, &end, 10);
+ // Reject a leading sign (strtoull would otherwise wrap it) and clamp
to
+ // what size_t can represent so a huge value does not truncate on
32-bit
+ // (or smaller size_t) builds.
+ if (errno == 0 && end != nullptr && *end == '\0' && value > 0 &&
env[0] != '-') {
+ if (value > std::numeric_limits<size_t>::max()) {
+ return std::numeric_limits<size_t>::max();
+ }
+ return static_cast<size_t>(value);
+ }
+ }
+ return defaultMaxDecompressLength;
+}
Review Comment:
`maxDecompressLength()` only checks `env[0] != '-'`, but `strtoull` skips
leading whitespace, so values like " -1" bypass the sign check and end up
clamping to `size_t` max (effectively disabling the limit). Consider rejecting
both '+'/'-' after trimming leading whitespace and parsing from the trimmed
pointer.
##########
lang/c++/test/DecompressionLimitTests.cc:
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * https://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.
+ */
+
+// AVRO-4285: a data-file block is decompressed according to the file's codec.
A
+// block with a very high compression ratio (or a malformed block) can expand
to
+// far more memory than its compressed size. Reading such a block must be
+// rejected once its decompressed size would exceed the configured maximum,
which
+// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH.
+
+#include <cstdlib>
+#include <filesystem>
+#include <sstream>
+#include <string>
+
+#include <boost/test/included/unit_test.hpp>
+
+#include "Compiler.hh"
+#include "DataFile.hh"
+#include "Exception.hh"
+#include "ValidSchema.hh"
+
+namespace avro {
+
+static void setDecompressLimit(const char *value) {
+#ifdef _WIN32
+ _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value);
+#else
+ setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1);
+#endif
+}
+
+static ValidSchema stringSchema() {
+ std::istringstream iss("\"string\"");
+ ValidSchema vs;
+ compileJsonSchema(iss, vs);
+ return vs;
+}
+
+static std::string tempFile(const char *name) {
+ return (std::filesystem::temp_directory_path() / name).string();
+}
+
+// Write a single, highly compressible value with the given codec, then read it
+// back with a small decompression limit and confirm the read is rejected.
+static void checkCodecRejectsOversized(Codec codec, const char *name) {
+ ValidSchema schema = stringSchema();
+ std::string path = tempFile(name);
+ std::string big(4 * 1024 * 1024, 'a'); // 4 MiB, compresses tiny
+
+ // Let any writer exception propagate: a failure here is a real problem
+ // (e.g. permissions or I/O), not a reason to silently skip. Codecs that
are
+ // not compiled in are excluded via the #ifdef guards on the callers below.
+ {
+ DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 *
1024, codec);
+ writer.write(big);
+ writer.close();
+ }
+
+ setDecompressLimit("1048576"); // 1 MiB, smaller than the 4 MiB block
+
+ bool rejected = false;
+ try {
+ DataFileReader<std::string> reader(path.c_str(), schema);
+ std::string out;
+ reader.read(out); // triggers block decompression
+ } catch (const Exception &) {
+ rejected = true;
+ }
+ std::filesystem::remove(path);
+ BOOST_CHECK_MESSAGE(rejected, std::string("codec not bounded: ") + name);
+}
+
+static void testDeflateDecompressionLimit() {
+ checkCodecRejectsOversized(DEFLATE_CODEC,
"avro_decompress_limit_deflate.avro");
+}
+
+static void testSnappyDecompressionLimit() {
+#ifdef SNAPPY_CODEC_AVAILABLE
+ checkCodecRejectsOversized(SNAPPY_CODEC,
"avro_decompress_limit_snappy.avro");
+#else
+ BOOST_TEST_MESSAGE("Snappy codec not available; skipping");
+#endif
+}
+
+static void testZstdDecompressionLimit() {
+#ifdef ZSTD_CODEC_AVAILABLE
+ checkCodecRejectsOversized(ZSTD_CODEC, "avro_decompress_limit_zstd.avro");
+#else
+ BOOST_TEST_MESSAGE("Zstandard codec not available; skipping");
+#endif
+}
+
+static void testWithinLimitStillReads() {
+ ValidSchema schema = stringSchema();
+ std::string path = tempFile("avro_decompress_within_limit.avro");
+ std::string payload = "hello world";
+
+ {
+ DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 *
1024, DEFLATE_CODEC);
+ writer.write(payload);
+ writer.close();
+ }
+
+ setDecompressLimit("1048576");
+
+ std::string out;
+ {
+ DataFileReader<std::string> reader(path.c_str(), schema);
+ BOOST_CHECK(reader.read(out));
+ }
Review Comment:
Like `checkCodecRejectsOversized()`, this test sets
`AVRO_MAX_DECOMPRESS_LENGTH` without restoring it, which can affect subsequent
tests in the same process (and can override an existing env var from the test
environment).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]