Re: Should we use MemSet or {0} for struct initialization?

From: Jelte Fennema <postgres(at)jeltef(dot)nl>
To: John Naylor <john(dot)naylor(at)enterprisedb(dot)com>
Cc: Junwang Zhao <zhjwpku(at)gmail(dot)com>, Richard Guo <guofenglinux(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Should we use MemSet or {0} for struct initialization?
Date: 2023-09-01 14:04:58
Message-ID: CAGECzQSXxq1f1xzyVk07+d==yhLRPy95bvkv82Qa+rPf=Yuk5A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, 1 Sept 2023 at 15:25, John Naylor <john(dot)naylor(at)enterprisedb(dot)com>
wrote:
> On Fri, Sep 1, 2023 at 7:48 PM Jelte Fennema <postgres(at)jeltef(dot)nl> wrote:
> > The C standard says:
> > > When a value is stored in an object of structure or union type,
including in a member object, the bytes of the object representation that
correspond to any padding bytes take unspecified values.
> >
> > So if you set any of the fields after a MemSet, the values of the
> > padding bytes that were set to 0 are now unspecified. It seems much
> > safer to actually spell out the padding fields of a hash key.
>
> No, the standard is telling you why you need to memset if consistency of
padding bytes matters.

Maybe I'm misunderstanding the sentence from the C standard I quoted. But
under my interpretation it means that even an assignment to a field of a
struct causes the padding bytes to take unspecified (but not undefined)
values, because of the "including in a member object" part of the sentence.
It's ofcourse possible that all compilers relevant to Postgres never
actually change padding when assigning to a field.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2023-09-01 14:30:23 Re: trying again to get incremental backup
Previous Message Chapman Flack 2023-09-01 14:03:27 Re: Should we use MemSet or {0} for struct initialization?