Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
60ab2483 SP |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
6b0e82e1 | 5 | require './test.pl'; |
43ece5b1 | 6 | set_up_inc('../lib'); |
60ab2483 SP |
7 | } |
8 | ||
d69c4304 DM |
9 | use Config; |
10 | ||
1b92e694 | 11 | plan(tests => 78); |
a687059c | 12 | |
da5a0da2 | 13 | my $exception_134139 = "Use of strings with code points over 0xFF as arguments to vec is forbidden"; |
210db7fc | 14 | |
60ab2483 | 15 | is(vec($foo,0,1), 0); |
9f621bb0 | 16 | is(length($foo), undef); |
a687059c | 17 | vec($foo,0,1) = 1; |
60ab2483 SP |
18 | is(length($foo), 1); |
19 | is(unpack('C',$foo), 1); | |
20 | is(vec($foo,0,1), 1); | |
a687059c | 21 | |
60ab2483 | 22 | is(vec($foo,20,1), 0); |
a687059c | 23 | vec($foo,20,1) = 1; |
60ab2483 SP |
24 | is(vec($foo,20,1), 1); |
25 | is(length($foo), 3); | |
26 | is(vec($foo,1,8), 0); | |
a687059c | 27 | vec($foo,1,8) = 0xf1; |
60ab2483 SP |
28 | is(vec($foo,1,8), 0xf1); |
29 | is((unpack('C',substr($foo,1,1)) & 255), 0xf1); | |
30 | is(vec($foo,2,4), 1);; | |
31 | is(vec($foo,3,4), 15); | |
deb3007b | 32 | vec($Vec, 0, 32) = 0xbaddacab; |
60ab2483 SP |
33 | is($Vec, "\xba\xdd\xac\xab"); |
34 | is(vec($Vec, 0, 32), 3135089835); | |
a687059c | 35 | |
4ebbc975 GS |
36 | # ensure vec() handles numericalness correctly |
37 | $foo = $bar = $baz = 0; | |
38 | vec($foo = 0,0,1) = 1; | |
39 | vec($bar = 0,1,1) = 1; | |
40 | $baz = $foo | $bar; | |
60ab2483 SP |
41 | ok($foo eq "1" && $foo == 1); |
42 | ok($bar eq "2" && $bar == 2); | |
43 | ok("$foo $bar $baz" eq "1 2 3"); | |
fe58ced6 MG |
44 | |
45 | # error cases | |
46 | ||
47 | $x = eval { vec $foo, 0, 3 }; | |
5a2b2173 | 48 | like($@, qr/^Illegal number of bits in vec/); |
60ab2483 | 49 | $@ = undef; |
fe58ced6 | 50 | $x = eval { vec $foo, 0, 0 }; |
5a2b2173 | 51 | like($@, qr/^Illegal number of bits in vec/); |
60ab2483 | 52 | $@ = undef; |
fe58ced6 | 53 | $x = eval { vec $foo, 0, -13 }; |
5a2b2173 | 54 | like($@, qr/^Illegal number of bits in vec/); |
60ab2483 | 55 | $@ = undef; |
fe58ced6 | 56 | $x = eval { vec($foo, -1, 4) = 2 }; |
5a2b2173 | 57 | like($@, qr/^Negative offset to vec in lvalue context/); |
60ab2483 SP |
58 | $@ = undef; |
59 | ok(! vec('abcd', 7, 8)); | |
246fae53 MG |
60 | |
61 | # UTF8 | |
62 | # N.B. currently curiously coded to circumvent bugs elswhere in UTF8 handling | |
63 | ||
64 | $foo = "\x{100}" . "\xff\xfe"; | |
65 | $x = substr $foo, 1; | |
60ab2483 SP |
66 | is(vec($x, 0, 8), 255); |
67 | $@ = undef; | |
315f3fc1 | 68 | { |
da5a0da2 | 69 | local $@; |
270d3c5d | 70 | eval { vec($foo, 1, 8) }; |
da5a0da2 JK |
71 | like($@, qr/$exception_134139/, |
72 | "Caught exception: code point over 0xFF used as argument to vec"); | |
270d3c5d KW |
73 | $@ = undef; |
74 | eval { vec($foo, 1, 8) = 13 }; | |
da5a0da2 JK |
75 | like($@, qr/$exception_134139/, |
76 | "Caught exception: code point over 0xFF used as argument to vec"); | |
315f3fc1 | 77 | } |
33b45480 | 78 | $foo = "\x{100}" . "\xff\xfe"; |
246fae53 MG |
79 | $x = substr $foo, 1; |
80 | vec($x, 2, 4) = 7; | |
60ab2483 | 81 | is($x, "\xff\xf7"); |
246fae53 MG |
82 | |
83 | # mixed magic | |
84 | ||
85 | $foo = "\x61\x62\x63\x64\x65\x66"; | |
60ab2483 | 86 | is(vec(substr($foo, 2, 2), 0, 16), 25444); |
246fae53 | 87 | vec(substr($foo, 1,3), 5, 4) = 3; |
60ab2483 | 88 | is($foo, "\x61\x62\x63\x34\x65\x66"); |
24aef97f HS |
89 | |
90 | # A variation of [perl #20933] | |
91 | { | |
92 | my $s = ""; | |
93 | vec($s, 0, 1) = 0; | |
94 | vec($s, 1, 1) = 1; | |
95 | my @r; | |
96 | $r[$_] = \ vec $s, $_, 1 for (0, 1); | |
60ab2483 | 97 | ok(!(${ $r[0] } != 0 || ${ $r[1] } != 1)); |
24aef97f | 98 | } |
0607bed5 EB |
99 | |
100 | ||
101 | my $destroyed; | |
102 | { package Class; DESTROY { ++$destroyed; } } | |
103 | ||
104 | $destroyed = 0; | |
105 | { | |
106 | my $x = ''; | |
107 | vec($x,0,1) = 0; | |
108 | $x = bless({}, 'Class'); | |
109 | } | |
2154eca7 | 110 | is($destroyed, 1, 'Timely scalar destruction with lvalue vec'); |
ee3818ca | 111 | |
2484f8db FC |
112 | use constant roref => \1; |
113 | eval { for (roref) { vec($_,0,1) = 1 } }; | |
ee3818ca FC |
114 | like($@, qr/^Modification of a read-only value attempted at /, |
115 | 'err msg when modifying read-only refs'); | |
fc9668ae DM |
116 | |
117 | ||
118 | { | |
119 | # downgradeable utf8 strings should be downgraded before accessing | |
120 | # the byte string. | |
121 | # See the p5p thread with Message-ID: | |
122 | # <CAMx+QJ6SAv05nmpnc7bmp0Wo+sjcx=ssxCcE-P_PZ8HDuCQd9A@mail.gmail.com> | |
123 | ||
124 | ||
125 | my $x = substr "\x{100}\xff\xfe", 1; # a utf8 string with all ords < 256 | |
126 | my $v; | |
127 | $v = vec($x, 0, 8); | |
128 | is($v, 255, "downgraded utf8 try 1"); | |
129 | $v = vec($x, 0, 8); | |
130 | is($v, 255, "downgraded utf8 try 2"); | |
131 | } | |
33a10326 FC |
132 | |
133 | # [perl #128260] assertion failure with \vec %h, \vec @h | |
134 | { | |
135 | my %h = 1..100; | |
136 | my @a = 1..100; | |
137 | is ${\vec %h, 0, 1}, vec(scalar %h, 0, 1), '\vec %h'; | |
138 | is ${\vec @a, 0, 1}, vec(scalar @a, 0, 1), '\vec @a'; | |
139 | } | |
d69c4304 DM |
140 | |
141 | ||
142 | # [perl #130915] heap-buffer-overflow in Perl_do_vecget | |
143 | ||
144 | { | |
145 | # ensure that out-of-STRLEN-range offsets are handled correctly. This | |
146 | # partially duplicates some tests above, but those cases are repeated | |
147 | # here for completeness. | |
148 | # | |
149 | # Note that all the 'Out of memory!' errors trapped eval {} are 'fake' | |
150 | # croaks generated by pp_vec() etc when they have detected something | |
151 | # that would have otherwise overflowed. The real 'Out of memory!' | |
152 | # error thrown by safesysrealloc() etc is not trappable. If it were | |
153 | # accidentally triggered in this test script, the script would exit at | |
154 | # that point. | |
155 | ||
156 | ||
157 | my $s = "abcdefghijklmnopqrstuvwxyz"; | |
158 | my $x; | |
159 | ||
160 | # offset is SvIOK_UV | |
161 | ||
162 | $x = vec($s, ~0, 8); | |
163 | is($x, 0, "RT 130915: UV_MAX rval"); | |
164 | eval { vec($s, ~0, 8) = 1 }; | |
165 | like($@, qr/^Out of memory!/, "RT 130915: UV_MAX lval"); | |
166 | ||
167 | # offset is negative | |
168 | ||
169 | $x = vec($s, -1, 8); | |
170 | is($x, 0, "RT 130915: -1 rval"); | |
171 | eval { vec($s, -1, 8) = 1 }; | |
172 | like($@, qr/^Negative offset to vec in lvalue context/, | |
173 | "RT 130915: -1 lval"); | |
174 | ||
175 | # offset positive but doesn't fit in a STRLEN | |
176 | ||
177 | SKIP: { | |
178 | skip 'IV is no longer than size_t', 2 | |
179 | if $Config{ivsize} <= $Config{sizesize}; | |
180 | ||
181 | my $size_max = (1 << (8 *$Config{sizesize})) - 1; | |
182 | my $sm2 = $size_max * 2; | |
183 | ||
184 | $x = vec($s, $sm2, 8); | |
185 | is($x, 0, "RT 130915: size_max*2 rval"); | |
186 | eval { vec($s, $sm2, 8) = 1 }; | |
187 | like($@, qr/^Out of memory!/, "RT 130915: size_max*2 lval"); | |
188 | } | |
67dd6f35 DM |
189 | |
190 | # (offset * num-bytes) could overflow | |
191 | ||
192 | for my $power (1..3) { | |
193 | my $bytes = (1 << $power); | |
194 | my $biglog2 = $Config{sizesize} * 8 - $power; | |
195 | for my $i (0..1) { | |
196 | my $offset = (1 << $biglog2) - $i; | |
197 | $x = vec($s, $offset, $bytes*8); | |
198 | is($x, 0, "large offset: bytes=$bytes biglog2=$biglog2 i=$i: rval"); | |
199 | eval { vec($s, $offset, $bytes*8) = 1; }; | |
200 | like($@, qr/^Out of memory!/, | |
201 | "large offset: bytes=$bytes biglog2=$biglog2 i=$i: rval"); | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | # Test multi-byte gets partially beyond the end of the string. | |
207 | # It's supposed to pretend there is a stream of \0's following the string. | |
208 | ||
209 | { | |
210 | my $s = "\x01\x02\x03\x04\x05\x06\x07"; | |
211 | my $s0 = $s . ("\0" x 8); | |
212 | ||
213 | for my $bytes (1, 2, 4, 8) { | |
214 | for my $offset (0..$bytes) { | |
215 | if ($Config{ivsize} < $bytes) { | |
216 | pass("skipping multi-byte bytes=$bytes offset=$offset"); | |
217 | next; | |
218 | } | |
219 | no warnings 'portable'; | |
220 | is (vec($s, 8 - $offset, $bytes*8), | |
221 | vec($s0, 8 - $offset, $bytes*8), | |
222 | "multi-byte bytes=$bytes offset=$offset"); | |
223 | } | |
224 | } | |
d69c4304 | 225 | } |
1b92e694 DM |
226 | |
227 | # RT #131083 maybe-lvalue out of range should only croak if assigned to | |
228 | ||
229 | { | |
230 | sub RT131083 { if ($_[0]) { $_[1] = 1; } $_[1]; } | |
231 | my $s = "abc"; | |
232 | my $off = -1; | |
233 | my $v = RT131083(0, vec($s, $off, 8)); | |
234 | is($v, 0, "RT131083 rval -1"); | |
235 | $v = eval { RT131083(1, vec($s, $off, 8)); }; | |
236 | like($@, qr/Negative offset to vec in lvalue context/, "RT131083 lval -1"); | |
237 | ||
238 | $off = ~0; | |
239 | my $v = RT131083(0, vec($s, $off, 8)); | |
240 | is($v, 0, "RT131083 rval ~0"); | |
241 | $v = eval { RT131083(1, vec($s, $off, 8)); }; | |
242 | like($@, qr/Out of memory!/, "RT131083 lval ~0"); | |
243 | } | |
da5a0da2 JK |
244 | |
245 | { | |
246 | # Adapting test formerly in t/lib/warnings/doop | |
247 | ||
248 | local $@; | |
249 | my $foo = "\x{100}" . "\xff\xfe"; | |
250 | eval { vec($foo, 1, 8) }; | |
251 | like($@, qr/$exception_134139/, | |
252 | "RT 134139: Use of strings with code points over 0xFF as arguments to 'vec' is now forbidden"); | |
253 | } |