Re: Fix out-of-bounds in the function PQescapeinternal (src/interfaces/libpq/fe-exec.c)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix out-of-bounds in the function PQescapeinternal (src/interfaces/libpq/fe-exec.c)
Date: 2024-04-02 21:13:20
Message-ID: 1836134.1712092400@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Ranier Vilela <ranier(dot)vf(at)gmail(dot)com> writes:
> While I working in [1], Coverity reported some errors:
> src/bin/pg_basebackup/pg_createsubscriber.c
> CID 1542690: (#1 of 2): Out-of-bounds access (OVERRUN)
> alloc_strlen: Allocating insufficient memory for the terminating null of
> the string. [Note: The source code implementation of the function has been
> overridden by a builtin model.]
> CID 1542690: (#2 of 2): Out-of-bounds access (OVERRUN)
> alloc_strlen: Allocating insufficient memory for the terminating null of
> the string. [Note: The source code implementation of the function has been
> overridden by a builtin model.]

Yeah, we saw that in the community run too. I'm tempted to call it
an AI hallucination. The "Note" seems to mean that they're not
actually analyzing our code but some made-up substitute.

> The source of errors is the function PQescapeInternal.
> The slow path has bugs when num_quotes or num_backslashes are greater than
> zero.
> For each num_quotes or num_backslahes we need to allocate two more.

Nonsense. The quote or backslash is already counted in input_len,
so we need to add just one more.

If there were anything wrong here, I'm quite sure our testing under
e.g. valgrind would have caught it years ago. However, just to be
sure, I tried adding an Assert that the allocated space is filled
exactly, as attached. It gets through check-world just fine.

regards, tom lane

Attachment Content-Type Size
assert-PQescapeInternal-isnt-broken.patch text/x-diff 443 bytes

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Davis 2024-04-02 21:13:57 Re: Statistics Import and Export
Previous Message Andrew Dunstan 2024-04-02 21:12:51 Re: WIP Incremental JSON Parser