git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] correct FLEXPTR_* example in comment

From
René Scharfe <l.s.r@web.de>
Date
Aug 13, 2016, 17:26 UTC
Message-ID
<57AF582A.3050406@web.de>
In-Reply-To
<20160813090947.uj6hurji5o6cwxge@sigill.intra.peff.net>
Am 13.08.2016 um 11:09 schrieb Jeff King:
Show 12 quoted lines
> On Sat, Aug 13, 2016 at 11:01:21AM +0200, René Scharfe wrote:
>
>> This section is about "The FLEXPTR_* variants", so use FLEXPTR_ALLOC_STR
>> in the example.
>
> Oops, yeah. Your patch is clearly an improvement.
>
> Since this is obviously an easy mistake to make (using one form rather
> than the other), I wondered if the compiler would catch it.
>
> I think it would catch an accidental use of FLEXPTR instead of FLEX,
> because it involves an attempted assignment of an array.
Gcc 5.4 reports "invalid use of flexible array member".
Show 5 quoted lines
> But I don't
> think we would catch the reverse; we'd just write the data directly on
> top of the pointer. That would probably crash immediately at runtime, so
> if you exercise the code at all in tests, it is OK. But something to be
> aware of.
No hint at compile time, segfaults at runtime.
> I suppose it could assert(sizeof((x)->flexname) == FLEX_ALLOC) or
> something, but I'm not sure if it is worth worrying about.

You can't use sizeof with an actual flexible array. It only works if FLEX_ARRAY is defined as 1 (for platforms without native support), and perhaps also if it's 0.

offsetof(struct x, arr) == sizeof(struct x) won't work either because of padding.

I have no idea what you can do with a flexible array that would throw a compile error when done with a pointer.

René
Previous: René ScharfeNext: Jeff King
Message 4 of 5 in “correct FLEXPTR_* example in comment”
  1. correct FLEXPTR_* example in commentRené Scharfe, Aug 13, 2016
  2. Jeff KingAug 13, 2016
  3. René ScharfeAug 13, 2016
  4. René ScharfeAug 13, 2016
  5. Jeff KingAug 13, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.