-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/cgo: C.stdout no longer assignable in go 1.10 #25221
Comments
Strictly speaking this has never been valid code. The C specification doesn't require stdout, stderr and stdin to be lvalues, that is, they don't have to be assignable to. For example, if you used musl instead of glibc, assigning to stdout would fail to compile even in ordinary C code. |
As a (non-portable) workaround, you can probably put the address operation in the cgo preamble. #include <stdint.h>
#include <stdio.h>
static FILE** p_stdout = &stdout;
static FILE** p_stderr = &stderr; |
This is unfortunate but I don't think it's a bug. As @dominikh says, you were relying on specific behavior of your library, which is not guaranteed by the C standard and does not work with other libraries. We've changed cgo to support I'm going to close this issue. Please comment if you disagree. |
@bcmills Thanks for the suggestion! In case anyone else has a similar hack... I needed to explicitly close stdout/stderr prior to modifying the pointers to get things working with the pointer-defined-in-C approach. Apparently Go was clever enough to close the files for me when directly referencing them in Go? |
@sean-jc, you might supposedly could employ an approach which is (and always was, IIUC) correct for C:
The "trick" making this work is due to this feature of
Since stdout always uses FD 1, (with stdin using 0 and stderr—2), when you close stdout and immediately Note that you must be sure these jumps around redirecting FDs are not done concurrently—to avoid races between One caveat about this is that calling |
The standard way of doing this on the C side is via @kostix Your advice regarding |
@fweimer IIUC, |
What version of Go are you using (
go version
)?go version devel +03876af91c Wed Aug 30 18:28:58 2017 +0000 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
Take the address of C.stdout and C.stderr to modify the underlying variable, e.g. to redirect stdout/stderr of C code. This worked in go1.9 and earlier, whereas newer Golang builds fail with
cannot take the address of _Cmacro_stdout()
, orcannot assign to _Cmacro_stdout()
when assigning directly to C.stdout. Bisecting pointed at03876af91c ("cmd/cgo: support niladic function-like macros")
https://play.golang.org/p/Fiwr8u-CaN8
What did you expect to see?
Successful compilation.
What did you see instead?
The text was updated successfully, but these errors were encountered: