ecpg: Improve error detection around ecpg_strdup()
authorMichael Paquier <michael@paquier.xyz>
Tue, 22 Jul 2025 23:18:36 +0000 (08:18 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 22 Jul 2025 23:18:36 +0000 (08:18 +0900)
commita6eabec6808cb1b8f20974ad57275b14fc079e3b
treedd618fe3405f4a3075d79eecceeae074dbce33de
parenta7ca73af662bc95e14058ac3f8fcf5d257f8bf79
ecpg: Improve error detection around ecpg_strdup()

Various code paths of the ECPG code did not check for memory allocation
failures, including the specific case where ecpg_strdup() considers a
NULL value given in input as a valid behavior.  strdup() returning
itself NULL on failure, there was no way to make the difference between
what could be valid and what should fail.

With the different cases in mind, ecpg_strdup() is redesigned and gains
a new optional argument, giving its callers the possibility to
differentiate allocation failures and valid cases where the caller is
giving a NULL value in input.  Most of the ECPG code does not expect a
NULL value, at the exception of ECPGget_desc() (setlocale) and
ECPGconnect(), like dbname being unspecified, with repeated strdup
calls.

The code is adapted to work with this new routine.  Note the case of
ecpg_auto_prepare(), where the code order is switched so as we handle
failures with ecpg_strdup() before manipulating any cached data,
avoiding inconsistencies.

This class of failure is unlikely a problem in practice, so no backpatch
is done.  Random OOM failures in ECPGconnect() could cause the driver to
connect to a different server than the one wanted by the caller, because
it could fallback to default values instead of the parameters defined
depending on the combinations of allocation failures and successes.

Author: Evgeniy Gorbanev <gorbanyoves@basealt.ru>
Co-authored-by: Aleksander Alekseev <aleksander@tigerdata.com>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/a6b193c1-6994-4d9c-9059-aca4aaf41ddd@basealt.ru
src/interfaces/ecpg/ecpglib/connect.c
src/interfaces/ecpg/ecpglib/descriptor.c
src/interfaces/ecpg/ecpglib/ecpglib_extern.h
src/interfaces/ecpg/ecpglib/execute.c
src/interfaces/ecpg/ecpglib/memory.c
src/interfaces/ecpg/ecpglib/prepare.c