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

Comments

Projects
None yet
5 participants
@rmmh
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 from cgo Unix C code can't modify environment variables for Go to runtime: cgo Unix C code can't modify environment variables for Go Sep 15, 2018

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Sep 15, 2018

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Sep 15, 2018

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

@rmmh

This comment has been minimized.

Contributor

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

This comment has been minimized.

Contributor

davecheney commented Sep 15, 2018

@dmitshur

This comment has been minimized.

Member

dmitshur commented Sep 17, 2018

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

This comment has been minimized.

wsc1 commented Oct 11, 2018

I have added it to the wiki

For me, that would suffice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment