iemejia commented on code in PR #3854: URL: https://github.com/apache/avro/pull/3854#discussion_r3568170647
########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,183 @@ +/* + * 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 <optional> +#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 { + +// Return the underlying status (0 on success) rather than asserting inside, so +// these can be called from DecompressLimitGuard's destructor with a +// non-throwing check (a throwing BOOST_REQUIRE in a noexcept destructor would +// terminate the process instead of reporting a clean failure). +static int setDecompressLimit(const char *value) { +#ifdef _WIN32 + return _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value); +#else + return setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1); +#endif +} + +static int unsetDecompressLimit() { +#ifdef _WIN32 + return _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""); +#else + return unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"); +#endif +} + +// Saves AVRO_MAX_DECOMPRESS_LENGTH on construction and restores it on +// destruction, so a test's override does not leak into other test cases that +// share the process. +struct DecompressLimitGuard { + std::optional<std::string> previous; + DecompressLimitGuard() { + const char *env = std::getenv("AVRO_MAX_DECOMPRESS_LENGTH"); + if (env != nullptr) { + previous = std::string(env); + } + } + ~DecompressLimitGuard() { + // Use a non-throwing check: this runs in a (noexcept) destructor, where a + // throwing BOOST_REQUIRE would terminate the process. + if (previous) { + BOOST_CHECK_EQUAL(setDecompressLimit(previous->c_str()), 0); + } else { + BOOST_CHECK_EQUAL(unsetDecompressLimit(), 0); + } + } +}; + +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(); +} Review Comment: Fixed — tempFile() now prefixes the filename with the process id (getpid on POSIX, _getpid on Windows), so concurrent runs and stale files no longer collide. -- 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]
