From e19063f18cb3eae022cb7f60009f5bbe06a3179e Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sat, 30 Nov 2024 21:47:06 +0000 Subject: [PATCH] nvalue sanitisation part 5: remove 'union Value' (re: 0686c708) The last uses of union Value were variables in shell functions in array.c, name.c and nvdisc.c. These are now converted to use simple void pointers, with typecasts where necessary for correct dereference when reading or assigning values. In most functions, 'union Value *up;' needed to be replaced with a pointer to a pointer, 'void **vpp', because up was not always dereferenced. However, in nv_arrayisset() and nv_getnum(), 'up' was always dereferenced, so a further simplification to 'void *vp' was possible, removing one layer of dereference. In array.c, the 'val' member of struct index_array is now an array of void pointers. With that, all 'union Value' usage is gone, so the union definition is deleted from name.h. --- src/cmd/ksh93/include/name.h | 19 ---- src/cmd/ksh93/sh/array.c | 161 +++++++++++++++---------------- src/cmd/ksh93/sh/name.c | 181 +++++++++++++++++------------------ src/cmd/ksh93/sh/nvdisc.c | 8 +- 4 files changed, 174 insertions(+), 195 deletions(-) diff --git a/src/cmd/ksh93/include/name.h b/src/cmd/ksh93/include/name.h index d557c3937e20..e1611bdb2782 100644 --- a/src/cmd/ksh93/include/name.h +++ b/src/cmd/ksh93/include/name.h @@ -24,25 +24,6 @@ #include #include - -/* Nodes can have all kinds of values */ -union Value -{ - const char *cp; - int *ip; - int32_t *lp; - pid_t *pidp; - Sflong_t *llp; /* for long long arithmetic */ - int16_t *sp; - double *dp; /* for floating point arithmetic */ - Sfdouble_t *ldp; /* for long floating point arithmetic */ - struct Namval *np; /* for Namval_t node */ - union Value *up; /* for indirect node */ - struct Ufunction *rp; /* shell user defined functions */ - struct Namref *nrp; /* name reference */ - void *bfp; /* pointer to built-in command's entry function (typecast to Shbltin_f) */ -}; - #include "nval.h" /* used for arrays */ diff --git a/src/cmd/ksh93/sh/array.c b/src/cmd/ksh93/sh/array.c index d2f8c943ba18..69ca9e6cae31 100644 --- a/src/cmd/ksh93/sh/array.c +++ b/src/cmd/ksh93/sh/array.c @@ -40,12 +40,12 @@ struct index_array { - Namarr_t header; - void *xp; /* if set, subscripts will be converted */ - int cur; /* index of current element */ - int maxi; /* maximum index for array */ - unsigned char *bits; /* bit array for child subscripts */ - union Value val[1]; /* array of value holders */ + Namarr_t header; + void *xp; /* if set, subscripts will be converted */ + int cur; /* index of current element */ + int maxi; /* maximum index for array */ + unsigned char *bits; /* bit array for child subscripts */ + void *val[1]; /* array of value holders */ }; struct assoc_array @@ -138,9 +138,9 @@ static int array_covered(Namval_t *np, struct index_array *ap) struct index_array *aq = (struct index_array*)ap->header.scope; if(!ap->header.fun && aq) #if SHOPT_FIXEDARRAY - return (ap->header.fixed || ((ap->curmaxi) && aq->val[ap->cur].cp)); + return (ap->header.fixed || ((ap->curmaxi) && aq->val[ap->cur])); #else - return ((ap->curmaxi) && aq->val[ap->cur].cp); + return ((ap->curmaxi) && aq->val[ap->cur]); #endif /* SHOPT_FIXEDARRAY */ return 0; } @@ -183,20 +183,20 @@ int array_maxindex(Namval_t *np) int i = ap->maxi; if(is_associative(ap)) return -1; - while(i>0 && ap->val[--i].cp==0); + while(i>0 && !ap->val[--i]); return i+1; } -static union Value *array_getup(Namval_t *np, Namarr_t *arp, int update) +static void **array_getup(Namval_t *np, Namarr_t *arp, int update) { struct index_array *ap = (struct index_array*)arp; - union Value *up; + void **vpp; /* pointer to value pointer */ #if SHOPT_FIXEDARRAY struct fixed_array *fp; #endif /* SHOPT_FIXEDARRAY */ int nofree=0; if(!arp) - return (union Value*)&np->nvalue; + return &np->nvalue; if(is_associative(ap)) { Namval_t *mp; @@ -204,22 +204,22 @@ static union Value *array_getup(Namval_t *np, Namarr_t *arp, int update) if(mp) { nofree = nv_isattr(mp,NV_NOFREE); - up = (union Value*)&mp->nvalue; + vpp = &mp->nvalue; } else - return (union Value*)((*arp->fun)(np,NULL,0)); + return (*arp->fun)(np,NULL,0); } #if SHOPT_FIXEDARRAY else if(fp = (struct fixed_array*)arp->fixed) { if(!fp->data) array_fixed_setdata(np,arp,fp); - up = (union Value*)&np->nvalue; + vpp = &np->nvalue; if(fp->ptr) - up->cp = *(((char**)fp->data)+fp->curi); + *vpp = *(((char**)fp->data) + fp->curi); else - up->cp = fp->data+fp->size*fp->curi; - } + *vpp = fp->data + fp->size * fp->curi; + } #endif /* SHOPT_FIXEDARRAY */ else { @@ -228,7 +228,7 @@ static union Value *array_getup(Namval_t *np, Namarr_t *arp, int update) errormsg(SH_DICT,ERROR_exit(1),e_subscript,nv_name(np)); UNREACHABLE(); } - up = &(ap->val[ap->cur]); + vpp = &(ap->val[ap->cur]); nofree = array_isbit(ap->bits,ap->cur,ARRAY_NOFREE); } if(update) @@ -238,19 +238,19 @@ static union Value *array_getup(Namval_t *np, Namarr_t *arp, int update) else nv_offattr(np,NV_NOFREE); } - return up; + return vpp; } int nv_arrayisset(Namval_t *np, Namarr_t *arp) { struct index_array *ap = (struct index_array*)arp; - union Value *up; + void *vp; /* value pointer */ if(is_associative(ap)) return (np = nv_opensub(np)) && !nv_isnull(np); if(ap->cur >= ap->maxi) return 0; - up = &(ap->val[ap->cur]); - if(up->cp==Empty) + vp = ap->val[ap->cur]; + if(vp==Empty) { Namfun_t *fp = &arp->hdr; for(fp=fp->next; fp; fp = fp->next) @@ -259,18 +259,18 @@ int nv_arrayisset(Namval_t *np, Namarr_t *arp) return 1; } } - return up->cp && up->cp!=Empty; + return vp && vp!=Empty; } /* - * Get the Value pointer for an array. + * Get the value pointer for an array. * Delete space as necessary if flag is ARRAY_DELETE * After the lookup is done the last @ or * subscript is incremented */ static Namval_t *array_find(Namval_t *np,Namarr_t *arp, int flag) { struct index_array *ap = (struct index_array*)arp; - union Value *up; + void **vpp; /* pointer to value pointer */ Namval_t *mp; int wasundef; #if SHOPT_FIXEDARRAY @@ -314,7 +314,7 @@ static Namval_t *array_find(Namval_t *np,Namarr_t *arp, int flag) { mp = (Namval_t*)((*arp->fun)(np,NULL,NV_ACURRENT)); if(!mp) - up = (union Value*)∓ + vpp = (void**)∓ else if(nv_isarray(mp)) { if(wasundef) @@ -323,10 +323,10 @@ static Namval_t *array_find(Namval_t *np,Namarr_t *arp, int flag) } else { - up = (union Value*)&mp->nvalue; + vpp = &mp->nvalue; if(nv_isvtree(mp)) { - if(!up->cp && flag==ARRAY_ASSIGN) + if(!*vpp && flag==ARRAY_ASSIGN) { nv_arraychild(np,mp,0); ap->header.nelem++; @@ -369,8 +369,8 @@ static Namval_t *array_find(Namval_t *np,Namarr_t *arp, int flag) errormsg(SH_DICT,ERROR_exit(1),e_subscript,nv_name(np)); UNREACHABLE(); } - up = &(ap->val[ap->cur]); - if((!up->cp||up->cp==Empty) && nv_type(np) && nv_isvtree(np)) + vpp = &(ap->val[ap->cur]); + if((!*vpp || *vpp==Empty) && nv_type(np) && nv_isvtree(np)) { char *cp; if(!ap->header.table) @@ -381,15 +381,15 @@ static Namval_t *array_find(Namval_t *np,Namarr_t *arp, int flag) mp->nvmeta = np; nv_arraychild(np,mp,0); } - if(up->np && array_isbit(ap->bits,ap->cur,ARRAY_CHILD)) + if(*vpp && array_isbit(ap->bits,ap->cur,ARRAY_CHILD)) { - if(wasundef && nv_isarray(up->np)) - nv_putsub(up->np,NULL,ARRAY_UNDEF); - return up->np; + if(wasundef && nv_isarray((Namval_t*)*vpp)) + nv_putsub(*vpp,NULL,ARRAY_UNDEF); + return *vpp; } } - np->nvalue = (void*)up->cp; - if(!up->cp) + np->nvalue = *vpp; + if(!*vpp) { char *xp = nv_setdisc(np,"get",np,(Namfun_t*)np); if(flag!=ARRAY_ASSIGN) @@ -503,7 +503,7 @@ static Namfun_t *array_clone(Namval_t *np, Namval_t *mp, int flags, Namfun_t *fp { mq->nvalue = NULL; if(!is_associative(ap)) - ar->val[ar->cur].np = mq; + ar->val[ar->cur] = mq; nv_clone(nq,mq,flags); } else if(flags&NV_ARRAY) @@ -520,13 +520,13 @@ static Namfun_t *array_clone(Namval_t *np, Namval_t *mp, int flags, Namfun_t *fp { Sfdouble_t d= nv_getnum(np); if(!is_associative(ap)) - ar->val[ar->cur].cp = 0; + ar->val[ar->cur] = NULL; nv_putval(mp,(char*)&d,NV_LDOUBLE); } else { if(!is_associative(ap)) - ar->val[ar->cur].cp = 0; + ar->val[ar->cur] = NULL; nv_putval(mp,nv_getval(np),NV_RDONLY); } aq->header.nelem |= ARRAY_NOSCOPE; @@ -586,7 +586,7 @@ static Sfdouble_t array_getnum(Namval_t *np, Namfun_t *disc) static void array_putval(Namval_t *np, const char *string, int flags, Namfun_t *dp) { Namarr_t *ap = (Namarr_t*)dp; - union Value *up; + void **vpp; /* pointer to value pointer */ Namval_t *mp; struct index_array *aq = (struct index_array*)ap; int scan,nofree = nv_isattr(np,NV_NOFREE); @@ -605,7 +605,7 @@ static void array_putval(Namval_t *np, const char *string, int flags, Namfun_t * if(!nv_isattr(np,NV_NOFREE)) _nv_unset(mp,flags&NV_RDONLY); array_clrbit(aq->bits,aq->cur,ARRAY_CHILD); - aq->val[aq->cur].cp = 0; + aq->val[aq->cur] = NULL; if(!nv_isattr(mp,NV_NOFREE)) nv_delete(mp,ap->table,0); goto skip; @@ -634,7 +634,7 @@ static void array_putval(Namval_t *np, const char *string, int flags, Namfun_t * if(mp!=np) { array_clrbit(aq->bits,aq->cur,ARRAY_CHILD); - aq->val[aq->cur].cp = 0; + aq->val[aq->cur] = NULL; if(!xfree) nv_delete(mp,ap->table,0); } @@ -673,18 +673,18 @@ static void array_putval(Namval_t *np, const char *string, int flags, Namfun_t * } skip: /* prevent empty string from being deleted */ - up = array_getup(np,ap,!nofree); - if(up->cp == Empty) - up->cp = 0; + vpp = array_getup(np,ap,!nofree); + if(*vpp == Empty) + *vpp = NULL; #if SHOPT_FIXEDARRAY if(nv_isarray(np) && !ap->fixed) #else if(nv_isarray(np)) #endif /* SHOPT_FIXEDARRAY */ - np->nvalue = up; + np->nvalue = vpp; nv_putv(np,string,flags,&ap->hdr); - if(nofree && !up->cp) - up->cp = Empty; + if(nofree && !*vpp) + *vpp = Empty; #if SHOPT_FIXEDARRAY if(fp = (struct fixed_array*)ap->fixed) { @@ -701,7 +701,7 @@ static void array_putval(Namval_t *np, const char *string, int flags, Namfun_t * if(string) array_clrbit(aq->bits,aq->cur,ARRAY_NOFREE); else if(mp==np) - aq->val[aq->cur].cp = 0; + aq->val[aq->cur] = NULL; } if(string && ap->hdr.type && nv_isvtree(np)) nv_arraysettype(np,ap->hdr.type,nv_getsub(np),0); @@ -805,7 +805,7 @@ static struct index_array *array_grow(Namval_t *np, struct index_array *arp,int errormsg(SH_DICT,ERROR_exit(1),e_subscript,fmtint(maxi,1)); UNREACHABLE(); } - i = (newsize-1)*sizeof(union Value)+newsize; + i = (newsize - 1) * sizeof(void*) + newsize; ap = new_of(struct index_array,i); memset(ap,0,sizeof(*ap)+i); ap->maxi = newsize; @@ -819,7 +819,7 @@ static struct index_array *array_grow(Namval_t *np, struct index_array *arp,int for(i=0;i < arp->maxi;i++) { ap->bits[i] = arp->bits[i]; - ap->val[i].cp = arp->val[i].cp; + ap->val[i] = arp->val[i]; } memcpy(ap->bits, arp->bits, arp->maxi); array_setptr(np,arp,ap); @@ -845,7 +845,7 @@ static struct index_array *array_grow(Namval_t *np, struct index_array *arp,int if(mp && nv_isnull(mp)) { Namfun_t *fp; - ap->val[0].np = mp; + ap->val[0] = mp; array_setbit(ap->bits,0,ARRAY_CHILD); for(fp=np->nvfun; fp && !fp->disc->readf; fp=fp->next); if(fp && fp->disc && fp->disc->readf) @@ -854,7 +854,7 @@ static struct index_array *array_grow(Namval_t *np, struct index_array *arp,int } } else - if((ap->val[0].cp = np->nvalue) || (nv_isattr(np,NV_INTEGER) && !nv_isnull(np))) + if((ap->val[0] = np->nvalue) || (nv_isattr(np,NV_INTEGER) && !nv_isnull(np))) i++; ap->header.nelem = i; ap->header.hdr.disc = &array_disc; @@ -867,7 +867,7 @@ static struct index_array *array_grow(Namval_t *np, struct index_array *arp,int } } for(;i < newsize;i++) - ap->val[i].cp = 0; + ap->val[i] = NULL; return ap; } @@ -915,7 +915,7 @@ static Namarr_t *nv_changearray(Namval_t *np, void *(*fun)(Namval_t*,const char* Namarr_t *ap; char numbuff[NUMSIZE+1]; unsigned dot, digit, n; - union Value *up; + void **vpp; /* pointer to value pointer */ struct index_array *save_ap; char *string_index=&numbuff[NUMSIZE]; numbuff[NUMSIZE]='\0'; @@ -932,7 +932,7 @@ static Namarr_t *nv_changearray(Namval_t *np, void *(*fun)(Namval_t*,const char* for(dot = 0; dot < (unsigned)save_ap->maxi; dot++) { - if(save_ap->val[dot].cp) + if(save_ap->val[dot]) { if ((digit = dot)== 0) *--string_index = '0'; @@ -942,9 +942,9 @@ static Namarr_t *nv_changearray(Namval_t *np, void *(*fun)(Namval_t*,const char* *--string_index = '0' + (n-10*digit); } nv_putsub(np, string_index, ARRAY_ADD); - up = (union Value*)((*ap->fun)(np,NULL,0)); - up->cp = save_ap->val[dot].cp; - save_ap->val[dot].cp = 0; + vpp = (void**)((*ap->fun)(np,NULL,0)); + *vpp = save_ap->val[dot]; + save_ap->val[dot] = NULL; } string_index = &numbuff[NUMSIZE]; } @@ -1008,7 +1008,7 @@ Namval_t *nv_arraychild(Namval_t *np, Namval_t *nq, int c) { Namfun_t *fp; Namarr_t *ap = nv_arrayptr(np); - union Value *up; + void **vpp; /* pointer to value pointer */ Namval_t *tp; if(!nq) return ap ? array_find(np,ap, ARRAY_LOOKUP) : 0; @@ -1017,9 +1017,9 @@ Namval_t *nv_arraychild(Namval_t *np, Namval_t *nq, int c) nv_putsub(np, NULL, ARRAY_FILL); ap = nv_arrayptr(np); } - if(!(up = array_getup(np,ap,0))) + if(!(vpp = array_getup(np,ap,0))) return NULL; - np->nvalue = (void*)up->cp; + np->nvalue = *vpp; if((tp=nv_type(np)) || c) { ap->nelem |= ARRAY_NOCLONE; @@ -1040,7 +1040,7 @@ Namval_t *nv_arraychild(Namval_t *np, Namval_t *nq, int c) array_setbit(aq->bits,aq->cur,ARRAY_CHILD); if(c=='.' && !nq->nvalue) ap->nelem++; - up->np = nq; + *vpp = nq; } if(c=='.') nv_setvtree(nq); @@ -1078,7 +1078,7 @@ int nv_nextsub(Namval_t *np) { nv_putsub(np,0,fp->curi|ARRAY_FIXED|ARRAY_SCAN); if(fp->ptr && *(((char**)fp->data)+fp->curi)) - return 1; + return 1; } ap->header.nelem &= ~ARRAY_FIXED; return 0; @@ -1109,22 +1109,23 @@ int nv_nextsub(Namval_t *np) for(dot=ap->cur+1; dot < (unsigned)ap->maxi; dot++) { aq = ap; - if(!ap->val[dot].cp && !(ap->header.nelem&ARRAY_NOSCOPE)) + if(!ap->val[dot] && !(ap->header.nelem&ARRAY_NOSCOPE)) { if(!(aq=ar) || dot>=(unsigned)aq->maxi) continue; } - if(aq->val[dot].cp==Empty && array_elem(&aq->header) < nv_aimax(np)+1) { + if(aq->val[dot]==Empty && array_elem(&aq->header) < nv_aimax(np)+1) + { ap->cur = dot; if(nv_getval(np)==Empty) continue; } - if(aq->val[dot].cp) + if(aq->val[dot]) { ap->cur = dot; if(array_isbit(aq->bits, dot,ARRAY_CHILD)) { - Namval_t *mp = aq->val[dot].np; + Namval_t *mp = aq->val[dot]; if((aq->header.nelem&ARRAY_NOCHILD) && nv_isvtree(mp) && !mp->nvfun->dsize) continue; if(nv_isarray(mp)) @@ -1204,22 +1205,22 @@ Namval_t *nv_putsub(Namval_t *np,char *sp,long mode) if(mode&ARRAY_SETSUB) { for(n=0; n <= ap->maxi; n++) - ap->val[n].cp = 0; + ap->val[n] = NULL; ap->header.nelem = 0; } for(n=0; n <= size; n++) { - if(!ap->val[n].cp) + if(!ap->val[n]) { - ap->val[n].cp = Empty; + ap->val[n] = Empty; if(!array_covered(np,ap)) ap->header.nelem++; } } if(n=ap->maxi-ap->maxi) - memset(&ap->val[size],0,n*sizeof(union Value)); + memset(&ap->val[size], 0, n * sizeof(void*)); } - else if(!(sp=(char*)ap->val[size].cp) || sp==Empty) + else if(!(sp = ap->val[size]) || sp==Empty) { if(sh.subshell) sh_assignok(np,1); @@ -1237,7 +1238,7 @@ Namval_t *nv_putsub(Namval_t *np,char *sp,long mode) nv_setvtree(mp); } else if(!sh.cond_expan) - ap->val[size].cp = Empty; + ap->val[size] = Empty; if(!sp && !array_covered(np,ap)) ap->header.nelem++; } @@ -1246,8 +1247,8 @@ Namval_t *nv_putsub(Namval_t *np,char *sp,long mode) { ap->header.nelem &= ~ARRAY_SCAN; if(array_isbit(ap->bits,size,ARRAY_CHILD)) - nv_putsub(ap->val[size].np,NULL,ARRAY_UNDEF); - if(sp && !(mode&ARRAY_ADD) && !ap->val[size].cp) + nv_putsub(ap->val[size],NULL,ARRAY_UNDEF); + if(sp && !(mode&ARRAY_ADD) && !ap->val[size]) np = 0; } return (Namval_t*)np; @@ -1554,7 +1555,7 @@ Namval_t *nv_opensub(Namval_t* np) else if(array_isbit(ap->bits,ap->cur,ARRAY_CHILD)) #endif /* SHOPT_FIXEDARRAY */ { - return ap->val[ap->cur].np; + return ap->val[ap->cur]; } #if SHOPT_FIXEDARRAY else if(fp) @@ -1634,7 +1635,7 @@ int nv_aimax(Namval_t* np) #endif /* SHOPT_FIXEDARRAY */ return -1; sub = ap->maxi; - while(--sub>0 && ap->val[sub].cp==0); + while(--sub>0 && !ap->val[sub]); return sub; } @@ -1811,7 +1812,7 @@ void nv_setvec(Namval_t *np,int append,int argc,char *argv[]) if(!(aq = (struct index_array*)ap->header.scope)) aq = ap; arg0 = ap->maxi; - while(--arg0>0 && ap->val[arg0].cp==0 && aq->val[arg0].cp==0); + while(--arg0>0 && !ap->val[arg0] && !aq->val[arg0]); arg0++; } else diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c index e00dafe9d66e..904304a92fe4 100644 --- a/src/cmd/ksh93/sh/name.c +++ b/src/cmd/ksh93/sh/name.c @@ -1604,7 +1604,7 @@ static char savechars[8+1]; void nv_putval(Namval_t *np, const char *string, int flags) { const char *sp=string; - union Value *up; + void **vpp; /* pointer to value pointer */ unsigned int size = 0; int was_local = nv_local; #if SHOPT_FIXEDARRAY @@ -1659,16 +1659,16 @@ void nv_putval(Namval_t *np, const char *string, int flags) nv_setattr(np,(flags&~NV_RDONLY)|NV_NOFREE); return; } - up = (union Value*)&np->nvalue; + vpp = &np->nvalue; #if SHOPT_FIXEDARRAY if(np->nvalue && nv_isarray(np) && (ap=nv_arrayptr(np)) && !ap->fixed) #else if(np->nvalue && nv_isarray(np) && nv_arrayptr(np)) #endif /* SHOPT_FIXEDARRAY */ - up = np->nvalue; - if(up && up->cp==Empty) - up->cp = 0; - if(nv_isattr (np, NV_INTEGER)) + vpp = np->nvalue; + if(vpp && *vpp==Empty) + *vpp = NULL; + if(nv_isattr(np,NV_INTEGER)) { if(nv_isattr(np, NV_DOUBLE) == NV_DOUBLE) { @@ -1686,11 +1686,11 @@ void nv_putval(Namval_t *np, const char *string, int flags) } else ld = sh_arith(sp); - if(!up->ldp) - up->ldp = new_of(Sfdouble_t,0); + if(!*vpp) + *vpp = new_of(Sfdouble_t,0); else if(flags&NV_APPEND) - old = *(up->ldp); - *(up->ldp) = old?ld+old:ld; + old = *(Sfdouble_t*)*vpp; + *(Sfdouble_t*)*vpp = old ? ld+old : ld; } else { @@ -1706,11 +1706,11 @@ void nv_putval(Namval_t *np, const char *string, int flags) } else d = sh_arith(sp); - if(!up->dp) - up->dp = new_of(double,0); + if(!*vpp) + *vpp = new_of(double,0); else if(flags&NV_APPEND) - od = *(up->dp); - *(up->dp) = od?d+od:d; + od = *(double*)*vpp; + *(double*)*vpp = od ? d+od : d; } } else @@ -1750,11 +1750,11 @@ void nv_putval(Namval_t *np, const char *string, int flags) } else if(sp) ll = (Sflong_t)sh_arith(sp); - if(!up->llp) - up->llp = new_of(Sflong_t,0); + if(!*vpp) + *vpp = new_of(Sflong_t,0); else if(flags&NV_APPEND) - oll = *(up->llp); - *(up->llp) = ll+oll; + oll = *(Sflong_t*)*vpp; + *(Sflong_t*)*vpp = ll + oll; } else { @@ -1804,27 +1804,27 @@ void nv_putval(Namval_t *np, const char *string, int flags) if(nv_isattr(np,NV_SHORT)) { int16_t os=0; - if(!up->sp) - up->sp = new_of(int16_t,0); + if(!*vpp) + *vpp = new_of(int16_t,0); else if(flags&NV_APPEND) - os = *(up->sp); - *(up->sp) = os+(int16_t)l; + os = *(int16_t*)*vpp; + *(int16_t*)*vpp = os + (int16_t)l; } else { int32_t ol=0; - if(!up->lp) - up->lp = new_of(int32_t,0); + if(!*vpp) + *vpp = new_of(int32_t,0); else if(flags&NV_APPEND) - ol = *(up->lp); - *(up->lp) = l+ol; + ol = *(int32_t*)*vpp; + *(int32_t*)*vpp = l + ol; } } } } else { - const char *tofree=0; + void *tofree=0; int offset = 0; #if _lib_pathnative char buff[PATH_MAX]; @@ -1883,25 +1883,25 @@ void nv_putval(Namval_t *np, const char *string, int flags) if(size) size = ja_size((char*)sp,size,nv_isattr(np,NV_RJUST|NV_ZFILL)); } - if(!up->cp || *up->cp==0) + if(!*vpp || *(char*)*vpp==0) flags &= ~NV_APPEND; if(!nv_isattr(np, NV_NOFREE)) { /* delay free in case points into free region */ - tofree = up->cp; + tofree = *vpp; } if(nv_isattr(np,NV_BINARY) && !(flags&NV_RAW)) tofree = 0; if(nv_isattr(np,NV_LJUST|NV_RJUST) && nv_isattr(np,NV_LJUST|NV_RJUST)!=(NV_LJUST|NV_RJUST)) tofree = 0; if(!sp) - up->cp = NULL; + *vpp = NULL; else { char *cp = NULL; /* pointer to new string */ unsigned int dot; /* attribute or type length; defaults to string length */ unsigned int append = 0; /* offset for appending */ - if(sp==up->cp && !(flags&NV_APPEND)) + if(sp==*vpp && !(flags&NV_APPEND)) return; dot = strlen(sp); if(nv_isattr(np,NV_BINARY)) @@ -1914,7 +1914,7 @@ void nv_putval(Namval_t *np, const char *string, int flags) free((void*)tofree); nv_offattr(np,NV_NOFREE); } - up->cp = sp; + *vpp = (void*)sp; return; } size = 0; @@ -1926,8 +1926,8 @@ void nv_putval(Namval_t *np, const char *string, int flags) *cp = 0; nv_offattr(np,NV_NOFREE); if(oldsize) - memcpy(cp,up->cp,oldsize); - up->cp = cp; + memcpy(cp,*vpp,oldsize); + *vpp = cp; if(size <= oldsize) return; dot = base64decode(sp,dot, NULL, cp+oldsize, size-oldsize,NULL); @@ -1943,7 +1943,7 @@ void nv_putval(Namval_t *np, const char *string, int flags) if(size==0 && nv_isattr(np,NV_HOST)!=NV_HOST &&nv_isattr(np,NV_LJUST|NV_RJUST|NV_ZFILL)) { nv_setsize(np,size=dot); - tofree = up->cp; + tofree = *vpp; } else if(size > dot) dot = size; @@ -1953,11 +1953,11 @@ void nv_putval(Namval_t *np, const char *string, int flags) { if(dot==0) return; - append = strlen(up->cp); + append = strlen(*vpp); if(!tofree || size) { offset = stktell(sh.stk); - sfputr(sh.stk,up->cp,-1); + sfputr(sh.stk,*vpp,-1); sfputr(sh.stk,sp,0); sp = stkptr(sh.stk,offset); dot += append; @@ -1969,7 +1969,7 @@ void nv_putval(Namval_t *np, const char *string, int flags) } } } - if(size==0 || tofree || dot || !(cp=(char*)up->cp)) + if(size==0 || tofree || dot || !(cp = *vpp)) { if(dot==0 && !nv_isattr(np,NV_LJUST|NV_RJUST)) { @@ -1995,7 +1995,7 @@ void nv_putval(Namval_t *np, const char *string, int flags) strncpy(cp+append, sp, dot+1); cp[dot+append] = c; } - up->cp = cp; + *vpp = cp; if(nv_isattr(np, NV_RJUST) && nv_isattr(np, NV_ZFILL)) rightjust(cp,size,'0'); else if(nv_isattr(np, NV_LJUST|NV_RJUST)==NV_RJUST) @@ -2345,13 +2345,13 @@ static void table_unset(Dt_t *root, int flags, Dt_t *oroot) */ void _nv_unset(Namval_t *np,int flags) { - union Value *up; + void **vpp; /* pointer to value pointer */ #if SHOPT_FIXEDARRAY Namarr_t *ap; #endif /* SHOPT_FIXEDARRAY */ if(np==SH_LEVELNOD) return; - if(!(flags&NV_RDONLY) && nv_isattr (np,NV_RDONLY)) + if(!(flags&NV_RDONLY) && nv_isattr(np,NV_RDONLY)) { errormsg(SH_DICT,ERROR_exit(1),e_readonly, nv_name(np)); UNREACHABLE(); @@ -2433,7 +2433,7 @@ void _nv_unset(Namval_t *np,int flags) #else if(np->nvalue && nv_isarray(np) && nv_arrayptr(np)) #endif /* SHOPT_FIXEDARRAY */ - up = np->nvalue; + vpp = np->nvalue; else if(nv_isref(np) && !nv_isattr(np,NV_EXPORT|NV_MINIMAL) && np->nvalue) { struct Namref *nrp = np->nvalue; @@ -2443,15 +2443,15 @@ void _nv_unset(Namval_t *np,int flags) free(nrp->sub); free(nrp); np->nvalue = NULL; - up = 0; + vpp = NULL; } else - up = (union Value*)&np->nvalue; - if(up && up->cp) + vpp = &np->nvalue; + if(vpp && *vpp) { - if(up->cp!=Empty && up->cp!=AltEmpty && !nv_isattr(np, NV_NOFREE)) - free((void*)up->cp); - up->cp = 0; + if(*vpp!=Empty && *vpp!=AltEmpty && !nv_isattr(np,NV_NOFREE)) + free(*vpp); + *vpp = NULL; } done: if(!nv_isarray(np) || !nv_arrayptr(np)) @@ -2618,8 +2618,8 @@ void sh_optclear(void *old) */ char *nv_getval(Namval_t *np) { - union Value *up = (union Value*)&np->nvalue; - int numeric; + void **vpp = &np->nvalue; /* pointer to value pointer */ + size_t n; if(!nv_local && nv_getoptimize()) nv_optimize(np); if((!np->nvfun || !np->nvfun->disc) && !nv_isattr(np,NV_ARRAY|NV_INTEGER|NV_FUNCT|NV_REF)) @@ -2648,24 +2648,21 @@ char *nv_getval(Namval_t *np) } nv_local=0; } - numeric = ((nv_isattr (np, NV_INTEGER)) != 0); - if(numeric) + if(nv_isattr(np,NV_INTEGER)) { Sflong_t ll; int base; - if(!up->cp) + if(!*vpp) return "0"; - if(nv_isattr (np,NV_DOUBLE)==NV_DOUBLE) + if(nv_isattr(np,NV_DOUBLE)==NV_DOUBLE) { - Sfdouble_t ld; - double d; char *format; - if(nv_isattr(np,NV_LONG)) + if(nv_isattr(np,NV_LONG) && sizeof(double)ldp; - if(nv_isattr (np,NV_EXPNOTE)) + Sfdouble_t ld = *(Sfdouble_t*)*vpp; + if(nv_isattr(np,NV_EXPNOTE)) format = "%.*Lg"; - else if(nv_isattr (np,NV_HEXFLOAT)) + else if(nv_isattr(np,NV_HEXFLOAT)) format = "%.*La"; else format = "%.*Lf"; @@ -2673,10 +2670,10 @@ char *nv_getval(Namval_t *np) } else { - d = *up->dp; - if(nv_isattr (np,NV_EXPNOTE)) + double d = *(double*)*vpp; + if(nv_isattr(np,NV_EXPNOTE)) format = "%.*g"; - else if(nv_isattr (np,NV_HEXFLOAT)) + else if(nv_isattr(np,NV_HEXFLOAT)) format = "%.*a"; else format = "%.*f"; @@ -2686,19 +2683,19 @@ char *nv_getval(Namval_t *np) } else if(nv_isattr(np,NV_UNSIGN)) { - if(nv_isattr (np,NV_LONG)) - ll = *(Sfulong_t*)up->llp; - else if(nv_isattr (np,NV_SHORT)) - ll = *(uint16_t*)(up->sp); + if(nv_isattr(np,NV_LONG)) + ll = *(Sfulong_t*)*vpp; + else if(nv_isattr(np,NV_SHORT)) + ll = *(uint16_t*)*vpp; else - ll = *(uint32_t*)(up->lp); + ll = *(uint32_t*)*vpp; } - else if(nv_isattr (np,NV_LONG)) - ll = *up->llp; - else if(nv_isattr (np,NV_SHORT)) - ll = *up->sp; + else if(nv_isattr(np,NV_LONG)) + ll = *(Sflong_t*)*vpp; + else if(nv_isattr(np,NV_SHORT)) + ll = *(int16_t*)*vpp; else - ll = *(up->lp); + ll = *(uint32_t*)*vpp; base = nv_size(np); if(base==10) return fmtint(ll, nv_isattr(np,NV_UNSIGN)); @@ -2711,28 +2708,28 @@ char *nv_getval(Namval_t *np) * if NV_RAW flag is on, return pointer to binary data * otherwise, base64 encode the data and return this string */ - if(up->cp && nv_isattr(np,NV_BINARY) && !nv_isattr(np,NV_RAW)) + if(*vpp && nv_isattr(np,NV_BINARY) && !nv_isattr(np,NV_RAW)) { char *cp; char *ep; int size= nv_size(np), insize=(4*size)/3+size/45+8; - base64encode(up->cp, size, NULL, cp=getbuf(insize), insize, (void**)&ep); + base64encode(*vpp, size, NULL, cp=getbuf(insize), insize, (void**)&ep); *ep = 0; return cp; } - if(!nv_isattr(np,NV_LJUST|NV_RJUST) && (numeric=nv_size(np)) && up->cp && up->cp[numeric]) + if(!nv_isattr(np,NV_LJUST|NV_RJUST) && (n = nv_size(np)) && *vpp && ((char*)*vpp)[n]) { - char *cp = getbuf(numeric+1); - memcpy(cp,up->cp,numeric); - cp[numeric]=0; + char *cp = getbuf(n + 1); + memcpy(cp,*vpp,n); + cp[n]=0; return cp; } - return ((char*)up->cp); + return *vpp; } Sfdouble_t nv_getnum(Namval_t *np) { - union Value *up; + void *vp; /* value pointer */ Sfdouble_t r=0; char *str; if(!nv_local && nv_getoptimize()) @@ -2758,35 +2755,35 @@ Sfdouble_t nv_getnum(Namval_t *np) if(str) nv_putsub(np,str,0L); } - if(nv_isattr (np, NV_INTEGER)) + if(nv_isattr(np,NV_INTEGER)) { - up = (union Value*)&np->nvalue; - if(!up->lp || up->cp==Empty) + vp = np->nvalue; + if(!vp || vp==Empty) r = 0; else if(nv_isattr(np, NV_DOUBLE)==NV_DOUBLE) { if(nv_isattr(np, NV_LONG)) - r = *up->ldp; + r = *(Sfdouble_t*)vp; else - r = *up->dp; + r = *(double*)vp; } else if(nv_isattr(np, NV_UNSIGN)) { if(nv_isattr(np, NV_LONG)) - r = (Sflong_t)*((Sfulong_t*)up->llp); + r = (Sflong_t)*(Sfulong_t*)vp; else if(nv_isattr(np, NV_SHORT)) - r = (Sflong_t)(*(uint16_t*)up->sp); + r = (Sflong_t)*(uint16_t*)vp; else - r = *((uint32_t*)up->lp); + r = *((uint32_t*)vp); } else { if(nv_isattr(np, NV_LONG)) - r = *up->llp; + r = *(Sflong_t*)vp; else if(nv_isattr(np, NV_SHORT)) - r = *up->sp; + r = *(int16_t*)vp; else - r = *up->lp; + r = *(int32_t*)vp; } } else if((str=nv_getval(np)) && *str!=0) diff --git a/src/cmd/ksh93/sh/nvdisc.c b/src/cmd/ksh93/sh/nvdisc.c index cd278661f35b..a8590f8cf7bb 100644 --- a/src/cmd/ksh93/sh/nvdisc.c +++ b/src/cmd/ksh93/sh/nvdisc.c @@ -243,7 +243,7 @@ static void assign(Namval_t *np,const char* val,int flags,Namfun_t *handle) Namval_t *nq = vp->disc[type]; struct blocked block, *bp = block_info(np, &block); Namval_t node; - union Value *up = np->nvalue; + void *saveval = np->nvalue; Namval_t *tp, *nr; /* for 'typeset -T' types */ int jmpval = 0; if(val && (tp=nv_type(np)) && (nr=nv_open(val,sh.var_tree,NV_VARNAME|NV_ARRAY|NV_NOADD|NV_NOFAIL)) && tp==nv_type(nr)) @@ -306,7 +306,7 @@ static void assign(Namval_t *np,const char* val,int flags,Namfun_t *handle) sh.savexit = savexit; /* avoid influencing $? */ } if(nv_isarray(np)) - np->nvalue = up; + np->nvalue = saveval; if(val) { char *cp; @@ -382,7 +382,7 @@ static char* lookup(Namval_t *np, int type, Sfdouble_t *dp,Namfun_t *handle) Namval_t *nq = vp->disc[type]; char *cp=0; Namval_t node; - union Value *up = np->nvalue; + void *saveval = np->nvalue; int jmpval = 0; if(nq && !isblocked(bp,type)) { @@ -433,7 +433,7 @@ static char* lookup(Namval_t *np, int type, Sfdouble_t *dp,Namfun_t *handle) sh.savexit = savexit; /* avoid influencing $? */ } if(nv_isarray(np)) - np->nvalue = up; + np->nvalue = saveval; if(bp== &block) block_done(bp); if(nq && nq->nvalue && ((struct Ufunction*)nq->nvalue)->running==1)