1#!/bin/bash
2
3if [[ -z "$AWK" || -z "$WORKDIR" ]]; then
4    printf '$AWK and $WORKDIR must be set\n' >&2
5    exit 1
6fi
7
8TEMP0=$WORKDIR/test.temp.0
9TEMP1=$WORKDIR/test.temp.1
10TEMP2=$WORKDIR/test.temp.2
11
12RESULT=0
13
14fail() {
15	echo "$1" >&2
16	RESULT=1
17}
18
19echo T.overflow: test some overflow conditions
20
21$AWK 'BEGIN {
22 	for (i = 0; i < 1000; i++) printf("abcdefghijklmnopqsrtuvwxyz")
23 	printf("\n")
24 	exit
25}' > $TEMP1
26$AWK '{print}' $TEMP1 > $TEMP2
27cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.overflow record 1'
28
29echo 'abcdefghijklmnopqsrtuvwxyz' > $TEMP1
30echo hello | $AWK '
31 { for (i = 1; i < 500; i++) s = s "abcdefghijklmnopqsrtuvwxyz "
32   $0 = s
33   print $1
34 }'  > $TEMP2
35cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.overflow abcdef'
36
37# default input record 3072, fields 200:
38$AWK '
39BEGIN {
40	for (j = 0; j < 2; j++) {
41		for (i = 0; i < 500; i++)
42			printf(" 123456789")
43		printf("\n");
44	}
45} ' > $TEMP1
46$AWK '{$1 = " 123456789"; print}' $TEMP1 > $TEMP2
47cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.overflow -mr -mf set $1'
48
49$AWK '
50BEGIN {
51	for (j = 0; j < 2; j++) {
52		for (i = 0; i < 500; i++)
53			printf(" 123456789")
54		printf("\n");
55	}
56} ' > $TEMP0
57$AWK  '{print NF}' $TEMP0 > $TEMP1
58echo '500
59500' > $TEMP2
60cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.overflow -mr -mf NF'
61
62rm -f core
63# this should not drop core
64$AWK 'BEGIN {
65	for (i = 1; i < 1000; i++) s = s "a-z"
66	if ("x" ~ "[" s "]")
67		print "ugh"
68}' > $TEMP0 2> $TEMP0
69[[ $? -eq 139 ]] && fail "BAD: T.overflow too long char class dropped core"
70
71echo 4000004 > $TEMP1
72$AWK '
73BEGIN {
74	x1 = sprintf("%1000000s\n", "hello")
75	x2 = sprintf("%-1000000s\n", "world")
76	x3 = sprintf("%1000000.1000000s\n", "goodbye")
77	x4 = sprintf("%-1000000.1000000s\n", "goodbye")
78	print length(x1 x2 x3 x4)
79}' > $TEMP2
80cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.overflow huge sprintfs'
81
82echo 0 > $TEMP1
83$AWK '
84BEGIN {
85	for (i = 0; i < 100000; i++)
86		x[i] = i
87	for (i in x)
88		delete x[i]
89	n = 0
90	for (i in x)
91		n++
92	print n
93}' > $TEMP2
94cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.overflow big array'
95
96echo x > $TEMP1
97$AWK '{print $40000000000000}' < $TEMP1 > $TEMP2 2> $TEMP0
98grep "out of range field" $TEMP0 >/dev/null || fail "BAD: T.overflow \$400000"
99
100rm -rf /tmp/awktestfoo*
101$AWK 'BEGIN { for (i=1; i <= 1000; i++) print i >("/tmp/awktestfoo" i) }'
102ls /tmp/awktestfoo* | grep '1000' >/dev/null || fail "BAD: T.overflow openfiles"
103
104exit $RESULT
105