Skip to content

Commit

Permalink
Support long PATH values in erlexec
Browse files Browse the repository at this point in the history
Previously a finite 10240 buffer was used. Long paths can fail
semi-silently, while long paths that already contain the erlang bindir
at the end can cause a crash.

Co-authored-by: Eric Meadows-Jönsson <[email protected]>
  • Loading branch information
HoloRin and ericmj committed Jan 22, 2025
1 parent 85ab462 commit 07339e8
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions erts/etc/common/erlexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,21 +555,29 @@ int main(int argc, char **argv)
if (s == NULL) {
erts_snprintf(tmpStr, sizeof(tmpStr),
"%s" PATHSEP "%s" DIRSEP "bin" PATHSEP, bindir, rootdir);
set_env("PATH", tmpStr);
} else if (strstr(s, rootdir) == NULL) {
erts_snprintf(tmpStr, sizeof(tmpStr),
"%s" PATHSEP "%s" DIRSEP "bin" PATHSEP "%s", bindir, rootdir, s);
set_env("PATH", tmpStr);
} else {
char *pathBuf = NULL;
int pathBufLen = 0;

const char *bindir_slug, *bindir_slug_index;
int bindir_slug_length;
const char *in_index;
char *out_index;

erts_snprintf(tmpStr, sizeof(tmpStr), "%s" PATHSEP, bindir);
pathBufLen = strlen(s) + strlen(bindir) + strlen(PATHSEP);
pathBuf = emalloc(pathBufLen);

erts_snprintf(pathBuf, pathBufLen, "%s" PATHSEP, bindir);

bindir_slug = strsave(tmpStr);
bindir_slug = strsave(pathBuf);
bindir_slug_length = strlen(bindir_slug);

out_index = &tmpStr[bindir_slug_length];
out_index = &pathBuf[bindir_slug_length];
in_index = s;

while ((bindir_slug_index = strstr(in_index, bindir_slug))) {
Expand All @@ -582,10 +590,11 @@ int main(int argc, char **argv)
}
efree((void*)bindir_slug);
strcpy(out_index, in_index);
}

set_env("PATH", pathBuf);
efree(pathBuf);
}
free_env_val(s);
set_env("PATH", tmpStr);

i = 1;

Expand Down

0 comments on commit 07339e8

Please sign in to comment.