Skip to content

Commit

Permalink
Attempt #2 to fix that FPATH crash (re: 6ffab55)
Browse files Browse the repository at this point in the history
The name.c change in that commit was ineffective at fixing the
crash; I was misled by the crash being intermittent. The same crash
in path_unsetfpath() continues to occur sometimes when FPATH is set
in bin/package.

This commit special-cases FPATH in sh_reinit(), which is used to
(partially) reinitialise the shell after forking and before running
a shell script without a hashbang path. It is a bit of a hack, but
then again the entire sh_reinit() thing is a hack that I plan to
get rid of eventually; for now, it just needs to not crash.

src/cmd/ksh93/sh/name.c: sh_envnolocal():
- Revert the ineffective fix.

src/cmd/ksh93/sh/init.c: sh_reinit():
- Before starting reinit, save FPATH's value if it has the export
  attribute, then unset FPATH.
- After reinit is done, restore FPATH's value and export attribute
  if the value was saved. This should reinitialise sh.fpathdict.

Fingers crossed...
  • Loading branch information
McDutchie committed May 29, 2023
1 parent 8607657 commit 1e827c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,7 @@ int sh_reinit(char *argv[])
Namval_t *np,*npnext;
Dt_t *dp;
int nofree;
char *savfpath = NULL;
sh_onstate(SH_INIT);
sh.subshell = sh.realsubshell = sh.comsub = sh.curenv = sh.jobenv = sh.inuse_bits = sh.fn_depth = sh.dot_depth = 0;
sh.envlist = NULL;
Expand All @@ -1522,6 +1523,10 @@ int sh_reinit(char *argv[])
sfclose(sh.heredocs);
sh.heredocs = 0;
}
/* save FPATH and treat specially */
if(nv_isattr(FPATHNOD,NV_EXPORT))
savfpath = sh_strdup(nv_getval(FPATHNOD));
_nv_unset(FPATHNOD,NV_RDONLY);
/* Remove non-exported variables, first pass (see sh_envnolocal() in name.c) */
nv_scan(sh.var_tree,sh_envnolocal,NULL,NV_EXPORT,0);
nv_scan(sh.var_tree,sh_envnolocal,NULL,NV_ARRAY,NV_ARRAY);
Expand Down Expand Up @@ -1665,6 +1670,13 @@ int sh_reinit(char *argv[])
/* update $$, $PPID */
sh.ppid = sh.current_ppid;
sh.pid = sh.current_pid;
/* restore an exported FPATH */
if(savfpath)
{
nv_setattr(FPATHNOD,NV_EXPORT);
nv_putval(FPATHNOD,savfpath,0);
free(savfpath);
}
/* call user init function, if any */
if(sh.userinit)
(*sh.userinit)(&sh, 1);
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/ksh93/sh/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -2282,8 +2282,6 @@ void sh_envnolocal (Namval_t *np, void *data)
return;
if(np==L_ARGNOD)
return;
if(np==FPATHNOD)
return;
if(np == sh.namespace)
return;
if(nv_isref(np))
Expand Down

0 comments on commit 1e827c9

Please sign in to comment.