Skip to content

Commit

Permalink
Null terminate PATH string when building PATH
Browse files Browse the repository at this point in the history
Co-authored-by: Rin Kuryloski <[email protected]>
  • Loading branch information
brandonduff and HoloRin committed Jan 28, 2025
1 parent da12429 commit 9ac7bcf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion erts/etc/common/erlexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ int main(int argc, char **argv)
const char *in_index;
char *out_index;

pathBufLen = strlen(s) + strlen(bindir) + strlen(PATHSEP);
pathBufLen = strlen(s) + strlen(bindir) + strlen(PATHSEP) + 1;
pathBuf = emalloc(pathBufLen);

strcpy(pathBuf, bindir);
Expand All @@ -608,6 +608,8 @@ int main(int argc, char **argv)
strcpy(out_index, PATHSEP);
out_index += sep_length;
strcpy(out_index, in_index);
} else {
*out_index = '\0';
}

set_env("PATH", pathBuf);
Expand Down
18 changes: 14 additions & 4 deletions erts/test/erlexec_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,15 @@ zdbbl_dist_buf_busy_limit(Config) when is_list(Config) ->
long_path_env(Config) when is_list(Config) ->
BinPath = os:getenv("BINDIR"),
ActualPath = os:getenv("PATH"),

PathComponents = string:split(ActualPath, pathsep(), all),
ActualPathNoBinPath = path_var_join(lists:filter(fun (Path) ->
Path =/= BinPath
end, PathComponents)),
ct:log("BINDIR: ~ts", [BinPath]),
ct:log("PATH: ~ts", [ActualPath]),

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

Expand All @@ -460,6 +465,10 @@ long_path_env(Config) when is_list(Config) ->
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])),

Output = compare_erl_path(Cmd, BinPath, path_var_join([ActualPathNoBinPath, LongPath])),
?assertEqual(string:find(Output, LongPath), LongPath),

ok.

long_path_env_when_rootdir_not_present(Config) when is_list(Config) ->
Expand All @@ -469,7 +478,7 @@ long_path_env_when_rootdir_not_present(Config) when is_list(Config) ->
ActualPath = os:getenv("PATH"),
LongPathLength = 10240,

LongPath = lists:duplicate(LongPathLength, "x"),
LongPath = lists:flatten(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()'",

Expand All @@ -481,15 +490,16 @@ long_path_env_when_rootdir_not_present(Config) when is_list(Config) ->
os:putenv("PATH", path_var_join([ActualPathNoRoot, LongPath, LongPath])),
Output = os:cmd(Cmd),

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

compare_erl_path(Cmd, BinPath, Path) ->
os:putenv("PATH", Path),
Output = os:cmd(Cmd),
% BinPath is at the front of PATH and nowhere else
?assertEqual(string:find(Output, BinPath ++ ":"), Output),
?assertEqual(string:find(Output, ":" ++ BinPath), nomatch).
?assertEqual(string:find(Output, ":" ++ BinPath), nomatch),
Output.

pathsep() ->
case os:type() of
Expand Down

0 comments on commit 9ac7bcf

Please sign in to comment.