15 my $exception_134139 = "Use of strings with code points over 0xFF as arguments to vec is forbidden";
19 is(length($foo), undef);
22 is(unpack('C',$foo), 1);
25 is(vec($foo,20,1), 0);
27 is(vec($foo,20,1), 1);
31 is(vec($foo,1,8), 0xf1);
32 is((unpack('C',substr($foo,1,1)) & 255), 0xf1);
33 is(vec($foo,2,4), 1);;
34 is(vec($foo,3,4), 15);
37 vec($Vec, 0, 32) = 0xbaddacab;
38 is($Vec, "\xba\xdd\xac\xab");
39 is(vec($Vec, 0, 32), 3135089835);
41 # ensure vec() handles numericalness correctly
43 $foo = $bar = $baz = 0;
44 vec($foo = 0,0,1) = 1;
45 vec($bar = 0,1,1) = 1;
47 ok($foo eq "1" && $foo == 1);
48 ok($bar eq "2" && $bar == 2);
49 ok("$foo $bar $baz" eq "1 2 3");
53 my $x = eval { vec $foo, 0, 3 };
54 like($@, qr/^Illegal number of bits in vec/);
56 $x = eval { vec $foo, 0, 0 };
57 like($@, qr/^Illegal number of bits in vec/);
59 $x = eval { vec $foo, 0, -13 };
60 like($@, qr/^Illegal number of bits in vec/);
62 $x = eval { vec($foo, -1, 4) = 2 };
63 like($@, qr/^Negative offset to vec in lvalue context/);
65 ok(! vec('abcd', 7, 8));
68 # N.B. currently curiously coded to circumvent bugs elswhere in UTF8 handling
70 $foo = "\x{100}" . "\xff\xfe";
72 is(vec($x, 0, 8), 255);
76 eval { my $foo = vec($foo, 1, 8) };
77 like($@, qr/$exception_134139/,
78 "Caught exception: code point over 0xFF used as argument to vec");
80 eval { vec($foo, 1, 8) = 13 };
81 like($@, qr/$exception_134139/,
82 "Caught exception: code point over 0xFF used as argument to vec");
84 $foo = "\x{100}" . "\xff\xfe";
91 $foo = "\x61\x62\x63\x64\x65\x66";
92 is(vec(substr($foo, 2, 2), 0, 16), 25444);
93 vec(substr($foo, 1,3), 5, 4) = 3;
94 is($foo, "\x61\x62\x63\x34\x65\x66");
96 # A variation of [perl #20933]
102 $r[$_] = \ vec $s, $_, 1 for (0, 1);
103 ok(!(${ $r[0] } != 0 || ${ $r[1] } != 1));
108 { package Class; DESTROY { ++$destroyed; } }
114 $x = bless({}, 'Class');
116 is($destroyed, 1, 'Timely scalar destruction with lvalue vec');
118 use constant roref => \1;
119 eval { for (roref) { vec($_,0,1) = 1 } };
120 like($@, qr/^Modification of a read-only value attempted at /,
121 'err msg when modifying read-only refs');
125 # downgradeable utf8 strings should be downgraded before accessing
127 # See the p5p thread with Message-ID:
128 # <CAMx+QJ6SAv05nmpnc7bmp0Wo+sjcx=ssxCcE-P_PZ8HDuCQd9A@mail.gmail.com>
131 my $x = substr "\x{100}\xff\xfe", 1; # a utf8 string with all ords < 256
134 is($v, 255, "downgraded utf8 try 1");
136 is($v, 255, "downgraded utf8 try 2");
139 # [perl #128260] assertion failure with \vec %h, \vec @h
143 is ${\vec %h, 0, 1}, vec(scalar %h, 0, 1), '\vec %h';
144 is ${\vec @a, 0, 1}, vec(scalar @a, 0, 1), '\vec @a';
148 # [perl #130915] heap-buffer-overflow in Perl_do_vecget
151 # ensure that out-of-STRLEN-range offsets are handled correctly. This
152 # partially duplicates some tests above, but those cases are repeated
153 # here for completeness.
155 # Note that all the 'Out of memory!' errors trapped eval {} are 'fake'
156 # croaks generated by pp_vec() etc when they have detected something
157 # that would have otherwise overflowed. The real 'Out of memory!'
158 # error thrown by safesysrealloc() etc is not trappable. If it were
159 # accidentally triggered in this test script, the script would exit at
163 my $s = "abcdefghijklmnopqrstuvwxyz";
169 is($x, 0, "RT 130915: UV_MAX rval");
170 eval { vec($s, ~0, 8) = 1 };
171 like($@, qr/^Out of memory!/, "RT 130915: UV_MAX lval");
176 is($x, 0, "RT 130915: -1 rval");
177 eval { vec($s, -1, 8) = 1 };
178 like($@, qr/^Negative offset to vec in lvalue context/,
179 "RT 130915: -1 lval");
181 # offset positive but doesn't fit in a STRLEN
184 skip 'IV is no longer than size_t', 2
185 if $Config{ivsize} <= $Config{sizesize};
187 my $size_max = (1 << (8 *$Config{sizesize})) - 1;
188 my $sm2 = $size_max * 2;
190 $x = vec($s, $sm2, 8);
191 is($x, 0, "RT 130915: size_max*2 rval");
192 eval { vec($s, $sm2, 8) = 1 };
193 like($@, qr/^Out of memory!/, "RT 130915: size_max*2 lval");
196 # (offset * num-bytes) could overflow
198 for my $power (1..3) {
199 my $bytes = (1 << $power);
200 my $biglog2 = $Config{sizesize} * 8 - $power;
202 no warnings 'portable';
203 my $offset = (1 << $biglog2) - $i;
204 $x = vec($s, $offset, $bytes*8);
205 is($x, 0, "large offset: bytes=$bytes biglog2=$biglog2 i=$i: rval");
206 eval { vec($s, $offset, $bytes*8) = 1; };
207 like($@, qr/^Out of memory!/,
208 "large offset: bytes=$bytes biglog2=$biglog2 i=$i: rval");
213 # Test multi-byte gets partially beyond the end of the string.
214 # It's supposed to pretend there is a stream of \0's following the string.
217 my $s = "\x01\x02\x03\x04\x05\x06\x07";
218 my $s0 = $s . ("\0" x 8);
220 no warnings 'portable';
221 for my $bytes (1, 2, 4, 8) {
222 for my $offset (0..$bytes) {
223 if ($Config{ivsize} < $bytes) {
224 pass("skipping multi-byte bytes=$bytes offset=$offset");
227 no warnings 'portable';
228 is (vec($s, 8 - $offset, $bytes*8),
229 vec($s0, 8 - $offset, $bytes*8),
230 "multi-byte bytes=$bytes offset=$offset");
235 # RT #131083 maybe-lvalue out of range should only croak if assigned to
238 sub RT131083 { if ($_[0]) { $_[1] = 1; } $_[1]; }
241 my $v = RT131083(0, vec($s, $off, 8));
242 is($v, 0, "RT131083 rval -1");
243 $v = eval { RT131083(1, vec($s, $off, 8)); };
244 like($@, qr/Negative offset to vec in lvalue context/, "RT131083 lval -1");
247 $v = RT131083(0, vec($s, $off, 8));
248 is($v, 0, "RT131083 rval ~0");
249 $v = eval { RT131083(1, vec($s, $off, 8)); };
250 like($@, qr/Out of memory!/, "RT131083 lval ~0");
254 # Adapting test formerly in t/lib/warnings/doop
257 my $foo = "\x{100}" . "\xff\xfe";
258 eval { my $bar = vec($foo, 1, 8) };
259 like($@, qr/$exception_134139/,
260 "RT 134139: Use of strings with code points over 0xFF as arguments to 'vec' is now forbidden");