Copilot commented on code in PR #3853:
URL: https://github.com/apache/avro/pull/3853#discussion_r3567379442
##########
lang/c/src/codec.c:
##########
@@ -43,9 +44,46 @@
#include "avro/errors.h"
#include "avro/allocation.h"
#include "codec.h"
+#include <errno.h>
+#include <limits.h>
+#include <stdint.h>
#define DEFAULT_BLOCK_SIZE (16 * 1024)
+/*
+ * When reading a data file, each 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. To guard against
+ * unbounded allocation, the decompressed size of a single block is capped.
This
+ * mirrors the Java SDK's decompression limit (AVRO-4247). The default can be
+ * overridden with the AVRO_MAX_DECOMPRESS_LENGTH environment variable.
+ */
+#define AVRO_DEFAULT_MAX_DECOMPRESS_LENGTH ((int64_t) 200 * 1024 * 1024)
/* 200 MiB */
+
+static int64_t
+avro_max_decompress_length(void)
+{
+ const char *env = getenv("AVRO_MAX_DECOMPRESS_LENGTH");
+ if (env != NULL && *env != '\0') {
+ char *end = NULL;
+ errno = 0;
+ long long value = strtoll(env, &end, 10);
+ if (errno == 0 && end != NULL && *end == '\0' && value > 0) {
+ int64_t v = (int64_t) value;
+ /* Clamp to what size_t can address so the value is
always
+ * safe to use as an allocation cap. On 32-bit platforms
+ * size_t is narrower than int64_t, and an unclamped
value
+ * would truncate when passed to
avro_malloc/avro_realloc. */
+ if ((uint64_t) v > (uint64_t) SIZE_MAX) {
+ return (int64_t) SIZE_MAX;
+ }
+ return v;
+ }
Review Comment:
`avro_max_decompress_length()` casts the `strtoll()` result (`long long`) to
`int64_t` before validating it fits in the `int64_t` range. On platforms where
`long long` is wider than 64-bit, a large but valid
`AVRO_MAX_DECOMPRESS_LENGTH` value can overflow/wrap in the cast and
potentially bypass the decompression cap (e.g., by becoming negative before the
`SIZE_MAX` clamp). Clamp `value` to `INT64_MAX` (and then to `SIZE_MAX`) before
casting/returning.
--
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]