Skip to content
Snippets Groups Projects
Commit dd41ebc2 authored by Vladislav Shpilevoy's avatar Vladislav Shpilevoy
Browse files

app: os.setenv() affects os.environ()

os.setenv() and os.environ() are Lua API for

    extern char **environ;
    int setenv();

The Open Group standardized access points for environment
variables. But there is no a word about that environ never
changes. Programs can't relay on that. For example, addition of
a new variable may cause realloc of the whole environ array, and
therefore change of its pointer value. That was exactly the case
in os.environ() - it was using value of environ array remembered
when Tarantool started.

And os.setenv() could realloc the array and turn the saved pointer
into garbage.

Closes #4733

(cherry picked from commit 954d4bdc)
parent 1f41f95b
No related branches found
No related tags found
No related merge requests found
......@@ -9,9 +9,8 @@ ffi.cdef[[
int unsetenv(const char *name);
]]
local environ = ffi.C.environ
os.environ = function()
local environ = ffi.C.environ
if not environ then
return nil
end
......
......@@ -24,6 +24,9 @@ type(env_dict)
---
- table
...
err = nil
---
...
test_run:cmd("setopt delimiter ';'")
---
- true
......@@ -31,11 +34,46 @@ test_run:cmd("setopt delimiter ';'")
do
for k, v in pairs(env_dict) do
if type(k) ~= 'string' or type(v) ~= 'string' then
return false
err = {k, v}
break
end
end
return true
end;
end
test_run:cmd("setopt delimiter ''");
---
- true
...
err
---
- null
...
--
-- gh-4733: os.setenv() should affect os.environ
--
size = 0
---
...
for _, __ in pairs(os.environ()) do size = size + 1 end
---
...
for i = 1, size do os.setenv('gh-4733-test-var'..i, tostring(i)) end
---
...
env = os.environ()
---
...
err = nil
---
...
for i = 1, size do \
local value = env['gh-4733-test-var'..i] \
if value ~= tostring(i) then \
err = {i, value} \
break \
end \
end
---
...
err
---
- null
...
......@@ -9,13 +9,32 @@ do os.getenv('location') end
env_dict = os.environ()
type(env_dict)
err = nil
test_run:cmd("setopt delimiter ';'")
do
for k, v in pairs(env_dict) do
if type(k) ~= 'string' or type(v) ~= 'string' then
return false
err = {k, v}
break
end
end
return true
end;
test_run:cmd("setopt delimiter ''")
end
test_run:cmd("setopt delimiter ''");
err
--
-- gh-4733: os.setenv() should affect os.environ
--
size = 0
for _, __ in pairs(os.environ()) do size = size + 1 end
for i = 1, size do os.setenv('gh-4733-test-var'..i, tostring(i)) end
env = os.environ()
err = nil
for i = 1, size do \
local value = env['gh-4733-test-var'..i] \
if value ~= tostring(i) then \
err = {i, value} \
break \
end \
end
err
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment