git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] correct FLEXPTR_* example in comment
@ 2016-08-13  9:01 René Scharfe
  2016-08-13  9:09 ` Jeff King
  0 siblings, 1 reply; 5+ messages in thread
From: René Scharfe @ 2016-08-13  9:01 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano, Jeff King

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(-)

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] correct FLEXPTR_* example in comment
  2016-08-13  9:01 [PATCH] correct FLEXPTR_* example in comment René Scharfe
@ 2016-08-13  9:09 ` Jeff King
  2016-08-13  9:19   ` René Scharfe
  2016-08-13 17:26   ` René Scharfe
  0 siblings, 2 replies; 5+ messages in thread
From: Jeff King @ 2016-08-13  9:09 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] correct FLEXPTR_* example in comment
  2016-08-13  9:09 ` Jeff King
@ 2016-08-13  9:19   ` René Scharfe
  2016-08-13 17:26   ` René Scharfe
  1 sibling, 0 replies; 5+ messages in thread
From: René Scharfe @ 2016-08-13  9:19 UTC (permalink / raw)
  To: Jeff King; +Cc: Git List, Junio C Hamano

Am 13.08.2016 um 11:09 schrieb Jeff King:
> 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é


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] correct FLEXPTR_* example in comment
  2016-08-13  9:09 ` Jeff King
  2016-08-13  9:19   ` René Scharfe
@ 2016-08-13 17:26   ` René Scharfe
  2016-08-13 18:36     ` Jeff King
  1 sibling, 1 reply; 5+ messages in thread
From: René Scharfe @ 2016-08-13 17:26 UTC (permalink / raw)
  To: Jeff King; +Cc: Git List, Junio C Hamano

Am 13.08.2016 um 11:09 schrieb Jeff King:
> 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".

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] correct FLEXPTR_* example in comment
  2016-08-13 17:26   ` René Scharfe
@ 2016-08-13 18:36     ` Jeff King
  0 siblings, 0 replies; 5+ messages in thread
From: Jeff King @ 2016-08-13 18:36 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano

On Sat, Aug 13, 2016 at 07:26:02PM +0200, René Scharfe wrote:

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-08-14 10:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-13  9:01 [PATCH] correct FLEXPTR_* example in comment René Scharfe
2016-08-13  9:09 ` Jeff King
2016-08-13  9:19   ` René Scharfe
2016-08-13 17:26   ` René Scharfe
2016-08-13 18:36     ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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