public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: "Paul Eggert" <eggert@cs.ucla.edu>,
	"Cristian Rodríguez" <cristian@rodriguez.im>,
	libc-alpha@sourceware.org
Cc: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Subject: Re: [PATCH 0/6] Support use of -fstrict-flex-arrays in user code
Date: Sat, 12 Aug 2023 13:07:45 +0200	[thread overview]
Message-ID: <b333172a-881e-fa21-7c54-7fce49116011@kernel.org> (raw)
In-Reply-To: <35cea809-9f98-882b-1df4-3c86d106b580@cs.ucla.edu>


[-- Attachment #1.1: Type: text/plain, Size: 2064 bytes --]

Hi Paul, Cristian,

On 2023-08-12 00:10, Paul Eggert wrote:
> On 2023-07-19 07:57, Cristian Rodríguez via Libc-alpha wrote:
>>> Can't we use the already define __flexarr/__glibc_c99_flexarr_available
>>> macro
>>> instead?
>>
>> If I did only that, sizeof public structures may change causing an abi
>> break.
> 
> Why would it cause an ABI break?
> 
> For example, no program's machine code should depend on sizeof (FTSENT).

+1


> fts.h's FTSENT is supposed to be an incomplete data type, like FILE: the 
> system allocates FTSENT objects, not user code. So I don't see the harm 
> in replacing [1] with [] in the definition of FTSENT's fts_name member, 
> if the compiler supports flex arrays.
> 

I'll raise the bid, and claim that no valid program can depend on
sizeof() of any struct with a flexible array member, of any kind (old
`[1]`, zero-length `[0]`, nor standard `[]`).

It is conceptually wrong to calculate sizeof() it, and the right thing to
do is to call offseof(s, fam).

In the best case, sizeof() will return the same as offsetof(3), and the
program will happen to work.  But in some cases, it will return a value
slightly bigger (ISO C guarantees that it will not be smaller, for no
reason I think), and in those cases, the program must have a bug.  That
bug can either be benign, and the program will just be wasting a few
bytes, or it can be bad, and read memory a few bytes off from where it
was written (still within the allocation bounds, so it won't crash; at
least not directly due to that, but maybe by a secondary bug).

See this GCC thread where I propose adding a warning when
sizeof(flexi_struct) is used in arithmetics.  There's an example program
showing why sizeof() is (almost) always a bad thing with these structures.
In the only use case where sizeof() is valid, reducing it wouldn't
change program behavior (it's within a malloc(MAX(sizeof(s), ...));).

Cheers,
Alex

-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-08-12 11:07 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-10 15:04 Cristian Rodríguez
2023-07-10 15:04 ` [PATCH 1/6] Introduce __decl_is_flex_array macro Cristian Rodríguez
2023-07-10 15:04 ` [PATCH 2/6] dlfcn : annotate dls_serpath with __decl_is_flex_array Cristian Rodríguez
2023-07-10 15:04 ` [PATCH 3/6] inet: annotate tu_data and tu_stuff " Cristian Rodríguez
2023-07-10 15:04 ` [PATCH 4/6] inet: annotate ip6r0_addr " Cristian Rodríguez
2023-07-10 15:04 ` [PATCH 5/6] io: annotate fts_name " Cristian Rodríguez
2023-07-10 15:04 ` [PATCH 6/6] misc: annotate q_data " Cristian Rodríguez
2023-07-19 12:40 ` [PATCH 0/6] Support use of -fstrict-flex-arrays in user code Cristian Rodríguez
2023-07-19 14:27   ` Adhemerval Zanella Netto
2023-07-19 14:57     ` Cristian Rodríguez
2023-07-19 17:48       ` Adhemerval Zanella Netto
2023-08-11 15:34         ` Cristian Rodríguez
2023-08-11 22:10       ` Paul Eggert
2023-08-12 11:07         ` Alejandro Colomar [this message]
2023-08-12 13:29           ` Alejandro Colomar
2023-08-12 14:53         ` Cristian Rodríguez
2023-08-12 18:20           ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b333172a-881e-fa21-7c54-7fce49116011@kernel.org \
    --to=alx@kernel.org \
    --cc=adhemerval.zanella@linaro.org \
    --cc=cristian@rodriguez.im \
    --cc=eggert@cs.ucla.edu \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).