]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: Clearenv now unsets env vars on Windows
authorJesse Szwedko <jesse.szwedko@gmail.com>
Sun, 13 Nov 2016 21:29:19 +0000 (13:29 -0800)
committerAlex Brainman <alex.brainman@gmail.com>
Mon, 14 Nov 2016 00:04:03 +0000 (00:04 +0000)
Previously, `os.Clearenv()` (by way of `syscall.Clearenv`) would simply
set all environment variables' values to `""` rather than actually
unsetting them causing subsequent `os.LookupEnv` calls to return that
they were still set.

Fixes #17902

Change-Id: I54081b4b98665e9a39f55ea7582c8d40bb8a2a22
Reviewed-on: https://go-review.googlesource.com/33168
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
src/os/env_test.go
src/syscall/env_windows.go

index d1074cdc60af11779957baa3616ec6e9d825c693..e5749f0e8996113951316d84c53e713dbae0923a 100644 (file)
@@ -95,6 +95,34 @@ func TestUnsetenv(t *testing.T) {
        }
 }
 
+func TestClearenv(t *testing.T) {
+       const testKey = "GO_TEST_CLEARENV"
+       const testValue = "1"
+
+       // reset env
+       defer func(origEnv []string) {
+               for _, pair := range origEnv {
+                       // Environment variables on Windows can begin with =
+                       // http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
+                       i := strings.Index(pair[1:], "=") + 1
+                       if err := Setenv(pair[:i], pair[i+1:]); err != nil {
+                               t.Errorf("Setenv(%q, %q) failed during reset: %v", pair[:i], pair[i+1:], err)
+                       }
+               }
+       }(Environ())
+
+       if err := Setenv(testKey, testValue); err != nil {
+               t.Fatalf("Setenv(%q, %q) failed: %v", testKey, testValue, err)
+       }
+       if _, ok := LookupEnv(testKey); !ok {
+               t.Errorf("Setenv(%q, %q) didn't set $%s", testKey, testValue, testKey)
+       }
+       Clearenv()
+       if val, ok := LookupEnv(testKey); ok {
+               t.Errorf("Clearenv() didn't clear $%s, remained with value %q", testKey, val)
+       }
+}
+
 func TestLookupEnv(t *testing.T) {
        const smallpox = "SMALLPOX"      // No one has smallpox.
        value, ok := LookupEnv(smallpox) // Should not exist.
index 3f751678ccb333a78bd8f6b6c9e83a74d1b3f9d3..1606b424ca0efedad65a85aed3ddae540685f747 100644 (file)
@@ -60,7 +60,7 @@ func Clearenv() {
                // http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
                for j := 1; j < len(s); j++ {
                        if s[j] == '=' {
-                               Setenv(s[0:j], "")
+                               Unsetenv(s[0:j])
                                break
                        }
                }