| From: | Никита Лосев <nktlsv05(at)gmail(dot)com> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | ECPG: NULL pointer dereference in EXECUTE with a NULL parameter |
| Date: | 2026-10-20 09:36:39 |
| Message-ID: | CABSGbiLbvjPMdruq=nMTmKotqKpozdD-Xeyx1JqQS=wUvomfsA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
Hello,
While reviewing a static analysis warning (DEREF_OF_NULL.EX) in PostgreSQL
17.0, I found a path in src/interfaces/ecpg/ecpglib/execute.c where a NULL
parameter is passed to strlen().
In ecpg_store_input(), a negative indicator sets *tobeinserted_p to NULL
(line 531 for a short indicator). The function returns true, since this is
a valid SQL NULL value.
The caller, ecpg_build_params(), checks only the return value at line 1395.
For ECPGst_exec_with_exprlist with a nonbinary parameter, execution then
reaches the call to insert_tobeinserted() at line 1489 with tobeinserted ==
NULL. That function calls strlen(tobeinserted) at line 1132 without
checking the pointer.
This appears to allow a valid NULL input parameter to crash the ECPG client
while constructing the command.
I have checked the execution path in the 17.0 sources, but have not
reproduced the failure at runtime or checked newer releases. The test case
and build commands below are untested.
The following program is intended to exercise this path. It connects to the
postgres database and requires permission to create a temporary table. Save
it as ecpg_null_exec.pgc:
#include <stdio.h>
#include <stdlib.h>
EXEC SQL INCLUDE sqlca;
int
main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
int value = 0;
short ind = -1;
int null_count = 0;
EXEC SQL END DECLARE SECTION;
EXEC SQL WHENEVER SQLERROR STOP;
EXEC SQL CONNECT TO postgres;
EXEC SQL CREATE TEMP TABLE ecpg_null_exec_test (v integer);
EXEC SQL PREPARE null_exec_stmt(integer) AS
INSERT INTO ecpg_null_exec_test (v) VALUES ($1);
EXEC SQL EXECUTE null_exec_stmt(:value :ind);
EXEC SQL SELECT count(*) INTO :null_count
FROM ecpg_null_exec_test
WHERE v IS NULL;
printf("null_count=%d\n", null_count);
EXEC SQL DISCONNECT ALL;
return null_count == 1 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Example commands for Linux, with ecpg and pg_config from the same
PostgreSQL installation:
ecpg -o ecpg_null_exec.c ecpg_null_exec.pgc
cc -g -O0 \
-I"$(pg_config --includedir)" \
ecpg_null_exec.c \
-L"$(pg_config --libdir)" \
-Wl,-rpath,"$(pg_config --libdir)" \
-lecpg \
-o ecpg_null_exec
./ecpg_null_exec
Connection settings and authentication need to be configured for the local
environment.
The expected result is one row containing SQL NULL, followed by null_count=1
and a successful exit. Based on the source, the EXECUTE statement instead
reaches strlen(NULL) before being sent to the server. I do not have a
runtime backtrace.
The expression-list substitution code should handle SQL NULL explicitly,
for example by inserting the unquoted keyword NULL. Any replacement string
would need to be allocated, since insert_tobeinserted() frees the buffer
passed to it. The example above could also serve as the basis for a
regression test.
Thank you.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Hayato Kuroda (Fujitsu) | 2026-10-20 11:29:38 | RE: START_REPLICATION silently truncates an overlong LSN component |
| Previous Message | Fujii Masao | 2026-10-20 09:31:18 | Re: START_REPLICATION silently truncates an overlong LSN component |