Skip to content

Commit

Permalink
Fix truncation of PATH when ROOTDIR is not in PATH
Browse files Browse the repository at this point in the history
Co-authored-by: Brandon Duff <[email protected]>
  • Loading branch information
adambray and brandonduff committed Jan 27, 2025
1 parent 0fd2ae9 commit da12429
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
16 changes: 14 additions & 2 deletions erts/etc/common/erlexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,21 @@ int main(int argc, char **argv)
"%s" PATHSEP "%s" DIRSEP "bin", bindir, rootdir);
set_env("PATH", tmpStr);
} else if (strstr(s, rootdir) == NULL) {
erts_snprintf(tmpStr, sizeof(tmpStr),
char *pathBuf = NULL;
int pathBufLen = 0;
int path_sep_length = strlen(PATHSEP);
int dir_sep_length = strlen(DIRSEP);

pathBufLen =
strlen(bindir) + path_sep_length
+ strlen(rootdir) + dir_sep_length + strlen("bin") + path_sep_length
+ strlen(s) + 1;

pathBuf = emalloc(pathBufLen);

erts_snprintf(pathBuf, pathBufLen,
"%s" PATHSEP "%s" DIRSEP "bin" PATHSEP "%s", bindir, rootdir, s);
set_env("PATH", tmpStr);
set_env("PATH", pathBuf);
} else {
char *pathBuf = NULL;
int pathBufLen = 0;
Expand Down
38 changes: 30 additions & 8 deletions erts/test/erlexec_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

-export([args_file/1, evil_args_file/1, missing_args_file/1, env/1, args_file_env/1,
otp_7461/1, otp_7461_remote/1, argument_separation/1, argument_with_option/1,
zdbbl_dist_buf_busy_limit/1, long_path_env/1]).
zdbbl_dist_buf_busy_limit/1, long_path_env/1, long_path_env_when_rootdir_not_present/1]).

-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
Expand All @@ -44,7 +44,7 @@ suite() ->
all() ->
[args_file, evil_args_file, missing_args_file, env, args_file_env,
otp_7461, argument_separation, argument_with_option, zdbbl_dist_buf_busy_limit,
long_path_env].
long_path_env, long_path_env_when_rootdir_not_present].

init_per_suite(Config) ->
[{suite_erl_flags, save_env()} | Config].
Expand Down Expand Up @@ -456,10 +456,32 @@ long_path_env(Config) when is_list(Config) ->
Cmd = PName ++ " -noshell -eval 'io:format(\"~ts\", [os:getenv(\"PATH\")]),erlang:halt()'",

compare_erl_path(Cmd, BinPath, ActualPath),
compare_erl_path(Cmd, BinPath, pathjoin([ActualPath, LongPath])),
compare_erl_path(Cmd, BinPath, pathjoin([ActualPath, LongPath, BinPath])),
compare_erl_path(Cmd, BinPath, pathjoin([BinPath, ActualPath, LongPath])),
compare_erl_path(Cmd, BinPath, pathjoin([BinPath, ActualPath, LongPath, BinPath])),
compare_erl_path(Cmd, BinPath, path_var_join([ActualPath, LongPath])),
compare_erl_path(Cmd, BinPath, path_var_join([ActualPath, LongPath, BinPath])),
compare_erl_path(Cmd, BinPath, path_var_join([BinPath, ActualPath, LongPath])),
compare_erl_path(Cmd, BinPath, path_var_join([BinPath, ActualPath, LongPath, BinPath])),
ok.

long_path_env_when_rootdir_not_present(Config) when is_list(Config) ->
BinPath = os:getenv("BINDIR"),
RootPath = os:getenv("ROOTDIR"),
RootPathWithBin = filename:join(RootPath, "bin"),
ActualPath = os:getenv("PATH"),
LongPathLength = 10240,

LongPath = lists:duplicate(LongPathLength, "x"),
{ok, [[PName]]} = init:get_argument(progname),
Cmd = "\"" ++ filename:join(RootPathWithBin, PName) ++ "\"" ++ " -noshell -eval 'io:format(\"~ts\", [os:getenv(\"PATH\")]),erlang:halt()'",

PathComponents = string:split(ActualPath, pathsep(), all),
ActualPathNoRoot = path_var_join(lists:filter(fun (Path) ->
(Path =/= RootPathWithBin) and (Path =/= (RootPathWithBin ++ "/")) and (Path =/= BinPath)
end, PathComponents)),

os:putenv("PATH", path_var_join([ActualPathNoRoot, LongPath, LongPath])),
Output = os:cmd(Cmd),

?assertEqual(string:length(string:find(Output, LongPath ++ ":" ++ LongPath)), (LongPathLength * 2) + 1),
ok.

compare_erl_path(Cmd, BinPath, Path) ->
Expand All @@ -475,8 +497,8 @@ pathsep() ->
_ -> ":"
end.

pathjoin(Components) ->
lists:concat(lists:join(pathsep(), Components)).
path_var_join(Paths) ->
lists:concat(lists:join(pathsep(), Paths)).


%%
Expand Down

0 comments on commit da12429

Please sign in to comment.