[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Special Comparisons: uninitialized array index
From: |
Andrew J. Schorr |
Subject: |
Re: [bug-gawk] Special Comparisons: uninitialized array index |
Date: |
Mon, 7 Aug 2017 12:37:36 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Mon, Aug 07, 2017 at 11:59:29AM -0400, Andrew J. Schorr wrote:
> On Mon, Aug 07, 2017 at 11:07:13AM -0400, Andrew J. Schorr wrote:
> > The fix is less obvious to me. One could patch str_array.c:str_lookup as
> > follows:
> >
> > --- a/str_array.c
> > +++ b/str_array.c
> > @@ -165,7 +165,7 @@ str_lookup(NODE *symbol, NODE *subs)
> > * "Array indices are always strings."
> > * ....
> > */
> > - if (subs->stfmt != STFMT_UNUSED) {
> > + if (subs->stfmt != STFMT_UNUSED || subs == Nnull_string) {
> > /* The string was generated using CONVFMT. */
> > NODE *tmp;
> >
> > But I'm not sure that's the best solution. Perhaps it should be attacked
> > on the extraction side in the str_list function, but that would probably
> > lead to memory leaks. Arnold -- what do you think?
>
> Sorry -- I read the code too fast. We could certainly fix this in str_list,
> but
> str_lookup seems more conceptually correct and consistent.
Note that this is still a bit weird:
bash-4.2$ gawk 'BEGIN {
> a[$1] = "null"
> for (i in a) {
> print length(i), a[i], typeof(i)
> print i==0, i==""
> }
> }
> '
0 null unassigned
0 1
The comparison results are correct, but is it desirable for "typeof"
to return "unassigned" in this scenario?
Perhaps the patch should be:
--- a/str_array.c
+++ b/str_array.c
@@ -165,7 +165,7 @@ str_lookup(NODE *symbol, NODE *subs)
* "Array indices are always strings."
* ....
*/
- if (subs->stfmt != STFMT_UNUSED) {
+ if (subs->stfmt != STFMT_UNUSED || subs == Nnull_string || (subs->flags
& NULL_FIELD)) {
/* The string was generated using CONVFMT. */
NODE *tmp;
Or maybe we need a macro or inlined function to test whether a value
is unassigned, since this logic is also in the do_typeof function...
-Andy
Re: [bug-gawk] Special Comparisons: uninitialized array index, Andrew J. Schorr, 2017/08/07