Re: Improper use about DatumGetInt32

From: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
To: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Cc: Ashutosh Bapat <ashutosh(dot)bapat(at)2ndquadrant(dot)com>, Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, "Hou, Zhijie" <houzj(dot)fnst(at)cn(dot)fujitsu(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Improper use about DatumGetInt32
Date: 2020-11-26 12:43:12
Message-ID: b69e2445-6eac-2b37-7769-35828692e2d7@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2020-11-25 20:04, Alvaro Herrera wrote:
> On 2020-Nov-25, Peter Eisentraut wrote:
>
>> bt_page_stats(PG_FUNCTION_ARGS)
>> {
>> text *relname = PG_GETARG_TEXT_PP(0);
>> - uint32 blkno = PG_GETARG_UINT32(1);
>> + int64 blkno = PG_GETARG_INT64(1);
>
> As a matter of style, I think it'd be better to have an int64 variable
> that gets the value from PG_GETARG_INT64(), then you cast that to
> another variable that's a BlockNumber and use that throughout the rest
> of the code. So you'd avoid changes like this:
>
>> static bytea *get_raw_page_internal(text *relname, ForkNumber forknum,
>> - BlockNumber blkno);
>> + int64 blkno);
>
> where the previous coding was correct, and the new one is dubious and it
> forces you to add unnecessary range checks in that function:
>
>> @@ -144,11 +144,16 @@ get_raw_page_internal(text *relname, ForkNumber forknum, BlockNumber blkno)
>> (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
>> errmsg("cannot access temporary tables of other sessions")));
>>
>> + if (blkno < 0 || blkno > MaxBlockNumber)
>> + ereport(ERROR,
>> + (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
>> + errmsg("invalid block number")));
>> +

The point of the patch is to have the range check somewhere. If you
just cast it, then you won't notice out of range arguments. Note that
other contrib modules that take block numbers work the same way.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Euler Taveira 2020-11-26 13:14:37 Re: cleanup temporary files after crash
Previous Message Krunal Bauskar 2020-11-26 12:40:31 Re: Improving spin-lock implementation on ARM.