Skip to content

AVRO-4344: [C] Reject out-of-range enum ordinal instead of returning an uninitialized pointer - #3948

Open
iemejia wants to merge 1 commit into
apache:mainfrom
iemejia:AVRO-4344-c-enum-ordinal
Open

AVRO-4344: [C] Reject out-of-range enum ordinal instead of returning an uninitialized pointer#3948
iemejia wants to merge 1 commit into
apache:mainfrom
iemejia:AVRO-4344-c-enum-ordinal

Conversation

@iemejia

@iemejia iemejia commented Aug 22, 2026

Copy link
Copy Markdown
Member

What

avro_schema_enum_get() (lang/c/src/schema.c) called st_lookup() but ignored its return value:

st_lookup(avro_schema_to_enum(enump)->symbols, index, &val.data);
return val.sym;   /* uninitialized when st_lookup() fails */

For an index outside the schema's symbol range, st_lookup() leaves val.data untouched, so the function returned an uninitialized stack value as a char *.

The binary reader (lang/c/src/value-read.c, AVRO_ENUM case) also passed the enum ordinal read from the input straight through without a range check, so a malformed or truncated container file could drive that path. A caller that then uses the returned symbol name (e.g. avropipe calling strlen()) reads through an indeterminate pointer and typically crashes with SIGSEGV on a default Release build.

Fix

  • value-read.c — validate the decoded ordinal against avro_schema_enum_number_of_symbols() and return EINVAL for an out-of-range value.
  • schema.cavro_schema_enum_get() returns NULL when st_lookup() fails, so it can never return an uninitialized pointer even if called directly.

Tests

Adds test_avro_4344 (self-contained, no data file): a valid ordinal reads successfully; an out-of-range positive ordinal, a negative ordinal, and avro_schema_enum_get(schema, 99) are all handled cleanly (error / NULL). Verified the test fails without the fix and passes with it; the full lang/c suite (28 tests) passes.

JIRA: https://issues.apache.org/jira/browse/AVRO-4344

Reported by Mahdi Alhakim.

…an uninitialized pointer

avro_schema_enum_get() called st_lookup() but ignored its return value, so
for an index outside the schema's symbol range it returned an uninitialized
stack value as a char *. The binary reader also passed the enum ordinal read
from the input straight through without a range check, so a malformed or
truncated container file could drive that path and a caller using the
returned symbol name (e.g. avropipe calling strlen) would read through an
indeterminate pointer and typically crash.

- value-read.c: validate the decoded enum ordinal against the number of
  symbols and return EINVAL for an out-of-range value.
- schema.c: avro_schema_enum_get() now returns NULL when st_lookup() fails.

Adds test_avro_4344 covering a valid ordinal, an out-of-range positive
ordinal, a negative ordinal, and avro_schema_enum_get() returning NULL.

Reported by Mahdi Alhakim.
@github-actions github-actions Bot added the C label Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant