1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-15 04:32:24 +00:00

Fix 'typeset -p' output of compound array types (#453)

This bugfix was backported from ksh93v- 2012-10-04. The bug fixed
by this change is one that causes 'typeset -p' to omit the -C flag
when listing compound arrays belonging to a type:

   $ typeset -T Foo_t=(compound -a bar)
   $ Foo_t baz
   $ typeset -p baz.bar
   typeset -a baz.bar=''  # This should be 'typeset -C -a'

src/cmd/ksh93/sh/nvtype.c:
- Backport change from 93v- 2012-10-04 that sets the array nvalue to
  a pointer named Null (which is "") in nv_mktype(), then to Empty
  in fixnode().
- Change the Null name from the 93v- code to AltEmpty to avoid
  misleading code readers into thinking that it's a null pointer.

src/cmd/ksh93/tests/types.sh:
- Backport the relevant 93v- changes to the types regression tests.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2022-02-08 11:52:13 -08:00 committed by Martijn Dekker
parent 787058bdbf
commit 7d4c7d9156
3 changed files with 25 additions and 6 deletions

11
NEWS
View file

@ -14,10 +14,13 @@ Any uppercase BUG_* names are modernish shell bug IDs.
an incompletely defined 'foo_t' built-in comamnd that will crash the shell an incompletely defined 'foo_t' built-in comamnd that will crash the shell
when used. Instead, it is now silently ignored for backwards compatibility. when used. Instead, it is now silently ignored for backwards compatibility.
- A bug in which the output of a two dimensional sparse indexed array would - Compound arrays belonging to a type created with 'typeset -T' now have
cause the second subscript to be treated as an associative array when read their -C attribute preserved in the output of 'typeset -p'.
back in has been fixed. Elements that are sparse indexed arrays are now
prefixed with "typeset -a". - A bug has been fixed in which the 'typeset -p' output of a two-dimensional
sparse indexed array would cause the second subscript to be treated as an
associative array when read back in by the shell. Elements that are sparse
indexed arrays are now prefixed with "typeset -a".
2022-02-05: 2022-02-05:

View file

@ -139,6 +139,8 @@ static const Namdisc_t type_disc =
0, 0,
}; };
static char *AltEmpty = "";
size_t nv_datasize(Namval_t *np, size_t *offset) size_t nv_datasize(Namval_t *np, size_t *offset)
{ {
size_t s=0, a=0; size_t s=0, a=0;
@ -333,6 +335,8 @@ static int fixnode(Namtype_t *dp, Namtype_t *pp, int i, struct Namref *nrp,int f
nv_offattr(nq,NV_NOFREE); nv_offattr(nq,NV_NOFREE);
} }
} }
else if(nq->nvalue.cp==AltEmpty)
nq->nvalue.cp = Empty;
else if(nq->nvalue.cp==Empty) else if(nq->nvalue.cp==Empty)
nv_offattr(nq,NV_NOFREE); nv_offattr(nq,NV_NOFREE);
@ -1159,7 +1163,12 @@ Namval_t *nv_mktype(Namval_t **nodes, int numnodes)
free((void*)np->nvalue.cp); free((void*)np->nvalue.cp);
} }
if(!nq->nvalue.cp && nq->nvfun== &pp->childfun.fun) if(!nq->nvalue.cp && nq->nvfun== &pp->childfun.fun)
nq->nvalue.cp = Empty; {
if(nv_isattr(np,NV_ARRAY|NV_NOFREE)==(NV_ARRAY|NV_NOFREE))
nq->nvalue.cp = AltEmpty;
else
nq->nvalue.cp = Empty;
}
np->nvalue.cp = 0; np->nvalue.cp = 0;
offset += (dsize?dsize:4); offset += (dsize?dsize:4);
} }

View file

@ -592,7 +592,7 @@ $SHELL << \EOF
compound p=( hello=world ) compound p=( hello=world )
c.b.binsert p 1 $i c.b.binsert p 1 $i
done done
exp='typeset -C c=(board_t b=(typeset -a board_y=( [1]=(typeset -a board_x=( [0]=(field=(hello=world;))[1]=(field=(hello=world)));));))' exp='typeset -C c=(board_t b=(typeset -C -a board_y=( [1]=(typeset -a board_x=( [0]=(field=(hello=world;))[1]=(field=(hello=world)));));))'
[[ $(typeset -p c) == "$exp" ]] || exit 1 [[ $(typeset -p c) == "$exp" ]] || exit 1
} }
main main
@ -634,6 +634,13 @@ Bar_t bar
bar.foo+=(bam) bar.foo+=(bam)
[[ ${bar.foo[0]} == bam ]] || err_exit 'appending to empty array variable in type does not create element 0' [[ ${bar.foo[0]} == bam ]] || err_exit 'appending to empty array variable in type does not create element 0'
# ======
# Compound arrays listed with 'typeset -p' should have their -C attribute
# preserved in the output.
typeset -T Z_t=(compound -a x)
Z_t z
[[ $(typeset -p z.x) == *'-C -a'* ]] || err_exit 'typeset -p for compound array element does not display all attributes'
# ====== # ======
# Type names that have 'a' as the first letter should be functional # Type names that have 'a' as the first letter should be functional
"$SHELL" -c 'typeset -T al=(typeset bar); al foo=(bar=testset)' || err_exit "type names that start with 'a' don't work" "$SHELL" -c 'typeset -T al=(typeset bar); al foo=(bar=testset)' || err_exit "type names that start with 'a' don't work"