Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions mrbgems/mruby-env/src/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ extern char **environ;
#endif
#endif

static void
/* Checks the name and returns it as a C string. RSTRING_PTR() is not one: a
substring shares the bytes of the string it was cut from, and getenv()
would read on past its length into them. */
static const char*
env_check_key(mrb_state *mrb, mrb_value key)
{
mrb_ensure_string_type(mrb, key);
Expand All @@ -33,6 +36,7 @@ env_check_key(mrb_state *mrb, mrb_value key)
if (memchr(RSTRING_PTR(key), '\0', RSTRING_LEN(key))) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "bad environment variable name: contains null byte");
}
return RSTRING_CSTR(mrb, key);
}

/* ENV[] */
Expand All @@ -43,8 +47,7 @@ mrb_env_aref(mrb_state *mrb, mrb_value self)
const char *val;

mrb_get_args(mrb, "o", &key);
env_check_key(mrb, key);
val = getenv(RSTRING_PTR(key));
val = getenv(env_check_key(mrb, key));
if (val == NULL) return mrb_nil_value();
return mrb_str_new_cstr(mrb, val);
}
Expand All @@ -54,15 +57,16 @@ static mrb_value
mrb_env_aset(mrb_state *mrb, mrb_value self)
{
mrb_value key, val;
const char *name;

mrb_get_args(mrb, "oo", &key, &val);
env_check_key(mrb, key);
name = env_check_key(mrb, key);

if (mrb_nil_p(val)) {
#ifdef _WIN32
_putenv_s(RSTRING_PTR(key), "");
_putenv_s(name, "");
#else
unsetenv(RSTRING_PTR(key));
unsetenv(name);
#endif
return mrb_nil_value();
}
Expand All @@ -72,9 +76,9 @@ mrb_env_aset(mrb_state *mrb, mrb_value self)
mrb_raise(mrb, E_ARGUMENT_ERROR, "bad environment variable value: contains null byte");
}
#ifdef _WIN32
_putenv_s(RSTRING_PTR(key), RSTRING_PTR(val));
_putenv_s(name, RSTRING_CSTR(mrb, val));
#else
setenv(RSTRING_PTR(key), RSTRING_PTR(val), 1);
setenv(name, RSTRING_CSTR(mrb, val), 1);
#endif
return val;
}
Expand All @@ -84,18 +88,18 @@ static mrb_value
mrb_env_delete(mrb_state *mrb, mrb_value self)
{
mrb_value key;
const char *val;
const char *name, *val;
mrb_value result;

mrb_get_args(mrb, "o", &key);
env_check_key(mrb, key);
val = getenv(RSTRING_PTR(key));
name = env_check_key(mrb, key);
val = getenv(name);
if (val == NULL) return mrb_nil_value();
result = mrb_str_new_cstr(mrb, val);
#ifdef _WIN32
_putenv_s(RSTRING_PTR(key), "");
_putenv_s(name, "");
#else
unsetenv(RSTRING_PTR(key));
unsetenv(name);
#endif
return result;
}
Expand Down Expand Up @@ -156,8 +160,7 @@ mrb_env_has_key(mrb_state *mrb, mrb_value self)
mrb_value key;

mrb_get_args(mrb, "o", &key);
env_check_key(mrb, key);
return mrb_bool_value(getenv(RSTRING_PTR(key)) != NULL);
return mrb_bool_value(getenv(env_check_key(mrb, key)) != NULL);
}

/* ENV.to_s */
Expand Down
19 changes: 19 additions & 0 deletions mrbgems/mruby-env/test/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
assert_nil ENV["MRUBY_ENV_TEST_A"]
end

assert('ENV reads a substring only up to its length') do
# long enough that the substrings share their parents' bytes
full = "MRUBY_ENV_TEST_SUBSTR_" + "K" * 20 + "_TAIL"
key = full[0, 42]
ENV[full] = "full"
assert_nil ENV[key]
assert_false ENV.key?(key)
assert_nil ENV.delete(key)
ENV[key] = "key"
assert_equal "full", ENV[full]
assert_equal "key", ENV[key]
ENV[key] = nil
assert_equal "full", ENV[full]
ENV[key] = ("v" * 30 + "_TAIL")[0, 30]
assert_equal "v" * 30, ENV[key]
ENV.delete(key)
ENV.delete(full)
end

assert('ENV[] returns nil for missing key') do
assert_nil ENV["MRUBY_ENV_TEST_NONEXISTENT"]
end
Expand Down
Loading