Skip to content

fix: crash parsing array with unterminated quoted element - #1860

Open
Keerthana-64 wants to merge 1 commit into
psycopg:masterfrom
Keerthana-64:array-unterminated-quoted-element
Open

Keerthana-64 wants to merge 1 commit into
psycopg:masterfrom
Keerthana-64:array-unterminated-quoted-element

Conversation

@Keerthana-64

Copy link
Copy Markdown

typecast_array_tokenize derives an element's length by subtracting the two surrounding quote characters, so an element that opens a quote but never closes it (a malformed array literal such as '{"}') has only one quote and l goes negative. that negative length is handed to the element typecasters through typecast_cast and used as a copy or allocation size: typecast_LONGINTEGER_cast runs strncpy(buffer, s, (size_t)len) with len == -1, and the float and binary casters pass it to PyUnicode_FromStringAndSize and PyMem_Malloc, so an int, bigint, numeric, float or bytea array column crashes the client with an out-of-bounds access. a well-behaved server never emits this, but a hostile or compromised one can, the same threat model as the recent malformed int64 and bytea fixes. the negative length is the tokenizer's own bug, so reject the unterminated element there with a DataError, the way the scanner already rejects unbalanced braces and other malformed arrays; that keeps the single fix at the root and covers every element type. the added test feeds the malformed literal to the int, bigint, numeric, float and string array casters, each of which segfaulted before the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant