Skip to content

Conversation

@clan
Copy link

@clan clan commented Dec 17, 2025

On musl-based systems, stderr is declared as FILE *const.

Referencing stderr directly from Go code (via C.stderr) causes cgo to generate assignment code for a const-qualified pointer, which is invalid C and fails to compile.

Both gcc and clang reject the generated code with error messages below:

clang:

cgo-gcc-prolog:85:9: error: cannot assign to variable '_cgo_r' with const-qualified type 'typeof (_cgo_a->r)' (aka 'struct _IO_FILE *const')
cgo-gcc-prolog:83:24: note: variable '_cgo_r' declared const here
cgo-gcc-prolog:88:12: error: cannot assign to non-static data member 'r' with const-qualified type 'FILE *const' (aka 'struct _IO_FILE *const')
cgo-gcc-prolog:80:15: note: non-static data member 'r' declared const here

gcc:

cgo-gcc-prolog:85:9: error: assignment of read-only variable '_cgo_r'
cgo-gcc-prolog:88:12: error: assignment of read-only member 'r'

This patch avoids referencing C.stderr from Go code and instead returns stderr from a small C helper function. This keeps the usage entirely in C and avoids cgo’s broken handling for const-qualified global objects.

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all
    commits. (git commit -s). (If needed, use git commit -s --amend). The author email must match
    the sign-off email address. See CONTRIBUTING.md
    for more information.
  • Referenced issues using Fixes: #00000 in commit message (if applicable)
  • Tests have been added/updated (or no tests are needed)
  • Documentation has been updated (or no documentation changes are needed)
  • All commits pass make validatepr (format/lint checks)
  • Release note entered in the section below (or None if no user-facing changes)

Does this PR introduce a user-facing change?

None

On musl-based systems, stderr is declared as FILE *const.

Referencing stderr directly from Go code (via C.stderr) causes cgo to
generate assignment code for a const-qualified pointer, which is invalid C
and fails to compile.

Both gcc and clang reject the generated code with error messages below:

clang:
> cgo-gcc-prolog:85:9: error: cannot assign to variable '_cgo_r' with const-qualified type 'typeof (_cgo_a->r)' (aka 'struct _IO_FILE *const')
> cgo-gcc-prolog:83:24: note: variable '_cgo_r' declared const here
> cgo-gcc-prolog:88:12: error: cannot assign to non-static data member 'r' with const-qualified type 'FILE *const' (aka 'struct _IO_FILE *const')
> cgo-gcc-prolog:80:15: note: non-static data member 'r' declared const here

gcc:
> cgo-gcc-prolog:85:9: error: assignment of read-only variable '_cgo_r'
> cgo-gcc-prolog:88:12: error: assignment of read-only member 'r'

This patch avoids referencing C.stderr from Go code and instead returns
stderr from a small C helper function. This keeps the usage entirely in
C and avoids cgo’s broken handling for const-qualified global objects.

Signed-off-by: Z. Liu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant