1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Fix various compiler warnings and minor issues (#362)

List of changes:
- Fixed some -Wuninitialized warnings and removed some unused variables.

- Removed the unused extern for B_login (re: d8eba9d1).

- The libcmd builtins and the vmalloc memfatal function now handle
  memory errors with 'ERROR_SYSTEM|ERROR_PANIC' for consistency with how
  ksh itself handles out of memory errors.

- Added usage of UNREACHABLE() where it was missing from error handling.

- Extend many variables from short to int to prevent overflows (most
  variables involve file descriptors).

- Backported a ksh2020 patch to fix unused value Coverity issues
  (https://github.com/att/ast/pull/740).

- Note in src/cmd/ksh93/README that ksh compiles with Cygwin on
  Windows 10 and Windows 11, albeit with many test failures.

- Add comments to detail some sections of code. Extensive list of
  commits related to this change:
  ca2443b5, 7e7f1372, 2db9953a, 7003aba4, 6f50ff64, b1a41311,
  222515bf, a0dcdeea, 0aa9e03f, 61437b27, 352e68da, 88e8fa67,
  bc8b36fa, 6e515f1d, 017d088c, 035a4cb3, 588a1ff7, 6d63b57d,
  a2f13c19, 794d1c86, ab98ec65, 1026006d

- Removed a lot of dead ifdef code.

- edit/emacs.c: Hide an assignment to avoid a -Wunused warning. (See
  also https://github.com/att/ast/pull/753, which removed the assignment
  because ksh2020 removed the !SHOPT_MULTIBYTE code.)

- sh/nvdisc.c: The sh_newof macro cannot return a null pointer because
  it will instead cause the shell to exit if memory cannot be allocated.
  That makes the if statement here a no-op, so remove it.

- sh/xec.c: Fixed one unused variable warning in sh_funscope().

- sh/xec.c: Remove a fallthrough comment added in commit ed478ab7
  because the TFORK code doesn't fall through (GCC also produces no
  -Wimplicit-fallthrough warning here).

- data/builtins.c: The cd and pwd man pages state that these builtins
  default to -P if PATH_RESOLVE is 'physical', which isn't accurate:
     $ /opt/ast/bin/getconf PATH_RESOLVE
     physical
     $ mkdir /tmp/dir; ln -s /tmp/dir /tmp/sym
     $ cd /tmp/sym
     $ pwd
     /tmp/sym
     $ cd -P /tmp/sym
     $ pwd
     /tmp/dir
  The behavior described by these man pages isn't specified in the ksh
  man page or by POSIX, so to avoid changing these builtin's behavior
  the inaccurate PATH_RESOLVE information has been removed.

- Mamfiles: Preserve multi-line errors by quoting the $x variable.
  This fix was backported from 93v-.
  (See also <a7e9cc82>.)

- sh/subshell.c: Remove set but not used sp->errcontext variable.
This commit is contained in:
Johnothan King 2021-12-07 22:51:38 -08:00 committed by Martijn Dekker
parent b3050769ea
commit beccb93fd4
66 changed files with 148 additions and 265 deletions

View file

@ -642,7 +642,7 @@ void sh_printopts(Shopt_t oflags,register int mode, Shopt_t *mask)
*/
char **sh_argbuild(Shell_t *shp,int *nargs, const struct comnod *comptr,int flag)
{
register struct argnod *argp;
register struct argnod *argp=0;
struct argnod *arghead=0;
shp->xargmin = 0;
{

View file

@ -272,7 +272,7 @@ static Sfdouble_t arith(const char **ptr, struct lval *lvalue, int type, Sfdoubl
c = mbchar(str);
if(isaletter(c))
{
register Namval_t *np;
register Namval_t *np=0;
int dot=0;
while(1)
{
@ -297,7 +297,6 @@ static Sfdouble_t arith(const char **ptr, struct lval *lvalue, int type, Sfdoubl
int off=stktell(shp->stk);
int fsize = str- (char*)(*ptr);
const struct mathtab *tp;
Namval_t *np;
c = **ptr;
lvalue->fun = 0;
sfprintf(shp->stk,".sh.math.%.*s%c",fsize,*ptr,0);

View file

@ -1775,7 +1775,7 @@ void *nv_associative(register Namval_t *np,const char *sp,int mode)
if((mode&NV_ADD) && nv_type(np))
nv_arraychild(np,mp,0);
if(sh.subshell)
np = sh_assignok(np,1);
sh_assignok(np,1);
/*
* For enum types (NV_UINT16 with discipline ENUM_disc), nelem should not
* not increased or 'unset' will fail to completely unset such an array.

View file

@ -74,7 +74,7 @@ void sh_deparse(Sfio_t *out, const Shnode_t *t,int tflags)
*/
static void p_tree(register const Shnode_t *t,register int tflags)
{
register char *cp;
register char *cp=0;
int save = end_line;
int needbrace = (tflags&NEED_BRACE);
int procsub = (tflags&PROCSUBST);
@ -390,7 +390,7 @@ static void p_keyword(const char *word,int flag)
static void p_arg(register const struct argnod *arg,register int endchar,int opts)
{
register const char *cp;
register int flag;
register int flag=0;
do
{
if(!arg->argnxt.ap)

View file

@ -1063,16 +1063,6 @@ static char *setdisc_any(Namval_t *np, const char *event, Namval_t *action, Namf
static const Namdisc_t SH_MATH_disc = { 0, 0, get_math, 0, setdisc_any, create_math, };
#if SHOPT_NAMESPACE
static char* get_nspace(Namval_t* np, Namfun_t *fp)
{
if(sh.namespace)
return(nv_name(sh.namespace));
return((char*)np->nvalue.cp);
}
static const Namdisc_t NSPACE_disc = { 0, 0, get_nspace };
#endif /* SHOPT_NAMESPACE */
#ifdef _hdr_locale
static const Namdisc_t LC_disc = { sizeof(Namfun_t), put_lang };
#endif /* _hdr_locale */
@ -1236,9 +1226,6 @@ Shell_t *sh_init(register int argc,register char *argv[], Shinit_f userinit)
shgd->lim.clk_tck = getconf("CLK_TCK");
shgd->lim.arg_max = getconf("ARG_MAX");
shgd->lim.child_max = getconf("CHILD_MAX");
shgd->lim.ngroups_max = getconf("NGROUPS_MAX");
shgd->lim.posix_version = getconf("VERSION");
shgd->lim.posix_jobcontrol = getconf("JOB_CONTROL");
if(shgd->lim.arg_max <=0)
shgd->lim.arg_max = ARG_MAX;
if(shgd->lim.child_max <=0)
@ -1310,7 +1297,7 @@ Shell_t *sh_init(register int argc,register char *argv[], Shinit_f userinit)
sh_siginit(shp);
stakinstall(NIL(Stak_t*),nomemory);
/* set up memory for name-value pairs */
shp->init_context = nv_init(shp);
shp->init_context = nv_init(shp);
/* initialize shell type */
if(argc>0)
{
@ -1883,10 +1870,7 @@ Dt_t *sh_inittree(Shell_t *shp,const struct shtable2 *name_vals)
n++;
np = (Namval_t*)sh_calloc(n,sizeof(Namval_t));
if(!shgd->bltin_nodes)
{
shgd->bltin_nodes = np;
shgd->bltin_nnodes = n;
}
else if(name_vals==(const struct shtable2*)shtab_builtins)
{
shgd->bltin_cmds = np;

View file

@ -1135,7 +1135,6 @@ int sh_redirect(Shell_t *shp,struct ionod *iop, int flag)
int r, indx = shp->topfd, perm= -1;
char *tname=0, *after="", *trace = shp->st.trap[SH_DEBUGTRAP];
Namval_t *np=0;
int isstring = shp->subshell?(sfset(sfstdout,0,0)&SF_STRING):0;
if(flag==2 && !sh_isoption(SH_POSIX))
clexec = 1;
@ -1189,6 +1188,7 @@ int sh_redirect(Shell_t *shp,struct ionod *iop, int flag)
}
if((iof&IOPROCSUB) && !(iof&IOLSEEK))
{
/* handle process substitution passed to redirection */
struct argnod *ap = (struct argnod*)stakalloc(ARGVAL+strlen(iop->ioname));
memset(ap, 0, ARGVAL);
if(iof&IOPUT)
@ -1450,7 +1450,6 @@ int sh_redirect(Shell_t *shp,struct ionod *iop, int flag)
else
{
regex_t *rp;
extern const char e_notimp[];
if(!(r&IOREAD))
{
message = e_noread;
@ -2143,6 +2142,8 @@ static int io_prompt(Shell_t *shp,Sfio_t *iop,register int flag)
flag = 0;
if(flag==0)
return(sfsync(sfstderr));
/* Temporarily disable 'set -o notify' while expanding the prompt to avoid
possible crashes (https://github.com/ksh93/ksh/issues/103). */
was_ttywait_on = sh_isstate(SH_TTYWAIT);
sh_offstate(SH_TTYWAIT);
sfflags = sfset(sfstderr,SF_SHARE|SF_PUBLIC|SF_READ,0);
@ -2165,7 +2166,7 @@ static int io_prompt(Shell_t *shp,Sfio_t *iop,register int flag)
}
#endif /* TIOCLBIC */
cp = sh_mactry(shp,nv_getval(sh_scoped(shp,PS1NOD)));
shp->exitval = 0;
shp->exitval = 0; /* avoid sending a signal on termination */
for(;c= *cp;cp++)
{
if(c==HIST_CHAR)
@ -2199,7 +2200,7 @@ done:
if(*shp->prompt && (endprompt=(char*)sfreserve(sfstderr,0,0)))
*endprompt = 0;
if(was_ttywait_on)
sh_onstate(SH_TTYWAIT);
sh_onstate(SH_TTYWAIT); /* re-enable 'set -o notify' */
sfset(sfstderr,sfflags&SF_READ|SF_SHARE|SF_PUBLIC,1);
return(sfsync(sfstderr));
}

View file

@ -317,8 +317,7 @@ int job_reap(register int sig)
{
if(!(flags&WNOHANG) && !sh.intrap && job.pwlist)
{
if(!was_ttywait_on)
sh_onstate(SH_TTYWAIT);
sh_onstate(SH_TTYWAIT);
if(waitevent && (*waitevent)(-1,-1L,0))
flags |= WNOHANG;
}
@ -1020,8 +1019,8 @@ int job_kill(register struct process *pw,register int sig)
#endif /* SIGTSTP */
job_lock();
errno = ECHILD;
if(pw==0)
goto error;
if(!pw)
goto error; /* not an actual shell job */
shp = pw->p_shp;
pid = pw->p_pid;
if(by_number)

View file

@ -1662,6 +1662,7 @@ done:
lp->assignok = (endchar(lp)==RBRACT?assignok:0);
if(lp->heredoc && !inheredoc)
{
/* here-document isn't fully contained in command substitution */
errormsg(SH_DICT,ERROR_exit(SYNBAD),e_lexsyntax5,lp->sh->inlineno,lp->heredoc->ioname);
UNREACHABLE();
}

View file

@ -2161,7 +2161,7 @@ static void comsubst(Mac_t *mp,register Shnode_t* t, int type)
{
/* special case $(<file) and $(<#file) */
register int fd;
int r;
int r=0;
struct checkpt buff;
struct ionod *ip=0;
sh_pushcontext(mp->shp,&buff,SH_JMPIO);

View file

@ -842,11 +842,8 @@ static void *newnode(const char *name)
{
register int s;
register Namval_t *np = sh_newof(0,Namval_t,1,s=strlen(name)+1);
if(np)
{
np->nvname = (char*)np+sizeof(Namval_t);
memcpy(np->nvname,name,s);
}
np->nvname = (char*)np+sizeof(Namval_t);
memcpy(np->nvname,name,s);
return((void*)np);
}
@ -1212,6 +1209,7 @@ Namval_t *sh_addbuiltin(const char *path, Shbltin_f bltin, void *extra)
{
if(nv_isattr(np,BLT_SPC))
{
/* builtin(1) cannot delete special builtins */
errormsg(SH_DICT,ERROR_exit(1),"cannot delete: %s%s",name,is_spcbuiltin);
UNREACHABLE();
}

View file

@ -2046,7 +2046,7 @@ unsigned long kiaentity(Lex_t *lexp,const char *name,int len,int type,int first,
else
sfputr(stkp,name,0);
}
sfputc(stkp,'\0');
sfputc(stkp,'\0'); /* terminate name while writing database output */
np = nv_search(stakptr(offset),lexp->entity_tree,NV_ADD);
stkseek(stkp,offset);
np->nvalue.i = pkind;

View file

@ -1573,7 +1573,9 @@ static int path_chkpaths(Shell_t *shp,Pathcomp_t *first, Pathcomp_t* old,Pathcom
if((fd=open(stakptr(offset),O_RDONLY))>=0)
{
fstat(fd,&statb);
if (!S_ISREG(statb.st_mode)) {
if(!S_ISREG(statb.st_mode))
{
/* .paths cannot be a directory */
close(fd);
return 0;
}

View file

@ -115,7 +115,7 @@ int main(int argc, char *argv[])
argc -= opt_info.index;
if(argc==0 && tty_check(0))
{
errormsg(SH_DICT,ERROR_exit(0),"refusing to read script from terminal",cp);
errormsg(SH_DICT,ERROR_exit(0),"refusing to read script from terminal");
error_info.errors++;
}
if(error_info.errors || argc>2)
@ -161,7 +161,7 @@ int main(int argc, char *argv[])
if(vflag)
sh_onoption(SH_VERBOSE);
if(!dflag)
sfwrite(out,header,sizeof(header));
sfwrite(out,header,sizeof(header)); /* write binary shcomp header */
sh_offoption(SH_MULTILINE);
shp->inlineno = 1;
#if SHOPT_BRACEPAT
@ -174,6 +174,7 @@ int main(int argc, char *argv[])
if(t = (Shnode_t*)sh_parse(shp,in,0))
{
if((t->tre.tretyp&(COMMSK|COMSCAN))==0 && t->com.comnamp && strcmp(nv_name((Namval_t*)t->com.comnamp),"alias")==0)
/* Create aliases found in the script to prevent syntax errors */
sh_exec(t,0);
if(!dflag && sh_tdump(out,t) < 0)
{

View file

@ -73,20 +73,18 @@ static struct subshell
Shell_t *shp; /* shell interpreter */
struct subshell *prev; /* previous subshell data */
struct subshell *pipe; /* subshell where output goes to pipe on fork */
Dt_t *var; /* variable table at time of subshell */
struct Link *svar; /* save shell variable table */
Dt_t *sfun; /* function scope for subshell */
Dt_t *strack;/* tracked alias scope for subshell */
Pathcomp_t *pathlist; /* for PATH variable */
struct Error_context_s *errcontext;
Shopt_t options;/* save shell options */
pid_t subpid; /* child process id */
Sfio_t* saveout;/* saved standard output */
char *pwd; /* present working directory */
void *jobs; /* save job info */
mode_t mask; /* saved umask */
short tmpfd; /* saved tmp file descriptor */
short pipefd; /* read fd if pipe is created */
int tmpfd; /* saved tmp file descriptor */
int pipefd; /* read fd if pipe is created */
char jobcontrol;
char monitor;
unsigned char fdstatus;
@ -519,8 +517,6 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_t *t, volatile int flags, int comsub)
sp->shp = shp;
sp->sig = 0;
subshell_data = sp;
sp->errcontext = &buff.err;
sp->var = shp->var_tree;
sp->options = shp->options;
sp->jobs = job_subsave();
sp->subdup = shp->subdup;

View file

@ -334,7 +334,10 @@ int eaccess(register const char *name, register int mode)
}
groups = (gid_t*)malloc((maxgroups+1)*sizeof(gid_t));
if(!groups)
error(ERROR_PANIC,"out of memory");
{
error(ERROR_SYSTEM|ERROR_PANIC,"out of memory");
UNREACHABLE();
}
n = getgroups(maxgroups,groups);
while(--n >= 0)
{

View file

@ -175,7 +175,6 @@ static int subpipe[3],subdup,tsetio,usepipe;
static int iousepipe(Shell_t *shp)
{
int fd=sffileno(sfstdout),i,err=errno;
if(usepipe)
{
usepipe++;
@ -191,7 +190,6 @@ static int iousepipe(Shell_t *shp)
void sh_iounpipe(Shell_t *shp)
{
int fd=sffileno(sfstdout),n,err=errno;
char buff[SF_BUFSIZE];
if(!usepipe)
return;
@ -1542,7 +1540,7 @@ int sh_exec(register const Shnode_t *t, int flags)
fifo_cleanup();
#endif
if(shp->topfd > topfd && !(shp->subshell && (np==SYSEXEC || np==SYSREDIR)))
sh_iorestore(shp,topfd,jmpval);
sh_iorestore(shp,topfd,jmpval); /* avoid leaking unused file descriptors */
exitset();
break;
}
@ -1812,7 +1810,6 @@ int sh_exec(register const Shnode_t *t, int flags)
sh_done(shp,0);
}
}
/* FALLTHROUGH */
case TSETIO:
{
@ -2899,7 +2896,6 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid)
if(parent)
{
int myjob,waitall=job.waitall;
shp->gd->nforks++;
if(job.toclear)
job_clear();
job.waitall = waitall;
@ -2956,7 +2952,6 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid)
shp->outpipepid = ((flags&FPOU)?shgd->current_pid:0);
if(shp->trapnote&SH_SIGTERM)
sh_exit(SH_EXITSIG|SIGTERM);
shp->gd->nforks=0;
timerdel(NIL(void*));
#ifdef JOBS
if(sh_isstate(SH_MONITOR))
@ -3114,6 +3109,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg)
Dt_t *last_root = shp->last_root;
Shopt_t options;
options = shp->options;
NOT_USED(argn);
if(shp->fn_depth==0)
shp->glob_options = shp->options;
else
@ -3483,7 +3479,6 @@ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,in
{
static pid_t spawnpid;
static int savetype;
static int savejobid;
struct checkpt *buffp = (struct checkpt*)stkalloc(shp->stk,sizeof(struct checkpt));
int otype=0, jmpval,jobfork=0;
volatile int scope=0, sigwasset=0;
@ -3636,7 +3631,6 @@ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,in
if(grp==1)
job.curpgid = spawnpid;
#endif /* JOBS */
savejobid = *jobid;
if(otype)
return(0);
}