agtype: fix substring() backend crash when start-offset is SQL NULL and length is supplied#2401
Merged
MuhammadTahaNaveed merged 1 commit intoApr 20, 2026
Conversation
…nd length is supplied
age_substring() reads the null map produced by extract_variadic_args()
and rejects null offset/length with this guard:
if ((nargs == 2 && nulls[1]) ||
(nargs == 3 && nulls[2]))
{
ereport(ERROR, ..., errmsg("substring() offset or length cannot be null"));
}
The condition only checks nulls[1] in the 2-argument form. When the
caller passes `substring(str, null, len)` the function takes nargs = 3,
nulls[1] = true, but the guard above does not fire. Execution reaches
the numeric-parameter loop below, which reads args[1] through
DatumGetInt32 / DATUM_GET_AGTYPE_P without ever re-checking nulls[i].
The Datum in that slot is undefined, the dereference segfaults, and
the PostgreSQL backend terminates - not a query error but a
connection-level crash (apache#2386).
Widen the guard to nargs >= 2 && nulls[1] so it catches start-is-null
in both the 2-arg and 3-arg forms. nulls[2] is still only checked
when nargs == 3. No behaviour change on any non-null path; the
connection-crash case is now reported as a normal query error,
matching the intent the existing error message already implies.
Fixes apache#2386
MuhammadTahaNaveed
approved these changes
Apr 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2386.
age_substring()reads the null map produced byextract_variadic_args()and rejects null offset/length with this guard:The condition only checks
nulls[1]in the 2-argument form. When the caller passessubstring(str, null, len)the function takesnargs = 3,nulls[1] = true, but the guard above does not fire. Execution reaches the numeric-parameter loop below, which readsargs[1]throughDatumGetInt32/DATUM_GET_AGTYPE_Pwithout ever re-checkingnulls[i]. The Datum in that slot is undefined, the dereference segfaults, and the PostgreSQL backend terminates — not a query error but a connection-level crash.This widens the guard to
nargs >= 2 && nulls[1]so it catches start-is-null in both the 2-arg and 3-arg forms.nulls[2]is still only checked whennargs == 3. No behaviour change on any non-null path; the connection-crash case is now reported as a normal query error, matching the intent the existing error message already implies.