Skip to contentSkip to conversation

correct FLEXPTR_* example in comment

PATCH In review René Scharfe 2 participants 5 messages Aug 13, 2016 – Aug 14, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 correct FLEXPTR_* example in comment

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

---
 git-compat-util.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-compat-util.h +1 -1
index 590bfdd..f52e00b 100644
@@ -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).
Signature · 1 line
2.9.3
#1 lore raw
Quoted 2 lines · René Scharfe

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

#2 lore raw

Am 13.08.2016 um 11:09 schrieb Jeff King:

Quoted 17 lines · René Scharfe
Quoted 3 lines

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.

Quoted 2 lines

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é

#3 lore raw
In reply to Jeff King

Am 13.08.2016 um 11:09 schrieb Jeff King:

Quoted 5 lines · René Scharfe
Quoted 3 lines

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

Quoted 7 lines

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

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

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

René

#4 lore raw
Quoted 7 lines · René Scharfe
Quoted 2 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.

Quoted 5 lines

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