Re: Avoid mix char with bool type in comparisons

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Avoid mix char with bool type in comparisons
Date: 2022-10-08 01:50:13
Message-ID: CAEudQArFXKtgt5Kv8j-D34zTLCGNuNZ6wdPbRaS0nS5Qom6wnA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Em sex., 7 de out. de 2022 às 15:45, Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
escreveu:

> Em sex., 7 de out. de 2022 às 14:44, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
> escreveu:
>
>> Ranier Vilela <ranier(dot)vf(at)gmail(dot)com> writes:
>> > Anyway, it's not *true* that collectMatchesForHeapRow deals
>> > only "true" and "false", once that key->entryRes is initialized with
>> > GIN_FALSE not false.
>>
>> Look: GinTernaryValue is carefully designed so that it is
>> representation-compatible with bool, including that GIN_FALSE is
>> identical to false and GIN_TRUE is identical to true. I'm quite
>> uninterested in debating whether that's a good idea or not.
>> Moreover, there are a ton of other places besides this one where
>> the GIN code relies on that equivalence, so we'd have to do a
>> lot more than what's in this patch if we wanted to decouple that.
>>
> Just now I checked all the places where GinTernaryValue is used and they
> all trust using GIN_TRUE, GIN_FALSE and GIN_MAYBE.
> The only problematic place I found was at ginget.c and jsonb_gin.c
>
> jsonb_gin.c:
> The function execute_jsp_gin_node returns GIN_TRUE if the node->type is
> JSP_GIN_ENTRY and the value is GIN_MAYBE.
> IMO, it should be GIN_FALSE?
>
I spend more time checking this and I think that answer is not.

So, in the patch attached it changes only ginget.c, to avoid mixing
GinTernaryValue with bool.
And add comments to warn that GIN_MAYBE comparison is equal GIN_TRUE in
jsonb_gin.c

I think that's all that needs to be changed at the moment.
I'm also glad there are no bugs, and IMO I hope the code has become more
readable and secure.

regards,
Ranier Vilela

Attachment Content-Type Size
v1-avoid_mix_bool_with_char_comparator.patch application/octet-stream 3.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ajay P S 2022-10-08 02:26:21 Difference between HeapTupleData and TupleTableSlot structures
Previous Message Tom Lane 2022-10-08 00:49:18 Re: Non-robustness in pmsignal.c