From 9f3bf573f7b794e1cbe30f8194b813ffa9fdc14c Mon Sep 17 00:00:00 2001 From: "Z. Liu" Date: Wed, 17 Dec 2025 10:43:09 +0800 Subject: [PATCH] idtools: avoid direct use of C.stderr to fix musl cgo build failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- storage/pkg/idtools/idtools_supported.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/storage/pkg/idtools/idtools_supported.go b/storage/pkg/idtools/idtools_supported.go index 9a17f57014..8a3076a0f8 100644 --- a/storage/pkg/idtools/idtools_supported.go +++ b/storage/pkg/idtools/idtools_supported.go @@ -20,6 +20,12 @@ struct subid_range get_range(struct subid_range *ranges, int i) return ranges[i]; } +// helper for stderr to avoid referencing C.stderr from Go code, +// which breaks cgo on musl due to stderr being declared as FILE *const +static FILE *subid_stderr(void) { + return stderr; +} + #if !defined(SUBID_ABI_MAJOR) || (SUBID_ABI_MAJOR < 4) # define subid_init libsubid_init # define subid_get_uid_ranges get_subuid_ranges @@ -44,7 +50,7 @@ func readSubid(username string, isUser bool) (ranges, error) { } onceInit.Do(func() { - C.subid_init(C.CString("storage"), C.stderr) + C.subid_init(C.CString("storage"), C.subid_stderr()) }) cUsername := C.CString(username)