Thread: Possible pointer var TupleDesc rettupdesc used not initialized (src/backend/optimizer/util/clauses.c)

Hi,

Possible pointer TupleDesc rettupdesc used not initialized?

if (!isNull) at line 4346 taking a true branch, the function check_sql_fn_retval at line 4448 can use rettupdesc uninitialized.

regards,
Ranier Vilela

> On May 24, 2021, at 5:37 PM, Ranier Vilela <ranier.vf@gmail.com> wrote:
>
> Hi,
>
> Possible pointer TupleDesc rettupdesc used not initialized?
>
> if (!isNull) at line 4346 taking a true branch, the function check_sql_fn_retval at line 4448 can use rettupdesc
uninitialized.

Care to submit a patch?

—
Mark Dilger
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company






Em seg., 24 de mai. de 2021 às 22:42, Mark Dilger <mark.dilger@enterprisedb.com> escreveu:


> On May 24, 2021, at 5:37 PM, Ranier Vilela <ranier.vf@gmail.com> wrote:
>
> Hi,
>
> Possible pointer TupleDesc rettupdesc used not initialized?
>
> if (!isNull) at line 4346 taking a true branch, the function check_sql_fn_retval at line 4448 can use rettupdesc uninitialized.

Care to submit a patch?
Hi Mark, sorry but not.
I examined the code and I can't say what the correct value is for rettupdesc.

regards,
Ranier Vilela

Livre de vírus. www.avast.com.


On Mon, May 24, 2021 at 7:21 PM Ranier Vilela <ranier.vf@gmail.com> wrote:
Em seg., 24 de mai. de 2021 às 22:42, Mark Dilger <mark.dilger@enterprisedb.com> escreveu:


> On May 24, 2021, at 5:37 PM, Ranier Vilela <ranier.vf@gmail.com> wrote:
>
> Hi,
>
> Possible pointer TupleDesc rettupdesc used not initialized?
>
> if (!isNull) at line 4346 taking a true branch, the function check_sql_fn_retval at line 4448 can use rettupdesc uninitialized.

Care to submit a patch?
Hi Mark, sorry but not.
I examined the code and I can't say what the correct value is for rettupdesc.

Hi,
It seems the following call would fill up value for rettupdesc :

functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc); 

Cheers

regards,
Ranier Vilela

Livre de vírus. www.avast.com.

Em seg., 24 de mai. de 2021 às 23:35, Zhihong Yu <zyu@yugabyte.com> escreveu:


On Mon, May 24, 2021 at 7:21 PM Ranier Vilela <ranier.vf@gmail.com> wrote:
Em seg., 24 de mai. de 2021 às 22:42, Mark Dilger <mark.dilger@enterprisedb.com> escreveu:


> On May 24, 2021, at 5:37 PM, Ranier Vilela <ranier.vf@gmail.com> wrote:
>
> Hi,
>
> Possible pointer TupleDesc rettupdesc used not initialized?
>
> if (!isNull) at line 4346 taking a true branch, the function check_sql_fn_retval at line 4448 can use rettupdesc uninitialized.

Care to submit a patch?
Hi Mark, sorry but not.
I examined the code and I can't say what the correct value is for rettupdesc.

Hi,
It seems the following call would fill up value for rettupdesc :

functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc); 
In short, do you suggest running half the else?
To do this, you need to fill fexpr correctly.
It will not always be a trivial solution.
 
regards,
Ranier Vilela


Ranier Vilela <ranier.vf@gmail.com> writes:
> Possible pointer TupleDesc rettupdesc used not initialized?
> if (!isNull) at line 4346 taking a true branch, the function
> check_sql_fn_retval at line 4448 can use rettupdesc uninitialized.

This seems to have been introduced by the SQL-function-body patch.

After some study, I concluded that the reason we haven't noticed
is that the case is nearly unreachable: check_sql_fn_retval never
consults the rettupdesc unless the function result type is composite
and the tlist length is more than one --- and we eliminated the latter
case earlier in inline_function.

There is an exception, namely if the single tlist item fails to
be coercible to the output type, but that's hard to get to given
that it'd have been checked while defining the SQL-body function.
I did manage to reproduce a problem after turning off
check_function_bodies so I could create a broken function.

In any case, inline_function has no business assuming that
check_sql_fn_retval doesn't need a valid value.

The simplest way to fix this seems to be to move the code that
creates "fexpr" and calls get_expr_result_type, so that we always
do that even for SQL-body cases.  We could alternatively use some
other way to obtain a result tupdesc in the SQL-body path; but
creating the dummy FuncExpr node is cheap enough that I don't
think it's worth contortions to avoid doing it.

            regards, tom lane



Em ter., 25 de mai. de 2021 às 13:09, Tom Lane <tgl@sss.pgh.pa.us> escreveu:
Ranier Vilela <ranier.vf@gmail.com> writes:
> Possible pointer TupleDesc rettupdesc used not initialized?
> if (!isNull) at line 4346 taking a true branch, the function
> check_sql_fn_retval at line 4448 can use rettupdesc uninitialized.

This seems to have been introduced by the SQL-function-body patch.

After some study, I concluded that the reason we haven't noticed
is that the case is nearly unreachable: check_sql_fn_retval never
consults the rettupdesc unless the function result type is composite
and the tlist length is more than one --- and we eliminated the latter
case earlier in inline_function.

There is an exception, namely if the single tlist item fails to
be coercible to the output type, but that's hard to get to given
that it'd have been checked while defining the SQL-body function.
I did manage to reproduce a problem after turning off
check_function_bodies so I could create a broken function.

In any case, inline_function has no business assuming that
check_sql_fn_retval doesn't need a valid value.

The simplest way to fix this seems to be to move the code that
creates "fexpr" and calls get_expr_result_type, so that we always
do that even for SQL-body cases.  We could alternatively use some
other way to obtain a result tupdesc in the SQL-body path; but
creating the dummy FuncExpr node is cheap enough that I don't
think it's worth contortions to avoid doing it.
Following the guidelines, I provided a patch.
But I did more than requested, removed redundant variable and reduced the scope of two.

vcregress check pass fine.

regards,
Ranier Vilela
Attachment
Ranier Vilela <ranier.vf@gmail.com> writes:
> Following the guidelines, I provided a patch.

Oh, I already pushed a fix, thanks.

            regards, tom lane



Em ter., 25 de mai. de 2021 às 14:35, Tom Lane <tgl@sss.pgh.pa.us> escreveu:
Ranier Vilela <ranier.vf@gmail.com> writes:
> Following the guidelines, I provided a patch.

Oh, I already pushed a fix, thanks.
No problem!

Thank you.

regards,
Ranier Vilela