Re: ECPG: NULL pointer dereference in EXECUTE with a NULL parameter

From: Ishjaap Singh <ishjaap(dot)singh07(at)gmail(dot)com>
To: Никита Лосев <nktlsv05(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: ECPG: NULL pointer dereference in EXECUTE with a NULL parameter
Date: 2026-11-18 17:39:03
Message-ID: CA+Y2BmfP8hkBFEK2awoAp0uobYXHmQC-C9iAZ7qs2OPfVtEQ5w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Nikita,

Thanks for the report. I ran your program and it does crash, as you
expected: on master it dies with a segfault in strlen(), called from
insert_tobeinserted(), before the EXECUTE is sent to the server. With
the indicator set to 0 instead of -1 it runs fine.

The same code handles any EXECUTE name(:var, ...), including after
PREPARE name FROM, not just PREPARE ... AS. A NULL bytea value can crash
too, even earlier: reading the code, convert_bytea_to_string() reads
from the null pointer. I saw that crash on master with a separate
program.

The attached patch does what you suggested. In the
ECPGst_exec_with_exprlist branch of ecpg_build_params(), a NULL value is
replaced with the keyword NULL (allocated, since insert_tobeinserted()
frees its argument), before the bytea conversion. It also adds a NULL
case to the existing prepareas test.

Tested on master, REL_19, REL_17 and REL_15: your program now stores the
NULL, the ECPG suite passes, and the new test fails without the fix. The
back-branch file also applies cleanly to REL_16 and REL_18; I didn't
build those.

There are two files because ecpg_strdup() takes a third argument from
19 on:

- v1-0001-ecpg-Fix-crash-on-NULL-in-EXECUTE-master.patch: master and REL_19
- v1-0001-ecpg-Fix-crash-on-NULL-in-EXECUTE-REL_15-18.patch.txt: REL_15 to
REL_18 (named .txt so cfbot only tests the master one)

Regards,
Ishjaap Singh

Attachment Content-Type Size
v1-0001-ecpg-Fix-crash-on-NULL-in-EXECUTE-REL_15-18.patch.txt text/plain 29.0 KB
v1-0001-ecpg-Fix-crash-on-NULL-in-EXECUTE-master.patch application/octet-stream 29.0 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Tender Wang 2026-11-18 12:30:20 Re: BUG #19912: MERGE NOT MATCHED BY SOURCE sees stale source values after concurrent update (EPQ)