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


##########
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:
   Updated the PR description to accurately reflect the implementation: 
explicit `INT64_MIN` guard returning `EINVAL` followed by safe `-block_count` 
negation.



##########
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:
   Good catch. Applied the same `INT64_MIN` guard + safe negation to 
`skip_array`/`skip_map` in `datum_skip.c` and `read_array`/`read_map` in 
`consume-binary.c`.



##########
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:
   Fixed — using explicit `(char)0xFF` casts to make the intent clear and avoid 
implementation-defined narrowing diagnostics on platforms with signed char.



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