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


##########
lang/c/src/value-read.c:
##########
@@ -38,6 +39,73 @@ static int
 read_value(avro_reader_t reader, avro_value_t *dest);
 
 
+/*
+ * Minimum number of bytes a single value of the given schema can occupy on the
+ * wire. Used to reject an array/map block count that could not be backed by 
the
+ * bytes remaining. A type that can encode to zero bytes (null) returns 0, 
which
+ * disables the collection check for it (so an array of nulls is not falsely
+ * rejected). A recursion guard breaks self-referencing schemas.
+ */
+static int64_t
+min_bytes_per_element(avro_schema_t schema, int depth)
+{
+       if (schema == NULL || depth > 64) {
+               return 0;
+       }

Review Comment:
   min_bytes_per_element() returns 0 when the recursion guard trips (depth > 
64). That causes ensure_collection_available() to be skipped (min_bytes <= 0), 
which can bypass the new pre-allocation protection for recursive/very-deep 
schemas and reintroduce the large-block-count allocation risk this PR is trying 
to prevent.



##########
lang/c/tests/test_avro_4293.c:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.
+ */
+
+/*
+ * A bytes/string value is encoded as a length prefix followed by that many
+ * bytes of data. A malicious or truncated input can declare a huge length with
+ * little or no actual data, which previously caused a correspondingly huge
+ * allocation before the shortfall was noticed. When the reader knows how many
+ * bytes remain (a memory-backed reader), the declared length must be rejected
+ * before allocating for it.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <avro.h>

Review Comment:
   This test should include <errno.h> if it’s going to assert specific error 
codes (e.g., EINVAL) for over-limit rejections; otherwise EINVAL may be 
undefined on some platforms.



##########
lang/c/tests/test_avro_4293.c:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.
+ */
+
+/*
+ * A bytes/string value is encoded as a length prefix followed by that many
+ * bytes of data. A malicious or truncated input can declare a huge length with
+ * little or no actual data, which previously caused a correspondingly huge
+ * allocation before the shortfall was noticed. When the reader knows how many
+ * bytes remain (a memory-backed reader), the declared length must be rejected
+ * before allocating for it.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <avro.h>
+
+/* Decodes buf against the given schema and returns the avro_value_read rc.
+ * A non-zero rc means the read was rejected. */
+static int try_decode(avro_value_iface_t *iface, const char *buf, size_t 
buf_size)
+{
+       int rc;
+       avro_value_t decoded;
+       avro_reader_t reader;
+
+       rc = avro_generic_value_new(iface, &decoded);
+       if (rc != 0) {
+               fprintf(stderr, "avro_generic_value_new failed: %s\n", 
avro_strerror());
+               return -1;
+       }
+
+       reader = avro_reader_memory(buf, buf_size);
+       if (reader == NULL) {
+               fprintf(stderr, "avro_reader_memory failed\n");
+               avro_value_decref(&decoded);
+               return -1;
+       }
+
+       rc = avro_value_read(reader, &decoded);
+
+       avro_reader_free(reader);
+       avro_value_decref(&decoded);
+       return rc;
+}
+
+static int check_rejects_oversized(const char *schema_literal, const char 
*label)
+{
+       avro_schema_t schema = NULL;
+       avro_value_iface_t *iface = NULL;
+       int rc;
+       int ret = EXIT_FAILURE;
+
+       /*
+        * A length prefix declaring 127 bytes (zig-zag long 254 -> varint
+        * 0xFE 0x01), followed by no data at all. The reader has 0 bytes
+        * remaining after the prefix, so a declared length of 127 must be
+        * rejected before allocating.
+        */
+       const char oversized[] = { (char) 0xFE, (char) 0x01 };
+
+       if (avro_schema_from_json_length(schema_literal, strlen(schema_literal),
+                                        &schema) != 0) {
+               fprintf(stderr, "%s: failed to parse schema: %s\n", label, 
avro_strerror());
+               return EXIT_FAILURE;
+       }
+
+       iface = avro_generic_class_from_schema(schema);
+       if (iface == NULL) {
+               fprintf(stderr, "%s: failed to create iface\n", label);
+               avro_schema_decref(schema);
+               return EXIT_FAILURE;
+       }
+
+       rc = try_decode(iface, oversized, sizeof(oversized));
+       if (rc == 0) {
+               fprintf(stderr, "%s: FAIL - oversized length was accepted\n", 
label);
+       } else {
+               fprintf(stderr, "%s: oversized length rejected as expected: 
%s\n",
+                       label, avro_strerror());
+               ret = EXIT_SUCCESS;
+       }

Review Comment:
   check_rejects_oversized() currently treats any non-zero return as success. 
That would also pass on the pre-fix behavior (e.g., ENOSPC after allocating and 
then failing to read). To validate the intended early rejection, assert that 
the error code is EINVAL (as returned by the new availability checks).



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