NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/42463: Bizarre behavior in awk with invalid numeric constants

The following reply was made to PR bin/42463; it has been noted by GNATS.

From: David Holland <>
Subject: Re: bin/42463: Bizarre behavior in awk with invalid numeric
Date: Sat, 23 Jan 2010 19:55:52 +0000

 On Sat, Dec 19, 2009 at 05:05:03AM +0000, Greg A. Woods wrote:
  >> This is not so surprising, although one would expect it to generate a
  >> syntax error (recall that awk doesn't handle hex integer constants...)
  >  Well, a syntax error would not really be correct so far as I can tell,
  >  maybe not even for constants in the program text.
 For arbitrary values, yes, but for constants in the program text?
 Surely those should be rejected if they aren't actually numbers.
  >  However as you've shown it doesn't seem as though things actually work
  >  the way _I_ would expect when it comes to expressions containing
  >  un-quoted non-numeric constants with numeric prefixes.
 Right. Whatever is going on is deeper than just running strtol() on
 some bogus strings gotten out of the program text.
  >  Interestingly to me awk and mawk behave in exactly the same bizarre way:
  >  $ awk 'BEGIN{v = 0xblegh - 5; printf("%s\n", v) }'
  >  0-5
  >  $ mawk 'BEGIN{v = 0xblegh - 5; printf("%s\n", v) }'
  >  0-5
  >  $ gawk 'BEGIN{v = 0xblegh - 5; printf("%s\n", v) }'
  >  11-5
  >  Those examples really do floor me.  What an amazing side effect, and
  >  identically in two different implementations!
  >  To avoid any possible code parser issues we can feed the value in as
  >  input, and indeed that does then seem to have a better result, though
  >  still not entirely an expected result since, IIUC, neither awk nor mawk
  >  should interpret hex for input values, but apparently they do:
  >  [...]
  >  23:07 [603] $ echo "0xblegh" | awk '{v = $1} END{printf("%s\n", v + 0)}'
  >  11
 That seems broken, yes...
 David A. Holland

Home | Main Index | Thread Index | Old Index