Copilot commented on code in PR #3842:
URL: https://github.com/apache/avro/pull/3842#discussion_r3563656276


##########
lang/c/tests/test_avro_4275.c:
##########
@@ -0,0 +1,377 @@
+/*
+ * 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 <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'
+};
+
+/*
+ * A valid array with negative block count: [-3] means 3 items follow,
+ * preceded by the block byte-size.
+ *
+ * Layout:
+ *   05        = varint 5 = zigzag(-3) => block_count = -3
+ *   06        = varint 6 = zigzag(3)  => block_size = 3 bytes
+ *   14 28 3C  = zigzag ints: 10, 20, 30
+ *   00        = terminator (block_count = 0)
+ */
+static const char valid_neg_block_array[] = {
+    '\x05',                     /* block_count = -3 */
+    '\x06',                     /* block_size = 3 */
+    '\x14', '\x28', '\x3C',    /* ints: 10, 20, 30 */
+    '\x00'                      /* terminator */
+};
+
+/*
+ * A valid map with negative block count: [-2] means 2 entries follow.
+ *
+ * Layout:
+ *   03        = varint 3 = zigzag(-2) => block_count = -2
+ *   0E        = varint 14 = zigzag(7) => block_size = 7 bytes
+ *   02 61 14  = key "a" (len=1, 'a'), value int 10
+ *   02 62 28  = key "b" (len=1, 'b'), value int 20
+ *   00        = terminator
+ */
+static const char valid_neg_block_map[] = {
+    '\x03',                     /* block_count = -2 */
+    '\x0E',                     /* block_size = 7 */
+    '\x02', '\x61', '\x14',    /* key "a", value 10 */
+    '\x02', '\x62', '\x28',    /* key "b", value 20 */

Review Comment:
   The block size for `valid_neg_block_map` is documented/encoded as 7 bytes, 
but the two entries shown are 3 bytes each (len=1 + key byte + 1-byte int), 
totaling 6 bytes. Keeping this consistent helps ensure the test truly 
represents a valid negative-block map encoding.



##########
lang/c/src/value-read.c:
##########
@@ -53,7 +53,15 @@ read_array_value(avro_reader_t reader, avro_value_t *dest)
 
        while (block_count != 0) {
                if (block_count < 0) {
-                       block_count = block_count * -1;
+                       /* Safe negation: avoid undefined behavior when
+                        * block_count == INT64_MIN, since -INT64_MIN is not
+                        * representable in int64_t (CWE-190). Use the
+                        * -(x+1)+1 idiom to negate without overflow. */
+                       block_count = -(block_count + 1) + 1;
+                       if (block_count <= 0) {
+                               avro_set_error("Invalid array block count");
+                               return EINVAL;
+                       }

Review Comment:
   The current negation `block_count = -(block_count + 1) + 1;` still overflows 
for `block_count == INT64_MIN` because the final `+ 1` cannot be represented in 
`int64_t`, so this does not actually eliminate the undefined behavior it aims 
to fix. A direct `INT64_MIN` guard followed by `block_count = -block_count;` 
avoids UB and keeps the code simple.



##########
lang/c/src/value-read.c:
##########
@@ -89,7 +97,15 @@ read_map_value(avro_reader_t reader, avro_value_t *dest)
 
        while (block_count != 0) {
                if (block_count < 0) {
-                       block_count = block_count * -1;
+                       /* Safe negation: avoid undefined behavior when
+                        * block_count == INT64_MIN, since -INT64_MIN is not
+                        * representable in int64_t (CWE-190). Use the
+                        * -(x+1)+1 idiom to negate without overflow. */
+                       block_count = -(block_count + 1) + 1;
+                       if (block_count <= 0) {
+                               avro_set_error("Invalid map block count");
+                               return EINVAL;
+                       }

Review Comment:
   Same issue as in `read_array_value()`: `block_count = -(block_count + 1) + 
1;` can still overflow when `block_count == INT64_MIN` due to the final `+ 1`, 
so the intended UB fix is incomplete. Add an explicit `INT64_MIN` guard before 
negating and then use `block_count = -block_count;`.



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