-
Notifications
You must be signed in to change notification settings - Fork 997
src/os: make Environ() return a copy of the environment #2647
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
Conversation
|
this pull request should also bring back the test deleted by #2603 |
Removed as flaky in tinygo-org#2603
src/os/env.go
Outdated
| func Environ() []string { | ||
| return syscall.Environ() | ||
| orig := syscall.Environ() | ||
| single := strings.Join(orig, "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upsteam does the copy in syscall.Environ(), we might want to do the same in case someone calls that directly?
|
That probably makes sense. I'll move this logic (or equivalent) there. |
|
Would it make sense to do a single allocation to copy libc_environ and then point to bits of it? That might be faster...? |
|
Yeah, I was torn about doing that because we need to loop over the environment a few times instead of just once. Let me try that approach and see what it looks like |
|
Now with a single allocation. |
b62f37f to
b235c9d
Compare
b235c9d to
72ca05f
Compare
dkegel-fastly
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| // copy over the bytes to the Go heap | ||
| copy(bs, envVar) | ||
| // convert trimmed slice to string | ||
| s := *(*string)(unsafe.Pointer(&bs)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a dirty trick... but I have to admit, this whole function isn't exactly clean and I wrote it originally.
(Dirty because it's not guaranteed that slice/string layout will stay the way it is now - although I don't see why it would need to change).
Note that this can have the side effect of keeping more alive than needed. Imagine you only need a single value out of the environment. So you call |
|
Good point about a single environment variable pinning the entire allocation. I think I'll add a comment to this function about that |
|
Yeah, that went through my head too, I should have mentioned it. |
Fixes #2646