Re: Unlikely-to-happen crash in ecpg driver caused by NULL-pointer check not done

From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>, PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Unlikely-to-happen crash in ecpg driver caused by NULL-pointer check not done
Date: 2015-02-03 10:50:23
Message-ID: 54D0A7EF.6080102@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 02/03/2015 09:28 AM, Michael Paquier wrote:
> Hi all,
>
> In ecpg_add_mem of memory.c, we use ecpg_alloc but there is actually
> no NULL-pointer check. If an OOM shows up exactly at this point, this
> is likely to cause a crash. Attached patch adds some extra processing
> to ecpg_add_mem to check if the allocation fails, and to fail properly
> if an OOM appears.

> --- a/src/interfaces/ecpg/ecpglib/descriptor.c
> +++ b/src/interfaces/ecpg/ecpglib/descriptor.c
> @@ -440,7 +440,12 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
> return false;
> }
> *(void **) var = mem;
> - ecpg_add_mem(mem, lineno);
> + if (!ecpg_add_mem(mem, lineno))
> + {
> + ecpg_free(mem);
> + va_end(args);
> + return false;
> + }
> var = mem;
> }

Hmm. Since the ecpg_add_mem call is done after setting (*(void **) var),
that's left to point to already-free'd memory. The other call sites have
a similar issue. I haven't analyzed the code to check if that's harmless
or not, but seems better to not do that.

In ecpg_add_mem, the ecpg_raise() call is unnecessary, since ecpg_alloc
already does that on failure.

(It would be less error-prone to have an ecpg_alloc_auto() function that
combines ecpg_alloc+ecpg_add_mem in one call.)

> /* Here are some methods used by the lib. */
>
> /* Returns a pointer to a string containing a simple type name. */
> bool ecpg_add_mem(void *ptr, int lineno);
>
> bool ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
> enum ECPGttype, char *, char *, long, long, long,
> enum ARRAY_TYPE, enum COMPAT_MODE, bool);

That second comment is completely bogus. It's not this patch's fault,
it's been like that forever, but just happened to notice..

- Heikki

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Sawada Masahiko 2015-02-03 11:09:50 Re: Proposal : REINDEX xxx VERBOSE
Previous Message Heikki Linnakangas 2015-02-03 10:05:34 Re: INSERT ... ON CONFLICT {UPDATE | IGNORE} 2.0