mailing list of musl libc
 help / color / mirror / Atom feed
* [musl] STRUCT _IO_FILE { char __x; }; as the poor C programmer's abstract struct
@ 2021-10-06 13:49 Pascal Cuoq
  2021-10-06 15:43 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Pascal Cuoq @ 2021-10-06 13:49 UTC (permalink / raw)
  To: musl

Hello,

The file stdio.h contains:

#if __STDC_VERSION__ < 201112L
#define __NEED_struct__IO_FILE
#endif
…
#include <bits/alltypes.h>

And the file alltypes.h is generated from alltypes.h.in which contains:

STRUCT _IO_FILE { char __x; };
TYPEDEF struct _IO_FILE FILE;

This means that when the compiler defines __STDC_VERSION__ to less than 201112L, a bogus definition for the type FILE and for the prototypes of functions that access values of this type can exist in files outside musl, alongside the real definition from stdio_impl.h which exists in files inside musl:

struct _IO_FILE {
        unsigned flags;
        unsigned char *rpos, *rend;
        int (*close)(FILE *);
 …

This is at least a minor annoyance when trying to do source-level verifications on a mix of source files including some files that implement musl together with some files that use musl from the outside.

Was it only in C11 that it became possible to avoid this ugly hack?

(We stumbled on this because we happened to be doing the pre-processing with -std=c99, and I think it should be no problem to change it to be done with -std=c11, but I'm surprised nevertheless because I don't see which item in https://port70.net/~nsz/c/c11/n1570.html#Forewordp6 corresponds to this change between C99 and C11. Could the test be changed to “__STDC_VERSION__ < 199901L”?)

Pascal



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

* Re: [musl] STRUCT _IO_FILE { char __x; }; as the poor C programmer's abstract struct
  2021-10-06 13:49 [musl] STRUCT _IO_FILE { char __x; }; as the poor C programmer's abstract struct Pascal Cuoq
@ 2021-10-06 15:43 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2021-10-06 15:43 UTC (permalink / raw)
  To: Pascal Cuoq; +Cc: musl

On Wed, Oct 06, 2021 at 01:49:16PM +0000, Pascal Cuoq wrote:
> Hello,
> 
> The file stdio.h contains:
> 
> #if __STDC_VERSION__ < 201112L
> #define __NEED_struct__IO_FILE
> #endif
> …
> #include <bits/alltypes.h>
> 
> And the file alltypes.h is generated from alltypes.h.in which contains:
> 
> STRUCT _IO_FILE { char __x; };
> TYPEDEF struct _IO_FILE FILE;
> 
> This means that when the compiler defines __STDC_VERSION__ to less
> than 201112L, a bogus definition for the type FILE and for the
> prototypes of functions that access values of this type can exist in
> files outside musl, alongside the real definition from stdio_impl.h
> which exists in files inside musl:
> 
> struct _IO_FILE {
>         unsigned flags;
>         unsigned char *rpos, *rend;
>         int (*close)(FILE *);
>  …
> 
> This is at least a minor annoyance when trying to do source-level
> verifications on a mix of source files including some files that
> implement musl together with some files that use musl from the
> outside.
> 
> Was it only in C11 that it became possible to avoid this ugly hack?
> 
> (We stumbled on this because we happened to be doing the
> pre-processing with -std=c99, and I think it should be no problem to
> change it to be done with -std=c11, but I'm surprised nevertheless
> because I don't see which item in
> https://port70.net/~nsz/c/c11/n1570.html#Forewordp6 corresponds to
> this change between C99 and C11. Could the test be changed to
> “__STDC_VERSION__ < 199901L”?)

The change was indeed between C99 and C11, and was obfuscated by a
change to the definition of "object type" in 6.2.5, from:

    Types are partitioned into object types (types that fully describe
    objects), function types (types that describe functions), and
    incomplete types (types that describe objects but lack information
    needed to determine their sizes).

to:

    Types are partitioned into object types (types that describe
    objects) and function types (types that describe functions). At
    various points within a translation unit an object type may be
    incomplete (lacking sufficient information to determine the size
    of objects of that type) or complete (having sufficient
    information).

This changed the meaning of the 7.21.1 ¶2 requirement that FILE be an
object type. Despite this seeming like an inadvertent change, I'm
pretty sure (based on anecdotes and possibly the Rationale document?)
that this was just the usual twisted mechanations of WG14 and that the
effect was intentional.

Note that for musl internal source files, you really should be
preprocessing them with src/include/* (introduced in commit
13d1afa46f8098df290008c681816c9eb89ffbdb) exposed. This will take care
of suppressing the 'fake' definition of struct _IO_FILE.

Rich

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

end of thread, other threads:[~2021-10-06 15:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-06 13:49 [musl] STRUCT _IO_FILE { char __x; }; as the poor C programmer's abstract struct Pascal Cuoq
2021-10-06 15:43 ` Rich Felker

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ https://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git