threads / patch / 43801

patchcorrect FLEXPTR_* example in comment

Subject: [PATCH] correct FLEXPTR_* example in comment

## tl;dr

5 messages between Aug 13, 2016 and Aug 14, 2016. Diffs are folded; open one to read it.

replies: 4people: 2as markdown or json

René Scharfe· Aug 13, 2016, 09:01 UTC · lore

This section is about "The FLEXPTR_* variants", so use FLEXPTR_ALLOC_STR in the example.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 git-compat-util.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to git-compat-util.h +1 −1
diff --git a/git-compat-util.h b/git-compat-util.h
index 590bfdd..f52e00b 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -815,7 +815,7 @@ extern FILE *fopen_for_writing(const char *path);
  * you can do:
  *
  *   struct foo *f;
- *   FLEX_ALLOC_STR(f, name, src);
+ *   FLEXPTR_ALLOC_STR(f, name, src);
  *
  * and "name" will point to a block of memory after the struct, which will be
  * freed along with the struct (but the pointer can be repointed anywhere).
-- 
2.9.3
Jeff King· Aug 13, 2016, 09:09 UTC · re: René Scharfe · lore

Re: [PATCH] correct FLEXPTR_* example in comment

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. 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.

I suppose it could assert(sizeof((x)->flexname) == FLEX_ALLOC) or something, but I'm not sure if it is worth worrying about.

-Peff
René Scharfe· Aug 13, 2016, 09:19 UTC · re: Jeff King · lore

Re: [PATCH] correct FLEXPTR_* example in comment

Am 13.08.2016 um 11:09 schrieb Jeff King:
Show 19 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. 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.
>
> I suppose it could assert(sizeof((x)->flexname) == FLEX_ALLOC) or
> something, but I'm not sure if it is worth worrying about.

A compilation error or warning would be nice. I scratched my head quite a bit before figuring out that the example was wrong. Copy&paste from a reputable source must be correct, right? :)

René
René Scharfe· Aug 13, 2016, 17:26 UTC · re: Jeff King · lore

Re: [PATCH] correct FLEXPTR_* example in comment

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é
Jeff King· Aug 13, 2016, 18:36 UTC · re: René Scharfe · lore

Re: [PATCH] correct FLEXPTR_* example in comment

On Sat, Aug 13, 2016 at 07:26:02PM +0200, René Scharfe wrote:
Show 12 quoted lines
> > 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.

Thanks for the input. I'd say we should not worry about it. The reason this particular "bug" persisted is because it was in a comment. People tend to notice code that cannot possibly do anything but segfault before they even send in patches.

-Peff

← back to recent threads