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