iemejia commented on code in PR #3854:
URL: https://github.com/apache/avro/pull/3854#discussion_r3564494310


##########
lang/c++/impl/DataFile.cc:
##########
@@ -18,6 +18,7 @@
 
 #include "DataFile.hh"
 #include "Compiler.hh"
+#include <cstdlib>
 #include <cstring>
 #include "Exception.hh"

Review Comment:
   Fixed in 66809e0fa4 — added <cerrno> and <limits> to DataFile.cc.



##########
lang/c++/impl/DataFile.cc:
##########
@@ -55,6 +56,26 @@ 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);
+        if (errno == 0 && end != nullptr && *end == '\0' && value > 0) {
+            return static_cast<size_t>(value);
+        }
+    }
+    return defaultMaxDecompressLength;
+}

Review Comment:
   Fixed in 66809e0fa4 — maxDecompressLength rejects a leading sign and clamps 
to std::numeric_limits<size_t>::max(), so a huge configured value cannot 
truncate on 32-bit builds.



##########
lang/c++/impl/ZstdDecompressWrapper.cc:
##########
@@ -44,8 +44,19 @@ std::string ZstdDecompressWrapper::decompress(const 
std::vector<char> &compresse
                 throw Exception("ZSTD decompression error: {}", 
ZSTD_getErrorName(ret));
             }
             uncompressed.append(tmp.data(), out.pos);
+            // Reject a block that decompresses to more than the allowed 
maximum.
+            if (uncompressed.size() > maxLength) {
+                throw Exception(
+                    "Decompressed block size exceeds the maximum allowed of {} 
bytes", maxLength);
+            }

Review Comment:
   Fixed in 66809e0fa4 — the streaming zstd path now checks the limit before 
appending (out.pos > maxLength - uncompressed.size()), so the buffer never 
grows past the limit.



##########
lang/c++/test/DecompressionLimitTests.cc:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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
+
+    try {
+        DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 * 
1024, codec);
+        writer.write(big);
+        writer.close();
+    } catch (const Exception &) {
+        // Codec not available in this build; nothing to exercise.
+        std::filesystem::remove(path);
+        return;
+    }

Review Comment:
   Fixed in 66809e0fa4 — the test no longer swallows writer exceptions (real 
failures surface); snappy/zstd cases are gated on the SNAPPY_CODEC_AVAILABLE / 
ZSTD_CODEC_AVAILABLE macros instead.



-- 
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]

Reply via email to