Re: Avoid unncessary always true test (src/backend/storage/buffer/bufmgr.c)

From: Gurjeet Singh <gurjeet(at)singh(dot)im>
To: Karina Litskevich <litskevichkarina(at)gmail(dot)com>
Cc: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>, Richard Guo <guofenglinux(at)gmail(dot)com>, Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>, michael(at)paquier(dot)xyz, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Avoid unncessary always true test (src/backend/storage/buffer/bufmgr.c)
Date: 2023-07-06 19:06:20
Message-ID: CABwTF4WO7jo6xRX9z1LO5Ktpr6NwNxDCXorJX3AURBceELLkbw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Jul 6, 2023 at 8:01 AM Karina Litskevich
<litskevichkarina(at)gmail(dot)com> wrote:
>
>
>> EB_SMGR and EB_REL are macros for making new structs.
>> IMO these are buggy, once make new structs without initializing all fields.
>> Attached a patch to fix this and make more clear when rel or smgr is NULL.
>
>
> As long as a structure is initialized, its fields that are not present in
> initialization are initialized to zeros and NULLs depending on their types.
> See C99 Standard 6.7.8.21 and 6.7.8.10. This behaviour is quite well known,
> so I don't think this place is buggy. Anyway, if someone else says the code
> is more readable with these fields initialized explicitly, then go on.

Even though I am not a fan of the Designated Initializers feature, I
agree with Karina. Per the standard, the unmentioned fields get
initialized to zeroes/NULLs, so the explicit initialization to
zero/null that this additional patch does is unnecessary. Moreover, I
feel that it makes the code less pleasant to read.

C99, 6.7.8.21:
> If there are fewer initializers in a brace-enclosed list than there are
> elements or members of an aggregate, or fewer characters in a string literal
> used to initialize an array of known size than there are elements in the array,
> the remainder of the aggregate shall be initialized implicitly the same as
> objects that have static storage duration.

C99, 6.7.8.10:
> If an object that has automatic storage duration is not initialized explicitly,
> its value is indeterminate. If an object that has static storage duration is
> not initialized explicitly, then:
> - if it has pointer type, it is initialized to a null pointer;
> - if it has arithmetic type, it is initialized to (positive or unsigned) zero;
> - if it is an aggregate, every member is initialized (recursively) according to these rules;
> - if it is a union, the first named member is initialized (recursively) according to these rules.

Best regards,
Gurjeet
http://Gurje.et

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2023-07-06 19:15:26 Re: Fix last unitialized memory warning
Previous Message Andres Freund 2023-07-06 19:01:15 Re: [PATCH] Add GitLab CI to PostgreSQL