Re: Avoid overflow with simplehash - Mailing list pgsql-hackers

From Ranier Vilela
Subject Re: Avoid overflow with simplehash
Date
Msg-id CAEudQApjsFoXMVONZjpiW5ZxVwg+uNg2vNV-w8F0cEHnRBUm5w@mail.gmail.com
Whole thread Raw
In response to Re: Avoid overflow with simplehash  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Avoid overflow with simplehash
List pgsql-hackers
Em qui., 6 de jul. de 2023 às 12:16, Tom Lane <tgl@sss.pgh.pa.us> escreveu:
Ranier Vilela <ranier.vf@gmail.com> writes:
> See the comments:
> "Search for the first empty element."
> If the empty element is not found, startelem has PG_UINT64_MAX value,
> which do not fit in uint32.

Hi Tom,
I think the point of that assertion is exactly that we're required to
have an empty element (because max fillfactor is less than 1),
so the search should have succeeded.

It does seem like we could do

        uint64          startelem = SH_MAX_SIZE;

        ...

        Assert(startelem < SH_MAX_SIZE);

which'd make it a little clearer that the expectation is for
startelem to have changed value.
I still have doubts about this.

see:
#include <iostream>
#include <string>
#include <limits.h>

#define SH_MAX_SIZE1 (((unsigned long long) 0xFFFFFFFFU) + 1)
#define SH_MAX_SIZE2 (((unsigned long long) 0xFFFFFFFFU) - 1)

int main()
{
    unsigned long long max_size1 = SH_MAX_SIZE1;
    unsigned long long max_size2 = SH_MAX_SIZE2;
    unsigned int cur1 = SH_MAX_SIZE1;
    unsigned int cur2 = SH_MAX_SIZE2;

    printf("SH_MAX_SIZE1=%llu\n", max_size1);
    printf("SH_MAX_SIZE2=%llu\n", max_size2);
    printf("cur1=%u\n", cur1);
    printf("cur2=%u\n", cur2);
}
warning: implicit conversion from 'unsigned long long' to 'unsigned int' changes value from 4294967296 to 0 [-Wconstant-conversion]

outputs:
SH_MAX_SIZE1=4294967296
SH_MAX_SIZE2=4294967294
cur1=0
cur2=4294967294

And in the comments we have:
"Iterate backwards, that allows the current element to be deleted, even
* if there are backward shifts"

So if an empty element is not found and the *cur* field is set to 0 (SH_MAX_SIZE -> uint32),
then will it iterate forwards?

  And I agree that declaring "i"
as int is wrong.
Thanks for the confirmation.

regards,
Ranier Vilela

pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: UUID v7
Next
From: Peter Eisentraut
Date:
Subject: Re: Add more sanity checks around callers of changeDependencyFor()