Copilot commented on code in PR #3842:
URL: https://github.com/apache/avro/pull/3842#discussion_r3563710436
##########
lang/c/src/value-read.c:
##########
@@ -53,7 +53,13 @@ read_array_value(avro_reader_t reader, avro_value_t *dest)
while (block_count != 0) {
if (block_count < 0) {
- block_count = block_count * -1;
+ /* Reject INT64_MIN: its negation is not
+ * representable in int64_t (CWE-190). */
+ if (block_count == INT64_MIN) {
+ avro_set_error("Invalid array block count");
+ return EINVAL;
+ }
+ block_count = -block_count;
Review Comment:
This PR fixes INT64_MIN negation overflow for value decoding, but the same
undefined-behavior pattern (`block_count = block_count * -1`) still exists in
other block-count readers (`lang/c/src/datum_skip.c` and
`lang/c/src/consume-binary.c`) for arrays/maps. To fully address CWE-190 for
the C implementation, those call paths should get the same INT64_MIN guard +
safe negation (or another overflow-safe abs) so malformed inputs can’t trigger
UB via skip/consumer APIs.
##########
lang/c/tests/test_avro_4275.c:
##########
@@ -0,0 +1,381 @@
+/*
+ * 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.
+ */
+
+/**
+ * Regression test for INT64_MIN negation overflow in read_array_value()
+ * and read_map_value() (CWE-190).
+ *
+ * The Avro binary format encodes block counts as zigzag-encoded varints.
+ * A negative block count means the absolute value is the actual count,
+ * preceded by a byte-size field. When block_count == INT64_MIN, the
+ * negation overflows (undefined behavior in C). This test verifies that
+ * the decoder rejects such malformed input gracefully.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <avro.h>
+
+/*
+ * Zigzag encoding of INT64_MIN is 0xFFFFFFFFFFFFFFFF, which encodes as
+ * the 10-byte varint: FF FF FF FF FF FF FF FF FF 01
+ */
+static const char int64min_block_count[] = {
+ '\xFF', '\xFF', '\xFF', '\xFF', '\xFF',
+ '\xFF', '\xFF', '\xFF', '\xFF', '\x01'
+};
Review Comment:
The `\xFF` bytes are being stored in a `char[]`; on platforms with signed
`char` this can produce implementation-defined values and sometimes warnings
about narrowing. Explicitly casting the out-of-range byte constants to `char`
makes the intent clear and avoids noisy compiler diagnostics without changing
any call sites.
##########
lang/c/src/value-read.c:
##########
@@ -53,7 +53,13 @@ read_array_value(avro_reader_t reader, avro_value_t *dest)
while (block_count != 0) {
if (block_count < 0) {
- block_count = block_count * -1;
+ /* Reject INT64_MIN: its negation is not
+ * representable in int64_t (CWE-190). */
+ if (block_count == INT64_MIN) {
+ avro_set_error("Invalid array block count");
+ return EINVAL;
+ }
+ block_count = -block_count;
Review Comment:
The PR description still states the fix uses the `-(block_count + 1) + 1`
idiom and adds a post-negation non-positive guard, but the current
implementation is an explicit `INT64_MIN` check followed by `block_count =
-block_count;` (and no additional post-negation validation). Please update the
PR description to match the code (or add the described guard if it is still a
requirement).
--
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]