Skip to content
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

runtime: cgo Unix C code can't modify environment variables for Go #27693

Open
rmmh opened this issue Sep 15, 2018 · 5 comments
Open

runtime: cgo Unix C code can't modify environment variables for Go #27693

rmmh opened this issue Sep 15, 2018 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rmmh
Copy link
Contributor

rmmh commented Sep 15, 2018

What version of Go are you using (go version)?

1.11

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

linux amd64

What did you do?

Because of envOnce and copyenv in env_unix.go, linked non-Go programs can't set environment variables that are visible to Go code.

This is probably WAI, but it's surprising when embedding and could use a mention somewhere.

package main

/*
#include <stdlib.h>       // for setenv()
*/
import "C"

import (
	"fmt"
	"os"
)

func main() {
	os.Setenv("foo", "1")
	fmt.Println("before:", os.Getenv("foo"))
	C.setenv(C.CString("foo"), C.CString("2"), 1)
	fmt.Println("after:", os.Getenv("foo"))
	os.Setenv("foo", "3")
	fmt.Println("after Go:", os.Getenv("foo"))
}

What did you see instead?

"1 1 3" instead of "1 2 3".

@ianlancetaylor ianlancetaylor changed the title cgo Unix C code can't modify environment variables for Go runtime: cgo Unix C code can't modify environment variables for Go Sep 15, 2018
@ianlancetaylor ianlancetaylor added Documentation NeedsFix The path to resolution is known, but the work has not been done. labels Sep 15, 2018
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Sep 15, 2018
@ianlancetaylor
Copy link
Contributor

Any suggestions on where this could be documented where the people who need to know about it would actually see it?

@rmmh
Copy link
Contributor Author

rmmh commented Sep 15, 2018

Maybe a "Known Issues" section on the cmd/cgo docs might work, linking to a relevant GitHub search? It already has some tidbits like "avoid passing uninitialized C memory to Go code if the Go code is going to store pointer values in it. Zero out the memory in C before passing it to Go.". I'm sure there are a few other cgo gotchas that should be documented.

@davecheney
Copy link
Contributor

davecheney commented Sep 15, 2018 via email

@dmitshur
Copy link
Contributor

This issue has a high similarity to #22302 in nature, which was resolved with CL 75751. Perhaps that is a good example to follow?

It stands out that unlike runtime.GOROOT being a single function, there are many functions in os that deal with getting/setting environment variables.

@wsc1
Copy link

wsc1 commented Oct 11, 2018

I have added it to the wiki

For me, that would suffice.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants