Re: Missing NULL check after calling ecpg_strdup

From: Aleksander Alekseev <aleksander(at)tigerdata(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Evgeniy Gorbanev <gorbanyoves(at)basealt(dot)ru>
Subject: Re: Missing NULL check after calling ecpg_strdup
Date: 2025-07-22 13:20:53
Message-ID: CAJ7c6TNX+sL6YLdOVcaHhh_aXQzC3ggfw8vcygJhrNbSzcNEig@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

> I have spent some time rechecking the whole code, and I have
> backpatched this part. [...]

Many thanks!

> Hmm.. Aren't you missing a va_end(args) in the early exit you are
> adding here?

I do, and it's rather stupid of me. Thanks.

> [...]
> At the end, I finish with the attached, where alloc_failed matters for
> the failure checks with repeated calls of strdup() in ECPGconnect()
> and also the setlocale() case. This is for HEAD due to how unlikely
> these issues would occur in practice.

v7 may have a compilation warning on Linux:

```
warning: unused variable ‘alloc_failed’ [-Wunused-variable]
```

... because the only use of the variable is hidden under #ifdef's.

Fixed in v8:

```
--- a/src/interfaces/ecpg/ecpglib/descriptor.c
+++ b/src/interfaces/ecpg/ecpglib/descriptor.c
@@ -240,9 +240,9 @@ ECPGget_desc(int lineno, const char *desc_name,
int index,...)
act_tuple;
struct variable data_var;
struct sqlca_t *sqlca = ECPGget_sqlca();
- bool alloc_failed = false;
+ bool alloc_failed = (sqlca == NULL);

- if (sqlca == NULL)
+ if (alloc_failed)
{
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
```

This code is also more consistent with what we ended up having in connect.c.

Other than that the patch looks OK to me.

Attachment Content-Type Size
v8-0001-ecpg-Improve-error-detection-of-ecpg_strdup.patch text/x-patch 15.2 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrei Lepikhov 2025-07-22 13:44:11 Re: track generic and custom plans in pg_stat_statements
Previous Message Fujii Masao 2025-07-22 13:13:31 Re: Log prefix missing for subscriber log messages received from publisher