1#!/bin/sh 2 3echo T.misc: miscellaneous buglets now watched for 4 5awk=${awk-../a.out} 6 7rm -f core 8 9echo 'The big brown over the lazy doe 10The big brown over the lazy dog 11x 12The big brown over the lazy dog' >foo 13echo 'failed 14succeeded 15failed 16succeeded' >foo1 17$awk '{ if (match($0, /^The big brown over the lazy dog/) == 0) { 18 printf("failed\n") 19 } else { 20 printf("succeeded\n") 21 } 22} ' foo >foo2 23cmp -s foo1 foo2 || echo 'BAD: T.misc ghosh RE bug' 24 25echo '123 261234567890 2712345678901' >foo 28echo '12345678901' >foo1 29$awk 'length($0) > 10' foo >foo2 30cmp -s foo1 foo2 || echo 'BAD: T.misc last number bug' 31 32# check some \ sequences in strings (ascii) 33echo HIJKL >foo1 34echo foo | $awk '{ print "H\x49\x4a\x4BL" }' >foo2 35cmp -s foo1 foo2 || echo 'BAD: T.misc hex string cvt' 36 37echo 012x45 >foo1 38$awk 'BEGIN { print "0\061\62x\0645" }' >foo2 39cmp -s foo1 foo2 || echo 'BAD: T.misc oct string cvt' 40 41# $i++ means ($i)++ 42echo 3 5 | $awk '{ i = 1; print $i++ ; print $1, i }' >foo1 43echo '3 444 1' >foo2 45cmp -s foo1 foo2 || echo 'BAD: T.misc bad field increment' 46 47# makes sure that fields are recomputed even if self-assignment 48# take into account that subtracting from NF now rebuilds the record 49echo 'a b c 50s p q r 51x y z' >foo 52echo 'a 53s p 54x' >foo1 55$awk '{ NF -= 2; $1 = $1; print }' <foo >foo2 56diff foo1 foo2 || echo 1>&2 "BAD: T.misc bad field self-assignment" 57 58echo '1 591' >foo1 60$awk 'BEGIN {x = 1; print x; x = x; print x}' >foo2 61diff foo1 foo2 || echo 1>&2 "BAD: T.misc bad self-assignment" 62 63echo 573109312 | $awk '{print $1*4}' >foo1 64echo 2292437248 >foo2 65diff foo1 foo2 || echo 1>&2 "BAD: T.misc bad overflow" 66 67# note that there are 8-bit characters in the echo 68# some shells will probably screw this up. 69echo '# 70code 1 71code 2' | 72$awk '/^#/' >foo1 73echo '#' >foo2 74diff foo1 foo2 || echo 1>&2 "BAD: T.misc bad match of 8-bit char" 75 76echo hello | 77$awk 'BEGIN { FILENAME = "/etc/passwd" } 78 { print $0 }' >/dev/null 79if test -r core; then echo 1>&2 "BAD: T.misc /etc/passwd dropped core"; fi 80 81echo hello | 82$awk ' function foo(foo) { 83 foo = 1 84 foo() 85 } 86 { foo(bar) } 87' >/dev/null 2>&1 88if test -r core; then 89 echo 1>&2 "BAD: T.misc function foo(foo) dropped core" 90 rm -f core 91fi 92 93echo '2 9410' | 95$awk '{ x[NR] = $0 } # test whether $0 is NUM as well as STR 96END { if (x[1] > x[2]) print "BAD: T.misc: $0 is not NUM" }' 97 98 99$awk 'BEGIN { 100 npad = substr("alexander" " ",1,15) 101 print npad 102 }' >foo 103grep '\\' foo && echo 1>&2 "BAD: T.misc alexander fails" 104 105# This should give an error about function arguments 106$awk ' 107function foo(x) { print "x is" x } 108BEGIN { foo(foo) } 109' 2>foo 110grep "can't use function foo" foo >/dev/null || echo 1>&2 "BAD: T.misc fcn args" 111 112 113# gawk defref test; should give error about undefined function 114$awk 'BEGIN { foo() }' 2>foo 115grep "calling undefined function foo" foo >/dev/null || echo 1>&2 "BAD: T.misc undefined function" 116 117 118# gawk arrayparm test; should give error about function 119$awk ' 120BEGIN { 121 foo[1]=1; 122 foo[2]=2; 123 bug1(foo); 124} 125function bug1(i) { 126 for (i in foo) { 127 bug2(i); 128 delete foo[i]; 129 print i,1,bot[1]; 130 } 131} 132function bug2(arg) { 133 bot[arg]=arg; 134} 135' 2>foo 136grep "can.t assign to foo" foo >/dev/null || echo 1>&2 "BAD: T.misc foo bug" 137 138 139# This should be a syntax error 140$awk ' 141!x = y 142' 2>foo 143grep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error !x=y fails" 144 145# This should print bbb 146$awk ' 147BEGIN { up[1] = "a" 148 for (i in up) gsub("a", "A", x) 149 print x x "bbb" 150 exit 151 } 152' >foo 153grep bbb foo >/dev/null || echo 1>&2 "BAD: T.misc gsub failed" 154 155echo yes | 156$awk ' 157BEGIN { 158 printf "push return" >"/dev/null" 159 getline ans <"/dev/null" 160} ' 161if test -r core; then echo 1>&2 "BAD: T.misc getline ans dropped core"; fi 162 163$awk 'BEGIN { unireghf() } 164function unireghf(hfeed) { hfeed[1] = 0 }' 165if test -r core; then echo 1>&2 "BAD: T.misc unireghf dropped core"; fi 166 167echo x | $awk '/[/]/' 2>foo 168grep 'nonterminated character class' foo >/dev/null || error 'BAD: T.misc nonterminated fails' 169if test -r core; then echo 1>&2 "BAD: T.misc nonterminated dropped core"; fi 170 171$awk ' 172function f() { return 12345 } 173BEGIN { printf "<%s>\n", f() } 174' >foo 175grep '<12345>' foo >/dev/null || echo 'BAD: T.misc <12345> fails' 176 177echo 'abc 178def 179 180ghi 181jkl' >foo 182$awk ' 183BEGIN { RS = "" 184 while (getline <"foo") 185 print 186}' >foo1 187$awk 'END {print NR}' foo1 | grep 4 >/dev/null || echo 'BAD: T.misc abcdef fails' 188 189# Test for RS regex being reapplied 190echo aaa1a2a | $awk 1 RS='^a' >foo1 191cat << \EOF > foo2 192 193aa1a2a 194 195EOF 196diff foo1 foo2 || echo 'BAD: T.misc ^regex reapplied fails' 197 198# The following should not produce a warning about changing a constant 199# nor about a curdled tempcell list 200$awk 'function f(x) { x = 2 } 201BEGIN { f(1) }' >foo 202grep '^' foo && echo 'BAD: test constant change fails' 203 204# The following should not produce a warning about a curdled tempcell list 205$awk 'function f(x) { x } 206BEGIN { f(1) }' >foo 207grep '^' foo && echo 'BAD: test tempcell list fails' 208 209$awk 'BEGIN { print 9, a=10, 11; print a; exit }' >foo1 210echo '9 10 11 21110' >foo2 212diff foo1 foo2 || echo 'BAD: T.misc (embedded expression)' 213 214echo "abc defgh ijkl" | $awk ' 215 { $1 = ""; line = $0; print line; print $0; $0 = line; print $0 }' >foo1 216echo " defgh ijkl 217 defgh ijkl 218 defgh ijkl" >foo2 219diff foo1 foo2 || echo 'BAD: T.misc (assignment to $0)' 220 221$awk ' 222function min(a, b) 223{ 224 if (a < b) 225 return a 226 else 227 return b 228} 229BEGIN { exit } 230' 231if test -r core; then echo 1>&2 "BAD: T.misc function min dropped core"; fi 232 233# The following should not give a syntax error message: 234$awk ' 235function expand(chart) { 236 getline chart < "CHAR.ticks" 237} 238' >foo 239grep '^' foo >/dev/null && echo 'BAD: T.misc expand error' 240 241$awk 'BEGIN { print 1e40 }' >/dev/null 242if test -r core; then echo 1>&2 "BAD: T.misc 1E40 dropped core"; fi 243 244# The following syntax error should not dump core: 245$awk ' 246$NF==3 {first=1} 247$NF==2 && first==0 && (abs($1-o1)>120||abs($2-o2)>120) {print $0} 248$NF==2 {o1=%1; o2=$2; first=0} 249' 2>/dev/null 250if test -r core; then echo 1>&2 "BAD: T.misc first/abs dropped core"; fi 251 252# The following syntax error should not dump core: 253$awk '{ n = split($1, address, !); print address[1] }' 2>foo 254grep 'illegal statement' foo >/dev/null || echo 'BAD: T.misc split error' 255if test -r core; then echo 1>&2 "BAD: T.misc split! dropped core"; fi 256 257# The following should cause a syntax error message 258$awk 'BEGIN {"hello"}' 2>foo 259grep 'illegal statement' foo >/dev/null || echo 'BAD: T.misc hello error' 260 261# The following should give a syntax error message: 262$awk ' 263function pile(c, r) { 264 r = ++pile[c] 265} 266 267{ pile($1) } 268' 2>foo 269grep 'context is' foo >/dev/null || echo 'BAD: T.misc pile error' 270 271# This should complain about missing atan2 argument: 272$awk 'BEGIN { atan2(1) }' 2>foo 273grep 'requires two arg' foo >/dev/null || echo 'BAD: T.misc atan2 error' 274 275# This should not core dump: 276$awk 'BEGIN { f() } 277function f(A) { delete A[1] } 278' 279if test -r core; then echo 1>&2 "BAD: T.misc delete dropped core"; fi 280 281# nasty one: should not be able to overwrite constants 282$awk 'BEGIN { gsub(/ana/,"anda","banana") 283 printf "the monkey ate a %s\n", "banana" } 284' >/dev/null 2>foo 285grep 'syntax error' foo >/dev/null || echo 'BAD: T.misc gsub banana error' 286 287# nasty one: should not be able to overwrite constants 288$awk 'BEGIN { sub(/ana/,"anda","banana") 289 printf "the monkey ate a %s\n", "banana" } 290' >/dev/null 2>foo 291grep 'syntax error' foo >/dev/null || echo 'BAD: T.misc sub banana error' 292 293# line numbers used to double-count comments 294$awk '# 295# 296# 297/x 298' >/dev/null 2>foo 299grep 'line [45]' foo >/dev/null || echo 'BAD: T.misc lineno' 300 301echo 'x 302\y' >foo1 303$awk 'BEGIN { print "x\f\r\b\v\a\\y" }' >foo2 304cmp -s foo1 foo2 || echo 'BAD: T.misc weird chars' 305 306echo 0 >foo1 307$awk ' BEGIN { exit } 308 { print } 309 END { print NR }' >foo2 310cmp -s foo1 foo2 || echo 'BAD: T.misc BEGIN exit' 311 312echo 1 >foo1 313$awk ' { exit } 314 END { print NR }' /etc/passwd >foo2 315cmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit' 316 317echo 1 >foo1 318$awk ' {i = 1; while (i <= NF) {if (i == NF) exit; i++ } } 319 END { print NR }' /etc/passwd >foo2 320cmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 2' 321 322echo 1 >foo1 323$awk ' function f() { 324 i = 1; while (i <= NF) {if (i == NF) return NR; i++ } 325 } 326 { if (f() == 1) exit } 327 END { print NR }' /etc/passwd >foo2 328cmp -s foo1 foo2 || echo 'BAD: T.misc while return' 329 330echo 1 >foo1 331$awk ' function f() { 332 split("a b c", arr) 333 for (i in arr) {if (i == 3) return NR; i++ } 334 } 335 { if (f() == 1) exit } 336 END { print NR }' /etc/passwd >foo2 337cmp -s foo1 foo2 || echo 'BAD: T.misc while return' 338 339echo 1 >foo1 340$awk ' {i = 1; do { if (i == NF) exit; i++ } while (i <= NF) } 341 END { print NR }' /etc/passwd >foo2 342cmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 3' 343 344echo 1 >foo1 345$awk ' function f() { 346 i = 1; do { if (i == NF) return NR; i++ } while (i <= NF) 347 } 348 { if (f() == 1) exit } 349 END { print NR }' /etc/passwd >foo2 350cmp -s foo1 foo2 || echo 'BAD: T.misc do return' 351 352echo 1 >foo1 353$awk ' {i = 1; do { if (i == NF) break; i++ } while (i <= NF); exit } 354 END { print NR }' /etc/passwd >foo2 355cmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 4' 356 357echo 1 >foo1 358$awk ' { n = split($0, x) 359 for (i in x) { 360 if (i == 1) 361 exit } } 362 END { print NR }' /etc/passwd >foo2 363cmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 5' 364 365echo XXXXXXXX >foo1 366$awk 'BEGIN { s = "ab\fc\rd\be" 367 t = s; gsub("[" s "]", "X", t); print t }' >foo2 368cmp -s foo1 foo2 || echo 'BAD: T.misc weird escapes in char class' 369 370$awk '{}' /etc/passwd glop/glop >foo 2>foo2 371grep "can't open.*glop" foo2 >/dev/null || echo "BAD: T.misc can't open" 372 373echo ' 374 375 376a 377aa 378 379b 380 381 382c 383 384' >foo 385echo 3 >foo1 386$awk 'BEGIN { RS = "" }; END { print NR }' foo >foo2 387cmp -s foo1 foo2 || echo 'BAD: T.misc RS botch' 388 389$awk 'BEGIN \ 390 { 391 print "hello, world" 392 } 393}}}' >foo1 2>foo2 394grep 'source line 4' foo2 >/dev/null 2>&1 || echo 'BAD: T.misc continuation line number' 395 396 397echo 111 222 333 >foo 398$awk '{ f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] }' foo >foo2 399echo 111 111 222 2 2 >foo1 400cmp -s foo1 foo2 || echo 'BAD: T.misc $f[1]++' 401 402 403# These should be syntax errors 404$awk . 2>foo 405grep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error . fails" 406 407$awk .. 2>foo 408grep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error .. fails" 409 410$awk .E. 2>foo 411grep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error .E. fails" 412 413$awk .++. 2>foo 414grep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error .++. fails" 415 416 417 418# These should be syntax errors 419$awk '$' 2>foo 420grep "unexpected" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error $ fails" 421 422$awk '{print $' 2>foo 423grep "unexpected" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error $2 fails" 424 425$awk '"' 2>foo 426grep "non-terminated" foo >/dev/null || echo 1>&2 "BAD: T.misc bare quote fails" 427 428 429# %c of 0 is explicit null byte 430 431./echo '3' >foo1 432$awk 'BEGIN {printf("%c%c\n", 0, 0) }' | wc | $awk '{print $3}' >foo2 433cmp -s foo1 foo2 || echo 'BAD: T.misc null byte' 434 435# non-terminated RE 436 437$awk /xyz >foo 2>&1 438grep "non-terminated" foo >/dev/null || echo 1>&2 "BAD: T.misc non-terminated RE" 439 440# next several were infinite loops, found by brian tsang. 441# this is his example: 442 443$awk 'BEGIN { 444 switch (substr("x",1,1)) { 445 case /ask.com/: 446 break 447 case "google": 448 break 449 } 450}' >foo 2>&1 451grep "illegal statement" foo >/dev/null || echo 1>&2 "BAD: T.misc looping syntax error 1" 452 453$awk 'BEGIN { s { c /./ } }' >foo 2>&1 454grep "illegal statement" foo >/dev/null || echo 1>&2 "BAD: T.misc looping syntax error 2" 455 456$awk 'BEGIN { s { c /../ } }' >foo 2>&1 457grep "illegal statement" foo >/dev/null || echo 1>&2 "BAD: T.misc looping syntax error 3" 458 459$awk 'BEGIN {printf "%2$s %1$s\n", "a", "b"}' >foo 2>&1 460grep "'$' not permitted in awk formats" foo >/dev/null || echo 1>&2 "BAD: T.misc '$' not permitted in formats" 461 462echo 'a 463b c 464de fg hi' >foo0 465$awk 'END { print NF, $0 }' foo0 >foo1 466awk '{ print NF, $0 }' foo0| tail -1 >foo2 467cmp -s foo1 foo2 || echo 'BAD: T.misc END must preserve $0' 468 469echo 'fg hi' >foo0 470$awk 'END { print NF, $0 }' foo0 >foo1 471awk '{ print NF, $0 }' foo0| tail -1 >foo2 472cmp -s foo1 foo2 || echo 'BAD: T.misc END must preserve $0' 473 474echo '' >foo0 475$awk 'END { print NF, $0 }' foo0 >foo1 476awk '{ print NF, $0 }' foo0| tail -1 >foo2 477cmp -s foo1 foo2 || echo 'BAD: T.misc END must preserve $0' 478 479# Check for nonzero exit status on I/O error. 480echo 'E 2' >foo1 481(trap '' PIPE; "$awk" 'BEGIN { print "hi"; }' 2>/dev/null; echo "E $?" >foo2) | : 482cmp -s foo1 foo2 || echo 'BAD: T.misc exit status on I/O error' 483