Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cursor/rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The AI agents should never link to any issue or a pull request
in any GitHub repository in the code reviews!

The AI agents should not review AI agents' config files like CLAUDE.md or AGENTS.md!

5 changes: 5 additions & 0 deletions .gemini/rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The AI agents should never link to any issue or a pull request
in any GitHub repository in the code reviews!

The AI agents should not review AI agents' config files like CLAUDE.md or AGENTS.md!

5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The AI agents should never link to any issue or a pull request
in any GitHub repository in the code reviews!

The AI agents should not review AI agents' config files like CLAUDE.md or AGENTS.md!

5 changes: 5 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The AI agents should never link to any issue or a pull request
in any GitHub repository in the code reviews!

The AI agents should not review AI agents' config files like CLAUDE.md or AGENTS.md!

2 changes: 2 additions & 0 deletions lang/c/src/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ typedef struct avro_encoding_t avro_encoding_t;
{ int rval = avro_write( writer, buf, len ); if(rval) return rval; }
#define AVRO_READ(reader, buf, len) \
{ int rval = avro_read( reader, buf, len ); if(rval) return rval; }
#define AVRO_READ_OR_FREE(reader, buf, len) \
{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, 0); buf = NULL; return rval; } }
Comment on lines +99 to +100

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The AVRO_READ_OR_FREE macro uses a hardcoded size of 0 when calling avro_free. In the Avro C library, avro_free expects the actual size of the allocated block. While the default allocator might ignore this value, custom allocators (e.g., pool allocators) often rely on it to correctly manage memory. It is recommended to pass the allocation size as an additional argument to the macro to ensure compatibility with all allocator implementations.

#define AVRO_READ_OR_FREE(reader, buf, len, sz)  \
{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, sz); buf = NULL; return rval; } }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong size passed to avro_free in macro

Medium Severity

The AVRO_READ_OR_FREE macro calls avro_free(buf, 0) with a hardcoded size of 0, but the buffer was allocated with size len + 1 (in both read_bytes and read_string). Every other avro_free call in the codebase passes the actual allocated size. The avro_allocator_t interface contract specifies that osize must be the size of the existing buffer; passing 0 violates this contract and will cause incorrect behavior for any custom allocator set via avro_set_allocator that relies on osize for memory tracking or bookkeeping.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 51216c8. Configure here.

Comment on lines +99 to +100

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

avro_free(buf, 0) passes the wrong size — breaks the Avro allocator contract and the project's own size-checking allocator.

Both call sites allocate len + 1 bytes (avro_malloc(*len + 1) / avro_malloc(str_len + 1)), but the macro unconditionally passes 0 as the old-size argument to avro_free. The Avro C allocator API documents that the osize argument must match the allocation size, and the project's own test suite uses a custom allocator that "verifies that the size that we use to free an object matches the size that we use to allocate it" — so this mismatch would be caught there, and would also break any user-supplied pool allocator that relies on osize.

Since both callers allocate exactly read_len + 1 bytes, the simplest correct fix is to pass len + 1 directly inside the macro. Alternatively, add an explicit alloc_size parameter for clarity and future safety.

🐛 Option A – pass len + 1 inside the macro (minimal change)
 `#define` AVRO_READ_OR_FREE(reader, buf, len)  \
-{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, 0); buf = NULL; return rval; } }
+{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, (size_t)(len) + 1); buf = NULL; return rval; } }
🐛 Option B – explicit alloc_size parameter (preferred for clarity)
-#define AVRO_READ_OR_FREE(reader, buf, len)  \
-{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, 0); buf = NULL; return rval; } }
+#define AVRO_READ_OR_FREE(reader, buf, len, alloc_size)  \
+{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, alloc_size); buf = NULL; return rval; } }

Then in encoding_binary.c:

-    AVRO_READ_OR_FREE(reader, *bytes, *len);
+    AVRO_READ_OR_FREE(reader, *bytes, *len, (size_t)*len + 1);
-    AVRO_READ_OR_FREE(reader, *s, str_len);
+    AVRO_READ_OR_FREE(reader, *s, str_len, (size_t)*len);   /* *len == str_len + 1 */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#define AVRO_READ_OR_FREE(reader, buf, len) \
{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, 0); buf = NULL; return rval; } }
`#define` AVRO_READ_OR_FREE(reader, buf, len) \
{ int rval = avro_read( reader, buf, len ); if(rval) { avro_free(buf, (size_t)(len) + 1); buf = NULL; return rval; } }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lang/c/src/encoding.h` around lines 99 - 100, The macro AVRO_READ_OR_FREE
currently calls avro_free(buf, 0) which violates the allocator contract because
callers allocate len + 1 bytes; change the macro to pass the correct old-size
(len + 1) to avro_free so the free matches avro_malloc's allocation size (i.e.,
replace the avro_free(buf, 0) call in AVRO_READ_OR_FREE with avro_free(buf, len
+ 1)); this ensures avro_free and AVRO_READ_OR_FREE honor the allocator/osize
requirement used by avro_malloc and custom allocators.

#define AVRO_SKIP(reader, len) \
{ int rval = avro_skip( reader, len); if (rval) return rval; }

@augmentcode augmentcode Bot May 8, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AVRO_READ_OR_FREE calls avro_free(buf, 0), but the allocator API expects the osize passed to avro_free to match the allocation size; several tests/custom allocators validate this and will abort on mismatch. Since the bytes/string paths allocate an extra NUL byte (len+1), freeing with size 0 can break exactly the failed-read path this macro is meant to clean up.

Severity: high

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.


Expand Down
4 changes: 2 additions & 2 deletions lang/c/src/encoding_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ static int read_bytes(avro_reader_t reader, char **bytes, int64_t * len)
avro_set_error("Cannot allocate buffer for bytes value");
return ENOMEM;
}
AVRO_READ(reader, *bytes, *len);
(*bytes)[*len] = '\0';
AVRO_READ_OR_FREE(reader, *bytes, *len);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The call to AVRO_READ_OR_FREE should provide the actual size of the allocated buffer (*len + 1) to ensure correct memory deallocation if a custom allocator is used.

	AVRO_READ_OR_FREE(reader, *bytes, *len, *len + 1);

return 0;
}

Expand Down Expand Up @@ -194,7 +194,7 @@ static int read_string(avro_reader_t reader, char **s, int64_t *len)
return ENOMEM;
}
(*s)[str_len] = '\0';
AVRO_READ(reader, *s, str_len);
AVRO_READ_OR_FREE(reader, *s, str_len);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The call to AVRO_READ_OR_FREE should provide the actual size of the allocated buffer (*len) to ensure correct memory deallocation if a custom allocator is used.

	AVRO_READ_OR_FREE(reader, *s, str_len, *len);

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions lang/c/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ add_avro_test_checkmem(test_avro_1379)
add_avro_test_checkmem(test_avro_1691)
add_avro_test_checkmem(test_avro_1906)
add_avro_test_checkmem(test_avro_1904)
add_avro_test_checkmem(test_avro_4246)
223 changes: 223 additions & 0 deletions lang/c/tests/test_avro_4246.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
* 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.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <avro.h>

#define STRING_SCHEMA_LITERAL "\"string\""
#define INT_SCHEMA_LITERAL "\"int\""

typedef struct
{
char data[256];
size_t size;
} encoded_buf_t;

static int encode_int(
avro_value_iface_t *iface,
int32_t number,
encoded_buf_t *out_buf)
{
int rc;
avro_value_t value;
avro_writer_t writer = NULL;

memset(out_buf, 0, sizeof(*out_buf));

rc = avro_generic_value_new(iface, &value);
if (rc != 0)
{
fprintf(stderr, "encode_int: avro_generic_value_new failed: %s\n",
avro_strerror());
return rc;
}

rc = avro_value_set_int(&value, number);
if (rc != 0)
{
fprintf(stderr, "encode_int: avro_value_set_int failed: %s\n",
avro_strerror());
avro_value_decref(&value);
return rc;
}

writer = avro_writer_memory(out_buf->data, sizeof(out_buf->data));
if (writer == NULL)
{
fprintf(stderr, "encode_int: avro_writer_memory failed\n");
avro_value_decref(&value);
return -1;
}

rc = avro_value_write(writer, &value);
if (rc != 0)
{
fprintf(stderr, "encode_int: avro_value_write failed: %s\n",
avro_strerror());
avro_writer_free(writer);
avro_value_decref(&value);
return rc;
}

out_buf->size = avro_writer_tell(writer);

avro_writer_free(writer);
avro_value_decref(&value);
return 0;
}

static int decode_as_string(
avro_value_iface_t *string_iface,
const void *buf,
size_t buf_size,
const char *label)
{
int rc;
avro_reader_t reader = NULL;
avro_value_t decoded;
const char *text = NULL;
size_t text_size = 0;

rc = avro_generic_value_new(string_iface, &decoded);
if (rc != 0)
{
fprintf(stderr, "%s: avro_generic_value_new failed: %s\n",
label, avro_strerror());
return rc;
}

reader = avro_reader_memory((const char *)buf, buf_size);
if (reader == NULL)
{
fprintf(stderr, "%s: avro_reader_memory failed\n", label);
avro_value_decref(&decoded);
return -1;
}

rc = avro_value_read(reader, &decoded);
if (rc != 0)
{
fprintf(stderr, "%s: avro_value_read failed as expected? rc=%d err=%s\n",
label, rc, avro_strerror());
avro_reader_free(reader);
avro_value_decref(&decoded);
return rc;
}

rc = avro_value_get_string(&decoded, &text, &text_size);
if (rc != 0)
{
fprintf(stderr, "%s: avro_value_get_string failed: %s\n",
label, avro_strerror());
avro_reader_free(reader);
avro_value_decref(&decoded);
return rc;
}

printf("%s: decoded successfully: \"%s\" (%zu bytes)\n",
label, text, text_size);

avro_reader_free(reader);
avro_value_decref(&decoded);
return 0;
}

int main(void)
{
int rc;
int ret = EXIT_SUCCESS;

avro_schema_t string_schema = NULL;
avro_schema_t int_schema = NULL;

avro_value_iface_t *string_iface = NULL;
avro_value_iface_t *int_iface = NULL;

encoded_buf_t encoded_string;
encoded_buf_t encoded_int;

rc = avro_schema_from_json_literal(STRING_SCHEMA_LITERAL, &string_schema);
if (rc != 0)
{
fprintf(stderr, "Failed to parse string schema: %s\n", avro_strerror());
return EXIT_FAILURE;
}

rc = avro_schema_from_json_literal(INT_SCHEMA_LITERAL, &int_schema);
if (rc != 0)
{
fprintf(stderr, "Failed to parse int schema: %s\n", avro_strerror());
avro_schema_decref(string_schema);
return EXIT_FAILURE;
}

string_iface = avro_generic_class_from_schema(string_schema);
if (string_iface == NULL)
{
fprintf(stderr, "Failed to create string iface\n");
avro_schema_decref(int_schema);
avro_schema_decref(string_schema);
return EXIT_FAILURE;
}

int_iface = avro_generic_class_from_schema(int_schema);
if (int_iface == NULL)
{
fprintf(stderr, "Failed to create int iface\n");
avro_value_iface_decref(string_iface);
avro_schema_decref(int_schema);
avro_schema_decref(string_schema);
return EXIT_FAILURE;
}

rc = encode_int(int_iface, 12345, &encoded_int);
if (rc != 0)
{
fprintf(stderr, "Encoding int failed\n");
avro_value_iface_decref(int_iface);
avro_value_iface_decref(string_iface);
avro_schema_decref(int_schema);
avro_schema_decref(string_schema);
return EXIT_FAILURE;
}

printf("Encoded int size: %zu bytes\n", encoded_int.size);

rc = decode_as_string(string_iface,
encoded_int.data,
encoded_int.size,
"int payload as string");
if (rc == 0)
{
fprintf(stderr, "Unexpected success decoding int payload as string\n");
ret = EXIT_FAILURE;
}
else
{
printf("int payload as string: failed cleanly, as expected\n");
}

avro_value_iface_decref(int_iface);
avro_value_iface_decref(string_iface);
avro_schema_decref(int_schema);
avro_schema_decref(string_schema);

return ret;
}
Loading