xref: /freebsd/contrib/one-true-awk/testdir/T.overflow (revision 02e9120893770924227138ba49df1edb3896112a)
1echo T.overflow: test some overflow conditions
2
3awk=${awk-../a.out}
4
5$awk 'BEGIN {
6 	for (i = 0; i < 1000; i++) printf("abcdefghijklmnopqsrtuvwxyz")
7 	printf("\n")
8 	exit
9}' >foo1
10$awk '{print}' foo1 >foo2
11cmp -s foo1 foo2 || echo 'BAD: T.overflow record 1'
12
13echo 'abcdefghijklmnopqsrtuvwxyz' >foo1
14echo hello | $awk '
15 { for (i = 1; i < 500; i++) s = s "abcdefghijklmnopqsrtuvwxyz "
16   $0 = s
17   print $1
18 }'  >foo2
19cmp -s foo1 foo2 || echo 'BAD: T.overflow abcdef'
20
21# default input record 3072, fields 200:
22$awk '
23BEGIN {
24	for (j = 0; j < 2; j++) {
25		for (i = 0; i < 500; i++)
26			printf(" 123456789")
27		printf("\n");
28	}
29} ' >foo1
30$awk '{$1 = " 123456789"; print}' foo1 >foo2
31cmp -s foo1 foo2 || echo 'BAD: T.overflow -mr -mf set $1'
32
33$awk '
34BEGIN {
35	for (j = 0; j < 2; j++) {
36		for (i = 0; i < 500; i++)
37			printf(" 123456789")
38		printf("\n");
39	}
40} ' >foo
41$awk  '{print NF}' foo >foo1
42echo '500
43500' >foo2
44cmp -s foo1 foo2 || echo 'BAD: T.overflow -mr -mf NF'
45
46rm -f core
47# this should not drop core
48$awk 'BEGIN {
49	for (i = 1; i < 1000; i++) s = s "a-z"
50	if ("x" ~ "[" s "]")
51		print "ugh"
52}' >foo 2>foo
53test -r core && echo 1>&2 "BAD: T.overflow too long char class dropped core"
54
55echo 4000004 >foo1
56$awk '
57BEGIN {
58	x1 = sprintf("%1000000s\n", "hello")
59	x2 = sprintf("%-1000000s\n", "world")
60	x3 = sprintf("%1000000.1000000s\n", "goodbye")
61	x4 = sprintf("%-1000000.1000000s\n", "goodbye")
62	print length(x1 x2 x3 x4)
63}' >foo2
64cmp -s foo1 foo2 || echo 'BAD: T.overflow huge sprintfs'
65
66echo 0 >foo1
67$awk '
68BEGIN {
69	for (i = 0; i < 100000; i++)
70		x[i] = i
71	for (i in x)
72		delete x[i]
73	n = 0
74	for (i in x)
75		n++
76	print n
77}' >foo2
78cmp -s foo1 foo2 || echo 'BAD: T.overflow big array'
79
80echo x >foo1
81$awk '{print $40000000000000}' <foo1 >foo2 2>foo
82grep "out of range field" foo >/dev/null || echo 1>&2 "BAD: T.overflow \$400000"
83
84rm -rf /tmp/awktestfoo*
85$awk 'BEGIN { for (i=1; i <= 1000; i++) print i >("/tmp/awktestfoo" i) }'
86ls /tmp/awktestfoo* | grep '1000' >/dev/null || echo 1>&2 "BAD: T.overflow openfiles"
87