File 0866-Null-terminate-PATH-string-when-building-PATH.patch of Package erlang

From 9ac7bcf17cde675d2fbba38c6c4f3efdb77448cb Mon Sep 17 00:00:00 2001
From: Brandon Duff <brandon@mechanical-orchard.com>
Date: Tue, 28 Jan 2025 11:42:35 -0500
Subject: [PATCH 6/6] Null terminate PATH string when building PATH

Co-authored-by: Rin Kuryloski <rin.kuryloski@mechanical-orchard.com>
---
 erts/etc/common/erlexec.c   |  4 +++-
 erts/test/erlexec_SUITE.erl | 18 ++++++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/erts/etc/common/erlexec.c b/erts/etc/common/erlexec.c
index ff56ce4fae..c0b144277d 100644
--- a/erts/etc/common/erlexec.c
+++ b/erts/etc/common/erlexec.c
@@ -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);
@@ -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);
diff --git a/erts/test/erlexec_SUITE.erl b/erts/test/erlexec_SUITE.erl
index 88568cead4..c2b2bf4162 100644
--- a/erts/test/erlexec_SUITE.erl
+++ b/erts/test/erlexec_SUITE.erl
@@ -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()'",
 
@@ -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) ->
@@ -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()'",
 
@@ -481,7 +490,7 @@ 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) ->
@@ -489,7 +498,8 @@ compare_erl_path(Cmd, BinPath, 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
-- 
2.43.0

openSUSE Build Service is sponsored by