1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 19:52:20 +00:00

Another round of tweaks

Notable changes:
- Change a bunch of uses of memcmp(3) to compare strings (which can
  cause buffer read overflows) to strncmp(3).
- src/cmd/ksh93/include/name.h: Eliminate redundant Nambfp_f type.
  Replace with Shbltin_f type from libast's shcmd.h. Since that is
  not guaranteed to be included here, repeat the type definition
  here without fully defining the struct (which is valid in C).
This commit is contained in:
Martijn Dekker 2022-06-24 15:25:05 +01:00
parent da97587e9e
commit cc1689849e
21 changed files with 47 additions and 50 deletions

View file

@ -131,7 +131,7 @@ static int enuminfo(Opt_t* op, Sfio_t *out, const char *str, Optdisc_t *fp)
return(0);
if(strcmp(str,"default")==0)
sfprintf(out,"\b%s\b",ep->values[0]);
else if(memcmp(str,"last",4)==0)
else if(strncmp(str,"last",4)==0)
{
while(ep->values[++n])
;

View file

@ -355,7 +355,7 @@ static char* setdisc(Namval_t* np, const char* event, Namval_t* action, Namfun_t
for (i = 0; cp = disctab[i]; i++)
{
if (memcmp(event, cp, n))
if (strncmp(event, cp, n))
continue;
if (action == np)
action = sp->disc[i];

View file

@ -692,7 +692,7 @@ static const char *mapformat(Sffmt_t *fe)
const struct printmap *pm = Pmap;
while(pm->size>0)
{
if(pm->size==fe->n_str && memcmp(pm->name,fe->t_str,fe->n_str)==0)
if(pm->size==fe->n_str && strncmp(pm->name,fe->t_str,fe->n_str)==0)
return(pm->map);
pm++;
}

View file

@ -445,7 +445,7 @@ int test_unop(register int op,register const char *arg)
return(statb.st_gid==sh.groupid);
case 'a':
case 'e':
if(memcmp(arg,"/dev/",5)==0 && sh_open(arg,O_NONBLOCK))
if(strncmp(arg,"/dev/",5)==0 && sh_open(arg,O_NONBLOCK))
return(1);
return(permission(arg, F_OK));
case 'o':

View file

@ -370,7 +370,7 @@ static int sig_number(const char *string)
}
while(c);
stakseek(o);
if(memcmp(stakptr(o),"SIG",3)==0)
if(strncmp(stakptr(o),"SIG",3)==0)
{
sig = 1;
o += 3;

View file

@ -723,7 +723,7 @@ static int setall(char **argv,register int flag,Dt_t *troot,struct tdata *tp
if(!is_afunction(np))
np = 0;
}
else if(memcmp(name,".sh.math.",9)==0 && sh_mathstd(name+9))
else if(strncmp(name,".sh.math.",9)==0 && sh_mathstd(name+9))
continue;
}
}

View file

@ -1744,7 +1744,7 @@ int ed_histgen(Edit_t *ep,const char *pattern)
if(ep->hlist)
{
m = strlen(ep->hpat)-4;
if(memcmp(pattern,ep->hpat+2,m)==0)
if(strncmp(pattern,ep->hpat+2,m)==0)
{
n = strcmp(cp,ep->hpat)==0;
for(argv=av=(char**)ep->hlist,mp=ep->hfirst; mp;mp= mp->next)

View file

@ -949,7 +949,7 @@ int hist_match(register History_t *hp,off_t offset,char *string,int *coffset)
n = (int)strlen(string);
while(m > n)
{
if(*cp==*string && memcmp(cp,string,n)==0)
if(strncmp((char*)cp,string,n)==0)
{
if(coffset)
*coffset = (cp-first);

View file

@ -2323,7 +2323,7 @@ static int curline_search(Vi_t *vp, const char *string)
#endif /* SHOPT_MULTIBYTE */
for(dp=(char*)vp->u_space,dpmax=dp+strlen(dp)-len; dp<=dpmax; dp++)
{
if(*dp==*cp && memcmp(cp,dp,len)==0)
if(strncmp(cp,dp,len)==0)
return(dp-(char*)vp->u_space);
}
#if SHOPT_MULTIBYTE

View file

@ -31,7 +31,8 @@
#include <ast.h>
#include <cdt.h>
typedef int (*Nambfp_f)(int, char**, void*);
typedef struct Shbltin_s Shbltin_t;
typedef int (*Shbltin_f)(int, char**, Shbltin_t*);
/* Nodes can have all kinds of values */
union Value
@ -54,7 +55,7 @@ union Value
struct Ufunction *rp; /* shell user defined functions */
struct Namfun *funp; /* discipline pointer */
struct Namref *nrp; /* name reference */
Nambfp_f bfp; /* builtin entry point function pointer */
Shbltin_f bfp; /* builtin entry point function pointer */
};
#include "nval.h"
@ -149,7 +150,7 @@ struct Ufunction
#define is_abuiltin(n) (nv_isattr(n,NV_BLTIN|NV_INTEGER)==NV_BLTIN)
#define is_afunction(n) (nv_isattr(n,NV_FUNCTION|NV_REF)==NV_FUNCTION)
#define nv_funtree(n) ((n)->nvalue.rp->ptree)
#define funptr(n) ((Shbltin_f)(n)->nvalue.bfp)
#define funptr(n) ((n)->nvalue.bfp)
/* NAMNOD MACROS */
/* ... for attributes */

View file

@ -9194,11 +9194,6 @@ option of the
.B alias\^
command to correct this situation.
.PP
Some very old shell scripts contain a
.B ^
as a synonym for the pipe character
.BR | .
.PP
Using the
.B hist\^
built-in command within a compound command will cause the whole

View file

@ -98,7 +98,7 @@ int path_expand(const char *pattern, struct argnod **arghead)
gp->gl_suffix = sufstr;
gp->gl_intr = &sh.trapnote;
suflen = 0;
if(memcmp(pattern,"~(N",3)==0)
if(strncmp(pattern,"~(N",3)==0)
flags &= ~GLOB_NOCHECK;
glob(pattern, flags, 0, gp);
sh_sigcheck();

View file

@ -1093,7 +1093,7 @@ static char* get_math(register Namval_t* np, Namfun_t *fp)
mp = (Namval_t*)dtprev(sh.fun_tree,&fake);
while(mp=(Namval_t*)dtnext(sh.fun_tree,mp))
{
if(memcmp(mp->nvname,".sh.math.",9))
if(strncmp(mp->nvname,".sh.math.",9))
break;
if(first++)
sfputc(sh.strbuf,' ');
@ -1992,7 +1992,7 @@ Dt_t *sh_inittree(const struct shtable2 *name_vals)
}
np->nvenv = 0;
if(name_vals==(const struct shtable2*)shtab_builtins)
np->nvalue.bfp = (Nambfp_f)((struct shtable3*)tp)->sh_value;
np->nvalue.bfp = ((struct shtable3*)tp)->sh_value;
else
{
if(name_vals == shtab_variables)

View file

@ -493,6 +493,7 @@ int sh_lex(Lex_t* lp)
{
if(lp->lex.intest)
return(c);
/* '((' arithmetic comamnd */
lp->lexd.nest=1;
lp->lastline = sh.inlineno;
lp->lexd.lex_state = ST_NESTED;
@ -522,6 +523,7 @@ int sh_lex(Lex_t* lp)
n = 0;
else if(n==LPAREN)
{
/* process substitution <(...) or >(...) */
c |= SYMLPAR;
lp->lex.reservok = 1;
lp->lex.skipword = 0;
@ -1584,9 +1586,9 @@ static int comsub(register Lex_t *lp, int endtok)
}
if(sh_lexstates[ST_NAME][c]==S_BREAK)
{
if(memcmp(word,"case",4)==0)
if(strncmp(word,"case",4)==0)
lp->lex.incase=1;
else if(memcmp(word,"esac",4)==0)
else if(strncmp(word,"esac",4)==0)
lp->lex.incase=0;
}
skip:

View file

@ -1162,7 +1162,7 @@ Namval_t *nv_create(const char *name, Dt_t *root, int flags, Namfun_t *dp)
nofree = 0;
np = nq;
}
else if(memcmp(cp,"[0]",3))
else if(strncmp(cp,"[0]",3))
return(nq);
else
{
@ -1390,7 +1390,7 @@ Namval_t *nv_open(const char *name, Dt_t *root, int flags)
{
if(xp->root!=root)
continue;
if(*name==*xp->name && xp->namespace==sh.namespace && (flags&(NV_ARRAY|NV_NOSCOPE))==xp->flags && strncmp(xp->name,name,xp->len)==0 && (name[xp->len]==0 || name[xp->len]=='=' || name[xp->len]=='+'))
if(xp->namespace==sh.namespace && (flags&(NV_ARRAY|NV_NOSCOPE))==xp->flags && strncmp(xp->name,name,xp->len)==0 && (name[xp->len]==0 || name[xp->len]=='=' || name[xp->len]=='+'))
{
sh_stats(STAT_NVHITS);
np = xp->np;
@ -2253,7 +2253,7 @@ static int scanfilter(Namval_t *np, struct scan *sp)
if(sp->scanflags==NV_FUNCTION || sp->scanflags==(NV_NOFREE|NV_BINARY|NV_RAW))
{
int n = strlen(tp->mapname);
if(memcmp(np->nvname,tp->mapname,n) || np->nvname[n]!='.' || strchr(&np->nvname[n+1],'.'))
if(strncmp(np->nvname,tp->mapname,n) || np->nvname[n]!='.' || strchr(&np->nvname[n+1],'.'))
return(0);
}
else if((sp->scanflags==NV_UTOL||sp->scanflags==NV_LTOU) && (cp=(char*)nv_mapchar(np,0)) && strcmp(cp,tp->mapname))
@ -2420,7 +2420,7 @@ static void table_unset(register Dt_t *root, int flags, Dt_t *oroot)
{
int len = strlen(np->nvname);
npnext = (Namval_t*)dtnext(root,np);
while((nq=npnext) && memcmp(np->nvname,nq->nvname,len)==0 && nq->nvname[len]=='.')
while((nq=npnext) && strncmp(np->nvname,nq->nvname,len)==0 && nq->nvname[len]=='.')
{
_nv_unset(nq,flags);
@ -3584,7 +3584,7 @@ char *nv_name(register Namval_t *np)
{
char *name = nv_name(sh.namespace);
int n = strlen(name);
if(memcmp(np->nvname,name,n)==0 && np->nvname[n]=='.')
if(strncmp(np->nvname,name,n)==0 && np->nvname[n]=='.')
return(np->nvname+n+1);
}
#endif /* SHOPT_NAMESPACE */

View file

@ -159,7 +159,6 @@ void nv_putv(Namval_t *np, const char *value, int flags, register Namfun_t *nfp)
#define APPEND 2
#define UNASSIGN 3
#define LOOKUPN 4
#define BLOCKED ((Namval_t*)&nv_local)
struct vardisc
{
@ -1174,7 +1173,7 @@ Namval_t *sh_addbuiltin(const char *path, Shbltin_f bltin, void *extra)
np->nvfun = 0;
if(bltin)
{
np->nvalue.bfp = (Nambfp_f)bltin;
np->nvalue.bfp = bltin;
nv_onattr(np,NV_BLTIN|NV_NOFREE);
np->nvfun = (Namfun_t*)extra;
}

View file

@ -488,9 +488,9 @@ static Namval_t *create_type(Namval_t *np,const char *name,int flag,Namfun_t *fp
{
char *base = (char*)np-sizeof(Dtlink_t);
int m=strlen(np->nvname);
while((nq=nv_namptr(base,++i)) && memcmp(nq->nvname,np->nvname,m)==0)
while((nq=nv_namptr(base,++i)) && strncmp(nq->nvname,np->nvname,m)==0)
{
if(nq->nvname[m]=='.' && memcmp(name,&nq->nvname[m+1],n)==0 && nq->nvname[m+n+1]==0)
if(nq->nvname[m]=='.' && strncmp(name,&nq->nvname[m+1],n)==0 && nq->nvname[m+n+1]==0)
goto found;
}
nq = 0;
@ -498,7 +498,7 @@ static Namval_t *create_type(Namval_t *np,const char *name,int flag,Namfun_t *fp
else for(i=0; i < dp->numnodes; i++)
{
nq = nv_namptr(dp->nodes,i);
if((n==0||memcmp(name,nq->nvname,n)==0) && nq->nvname[n]==0)
if((n==0||strncmp(name,nq->nvname,n)==0) && nq->nvname[n]==0)
{
while(nv_isref(nq))
nq = nq->nvalue.nrp->np;
@ -516,7 +516,7 @@ found:
{
if(name[n]!='=') for(i=0; i < dp->ndisc; i++)
{
if((memcmp(name,dp->names[i],n)==0) && dp->names[i][n]==0)
if((strncmp(name,dp->names[i],n)==0) && dp->names[i][n]==0)
return(nq);
}
errormsg(SH_DICT,ERROR_exit(1),e_notelem,n,name,nv_name(np));
@ -660,7 +660,7 @@ static int typeinfo(Opt_t* op, Sfio_t *out, const char *str, Optdisc_t *fp)
{
sfprintf(out,"\t[+%s?%s.\n",nq->nvname,tp->nvname);
n = strlen(nq->nvname);
while((cp=nv_namptr(dp->nodes,i+1)->nvname) && memcmp(cp,nq->nvname,n)==0 && cp[n]=='.')
while((cp=nv_namptr(dp->nodes,i+1)->nvname) && strncmp(cp,nq->nvname,n)==0 && cp[n]=='.')
i++;
}
else
@ -741,7 +741,7 @@ static int std_disc(Namval_t *mp, Namtype_t *pp)
}
return(0);
found:
if(memcmp(sp=mp->nvname,NV_CLASS,sizeof(NV_CLASS)-1)==0)
if(strncmp(sp=mp->nvname,NV_CLASS,sizeof(NV_CLASS)-1)==0)
sp += sizeof(NV_CLASS);
sp += strlen(pp->fun.type->nvname)+1;
if(sp == cp)
@ -749,7 +749,7 @@ found:
else for(i=1; i < pp->numnodes; i++)
{
nq = nv_namptr(pp->nodes,i);
if(memcmp(nq->nvname, sp, cp-sp-1)==0)
if(strncmp(nq->nvname, sp, cp-sp-1)==0)
{
np = nq;
break;
@ -849,7 +849,7 @@ Namval_t *nv_mktype(Namval_t **nodes, int numnodes)
if(!std_disc(np, (Namtype_t*)0))
{
size += strlen(np->nvname+m)+1;
if(memcmp(np->nvname,NV_CLASS,sizeof(NV_CLASS)-1)==0)
if(strncmp(np->nvname,NV_CLASS,sizeof(NV_CLASS)-1)==0)
size -= sizeof(NV_CLASS);
nd++;
}
@ -894,7 +894,7 @@ Namval_t *nv_mktype(Namval_t **nodes, int numnodes)
else
size += n + dp->numnodes*(strlen(&np->nvname[m])+1);
n = strlen(np->nvname);
while((i+1) < numnodes && (cp=nodes[i+1]->nvname) && memcmp(cp,np->nvname,n)==0 && cp[n]=='.')
while((i+1) < numnodes && (cp=nodes[i+1]->nvname) && strncmp(cp,np->nvname,n)==0 && cp[n]=='.')
i++;
}
else if(nv_isattr(np,NV_REF))
@ -939,7 +939,7 @@ Namval_t *nv_mktype(Namval_t **nodes, int numnodes)
if(is_afunction(np))
{
sp = np->nvname+m;
if(memcmp(np->nvname,NV_CLASS,sizeof(NV_CLASS)-1)==0)
if(strncmp(np->nvname,NV_CLASS,sizeof(NV_CLASS)-1)==0)
sp += sizeof(NV_CLASS);
if(!std_disc(np, pp))
{
@ -1098,7 +1098,7 @@ Namval_t *nv_mktype(Namval_t **nodes, int numnodes)
cp = strcopy(cp,nr->nvname);
*cp++ = 0;
}
while((i+1) < numnodes && (cname=&nodes[i+1]->nvname[m]) && memcmp(cname,&np->nvname[m],n)==0 && cname[n]=='.')
while((i+1) < numnodes && (cname=&nodes[i+1]->nvname[m]) && strncmp(cname,&np->nvname[m],n)==0 && cname[n]=='.')
{
int j=kfirst;
nv_unset(np);

View file

@ -188,11 +188,11 @@ static void check_typedef(struct comnod *tp, char intypeset)
struct argnod *ap = tp->comarg;
while(ap = ap->argnxt.ap)
{
if(!(ap->argflag&ARG_RAW) || memcmp(ap->argval,"--",2))
if(!(ap->argflag&ARG_RAW) || strncmp(ap->argval,"--",2))
break;
if(sh_isoption(SH_NOEXEC))
typeset_order(ap->argval,tp->comline);
if(memcmp(ap->argval,"-T",2)==0)
if(strncmp(ap->argval,"-T",2)==0)
{
if(ap->argval[2])
cp = ap->argval+2;
@ -220,11 +220,11 @@ static void check_typedef(struct comnod *tp, char intypeset)
return;
cp = argv[opt_info.index];
}
else while((cp = *argv++) && memcmp(cp,"--",2))
else while((cp = *argv++) && strncmp(cp,"--",2))
{
if(sh_isoption(SH_NOEXEC))
typeset_order(cp,tp->comline);
if(memcmp(cp,"-T",2)==0)
if(strncmp(cp,"-T",2)==0)
{
if(cp[2])
cp = cp+2;
@ -876,7 +876,7 @@ static Shnode_t *funct(Lex_t *lexp)
}
nargs = argv-argv0;
size += sizeof(struct dolnod)+(nargs+ARG_SPARE)*sizeof(char*);
if(sh.shcomp && memcmp(".sh.math.",t->funct.functnam,9)==0)
if(sh.shcomp && strncmp(".sh.math.",t->funct.functnam,9)==0)
{
Namval_t *np= nv_open(t->funct.functnam,sh.fun_tree,NV_ADD|NV_VARNAME);
np->nvalue.rp = new_of(struct Ufunction,sh.funload?sizeof(Dtlink_t):0);

View file

@ -1581,7 +1581,7 @@ static int checkdotpaths(Pathcomp_t *first, Pathcomp_t* old,Pathcomp_t *pp, int
}
*cp = 0;
m = ep ? (ep-sp) : 0;
if(m==0 || m==6 && memcmp((void*)sp,(void*)"FPATH=",m)==0)
if(m==0 || m==6 && strncmp(sp,"FPATH=",m)==0)
{
if(first)
{
@ -1591,7 +1591,7 @@ static int checkdotpaths(Pathcomp_t *first, Pathcomp_t* old,Pathcomp_t *pp, int
path_addcomp(first,old,stakptr(offset),PATH_FPATH|PATH_BFPATH);
}
}
else if(m==11 && memcmp((void*)sp,(void*)"PLUGIN_LIB=",m)==0)
else if(m==11 && strncmp(sp,"PLUGIN_LIB=",m)==0)
{
if(pp->bbuf)
free(pp->bbuf);

View file

@ -2,7 +2,7 @@
# #
# This software is part of the ast package #
# Copyright (c) 1985-2011 AT&T Intellectual Property #
# Copyright (c) 2020-2021 Contributors to ksh 93u+m #
# Copyright (c) 2020-2022 Contributors to ksh 93u+m #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #

View file

@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1985-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2021 Contributors to ksh 93u+m *
* Copyright (c) 2020-2022 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
@ -130,7 +130,7 @@ str2inet(register char* s, char* prot, struct sockaddr_in* addr)
unsigned long a = 0;
unsigned short p = 0;
if (!memcmp(s, "local/", 6))
if (!strncmp(s, "local/", 6))
{
a = INADDR_LOOPBACK;
n = 4;