Skip to content

getEnv + friends gives SIGSEGV or wrong results; osenv should not try to keep track of environment #18533

@timotheecour

Description

@timotheecour

Example 1

import os

proc c_getenv(env: cstring): cstring {.importc: "getenv", header: "<stdlib.h>".}
var thr: Thread[void]
proc threadFunc {.thread.} =
  putEnv("foo", "fooVal2")
proc main()=
  putEnv("foo", "fooVal1")
  echo getEnv("foo")
  createThread(thr, threadFunc)
  joinThreads(thr)
  echo getEnv("foo")
  echo $c_getenv("foo")
main()

Current Output

before #18530:

fooVal1
fooVal1
Traceback (most recent call last)
/Users/timothee/git_clone/nim/timn/tests/nim/all/t12606.nim(18) t12606
/Users/timothee/git_clone/nim/timn/tests/nim/all/t12606.nim(17) main
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

after #18530: it improves, but still wrong (/cc @brightly-salty):
fooVal1
fooVal1
fooVal2

Expected Output

fooVal1
fooVal2
fooVal2

Example 2

delEnv has similar bug (since it also uses environment)

this should raise an OSError, but doesn't, because the call is intercepted by 1st checking environment

import std/os
delEnv("foo=bar")

Possible Solution

osenv should not try to keep track of environment; it's not thread-safe, and not safe against other modifications of environment variables (eg via importc procs etc)

Additional Information

1.5.1 25efb53

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS (stdlib)Related to OS modules in standard library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions