mirror of
https://github.com/golang/go
synced 2024-11-26 14:56:47 -07:00
a95e2ae280
This test is verifying that setting or unsetting an environment variable in Go via the "os" package makes that change visible to the C getenv function. The test has been failing on Windows since CL 304569; it isn't clear to me whether it was running at all before that point. On Windows the getenv and _putenv C functions are not thread-safe, so Go's os.Setenv and os.Getenv use the SetEnvironmentVariable and GetEnvironmentVariable system calls instead. That seems to work fine in practice; however, changes via SetEnvironmentVariable are empirically not visible to the C getenv function on certain versions of Windows. The MSDN getenv documentation¹ states that ‘getenv operates only on the data structures accessible to the run-time library and not on the environment “segment” created for the process by the operating system. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.’ That may be related to what we're seeing here. (https://github.com/curl/curl/issues/4774 describes this same behavior observed in the curl project.) ¹https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-wgetenv?view=msvc-160#remarks Updates #36705 Change-Id: I222792f75c650f32c5025b0fa3edab232ff66353 Reviewed-on: https://go-review.googlesource.com/c/go/+/304669 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org>
28 lines
644 B
Go
28 lines
644 B
Go
// +build cgo,!windows
|
|
// run fake-arg-to-force-use-of-go-run
|
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
// #include <stdlib.h>
|
|
// #include <unistd.h>
|
|
import "C"
|
|
|
|
import "os"
|
|
|
|
func main() {
|
|
os.Setenv("FOO", "bar")
|
|
s := C.GoString(C.getenv(C.CString("FOO")))
|
|
if s != "bar" {
|
|
panic("bad setenv, environment variable only has value \"" + s + "\"")
|
|
}
|
|
os.Unsetenv("FOO")
|
|
s = C.GoString(C.getenv(C.CString("FOO")))
|
|
if s != "" {
|
|
panic("bad unsetenv, environment variable still has value \"" + s + "\"")
|
|
}
|
|
}
|