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


##########
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:
   Good catch. The `-(block_count + 1) + 1` idiom indeed still overflows when 
`block_count == INT64_MIN` because `-(INT64_MIN + 1)` yields `INT64_MAX`, and 
`INT64_MAX + 1` is UB. Replaced with an explicit `INT64_MIN` guard followed by 
`block_count = -block_count;`.



##########
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:
   Fixed the same way as the array path: explicit `INT64_MIN` guard + simple 
negation.



##########
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:
   You're right, the two entries total 6 bytes (each is: 1-byte string length 
varint + 1-byte key + 1-byte int varint = 3 bytes). Fixed the block_size from 
`0x0E` (zigzag 7) to `0x0C` (zigzag 6) and updated the comment to match.



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