Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saner numeric strings #5762

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions Zend/tests/add_006.phpt
Expand Up @@ -11,9 +11,12 @@ $s2 = "876222numeric";
$s3 = "48474874";
$s4 = "25.68";

$c = $i + $s1;
var_dump($c);

try {
$c = $i + $s1;
var_dump($c);
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
$c = $i + $s2;
var_dump($c);

Expand All @@ -23,8 +26,12 @@ var_dump($c);
$c = $i + $s4;
var_dump($c);

$c = $s1 + $i;
var_dump($c);
try {
$c = $s1 + $i;
var_dump($c);
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

$c = $s2 + $i;
var_dump($c);
Expand All @@ -38,18 +45,15 @@ var_dump($c);
echo "Done\n";
?>
--EXPECTF--
Warning: A non-numeric value encountered in %s on line %d
int(75636)
Unsupported operand types: int + string

Notice: A non well formed numeric value encountered in %s on line %d
Warning: A non-numeric value encountered in %s on line %d
int(951858)
int(48550510)
float(75661.68)
Unsupported operand types: string + int

Warning: A non-numeric value encountered in %s on line %d
int(75636)

Notice: A non well formed numeric value encountered in %s on line %d
int(951858)
int(48550510)
float(75661.68)
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/bug24773.phpt
Expand Up @@ -6,7 +6,7 @@ Bug #24773 (unset() of integers treated as arrays causes a crash)
unset($array["lvl1"]["lvl2"]["b"]);
?>
--EXPECTF--
Fatal error: Uncaught Error: Cannot use string offset as an array in %s:%d
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
25 changes: 17 additions & 8 deletions Zend/tests/bug31098.phpt
Expand Up @@ -17,16 +17,28 @@ var_dump(isset($a['b']));

$simpleString = "Bogus String Text";
echo isset($simpleString->wrong)?"bug\n":"ok\n";
echo isset($simpleString["wrong"])?"bug\n":"ok\n";
try {
echo isset($simpleString["wrong"])?"bug\n":"ok\n";
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
echo isset($simpleString[-20])?"bug\n":"ok\n";
echo isset($simpleString[0])?"ok\n":"bug\n";
echo isset($simpleString["0"])?"ok\n":"bug\n";
echo isset($simpleString["16"])?"ok\n":"bug\n";
echo isset($simpleString["17"])?"bug\n":"ok\n";
echo $simpleString->wrong === null?"ok\n":"bug\n";
echo $simpleString["wrong"] === "B"?"ok\n":"bug\n";
try {
echo $simpleString["wrong"] === "B"?"ok\n":"bug\n";
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
echo $simpleString["0"] === "B"?"ok\n":"bug\n";
$simpleString["wrong"] = "f";
try {
$simpleString["wrong"] = "f";
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
echo $simpleString["0"] === "f"?"ok\n":"bug\n";
?>
--EXPECTF--
Expand All @@ -46,10 +58,7 @@ ok

Warning: Attempt to read property "wrong" on string in %s on line %d
ok

Warning: Illegal string offset "wrong" in %s on line %d
ok
Cannot access offset of type string on string
ok

Warning: Illegal string offset "wrong" in %s on line %d
Cannot access offset of type string on string
ok
7 changes: 2 additions & 5 deletions Zend/tests/bug39018_2.phpt
Expand Up @@ -8,11 +8,8 @@ error_reporting(E_ALL);
$foo = 'test';
$x = @$foo[6];

print @($foo[100] + $foo[130]);

print "\nDone\n";
var_dump(@($foo[100] . $foo[130]));

?>
--EXPECT--
0
Done
string(0) ""
10 changes: 6 additions & 4 deletions Zend/tests/bug53432.phpt
Expand Up @@ -16,7 +16,11 @@ var_dump($str[-1] = 'a');
var_dump($str);

$str = '';
var_dump($str['foo'] = 'a');
try {
var_dump($str['foo'] = 'a');
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump($str);

$str = '';
Expand Down Expand Up @@ -53,9 +57,7 @@ string(6) " a"
Warning: Illegal string offset -1 in %s on line %d
NULL
string(0) ""

Warning: Illegal string offset "foo" in %s on line %d
string(1) "a"
Cannot access offset of type string on string
string(1) "a"
Error: [] operator not supported for strings
string(0) ""
Expand Down
4 changes: 2 additions & 2 deletions Zend/tests/bug64578.phpt
Expand Up @@ -5,10 +5,10 @@ Bug #64578 (debug_backtrace in set_error_handler corrupts zend heap: segfault)

set_error_handler(function($no, $err) { var_dump($err); });

function x($s) { $s['a'] = 1; };
function x($s) { $s['2a'] = 1; };
$y = '1';
x($y);
print_r($y);
--EXPECT--
string(25) "Illegal string offset "a""
string(26) "Illegal string offset "2a""
1
19 changes: 0 additions & 19 deletions Zend/tests/bug72057.phpt

This file was deleted.

4 changes: 2 additions & 2 deletions Zend/tests/bug73792.phpt
Expand Up @@ -4,15 +4,15 @@ Bug #73792 (invalid foreach loop hangs script)
<?php
$a = 'aaa';

foreach ($a['bbb'] as &$value) {
foreach ($a['2bbb'] as &$value) {
echo 'loop';
}

unset($value);
echo 'done';
?>
--EXPECTF--
Warning: Illegal string offset "bbb" in %s on line %d
Warning: Illegal string offset "2bbb" in %s on line %d

Fatal error: Uncaught Error: Cannot iterate on string offsets by reference in %sbug73792.php:4
Stack trace:
Expand Down
4 changes: 2 additions & 2 deletions Zend/tests/bug76534.phpt
Expand Up @@ -7,10 +7,10 @@ set_error_handler(function ($severity, $message, $file, $line) {
});

$x = "foo";
$y = &$x["bar"];
$y = &$x["2bar"];
?>
--EXPECTF--
Fatal error: Uncaught Exception: Illegal string offset "bar" in %s:%d
Fatal error: Uncaught Exception: Illegal string offset "2bar" in %s:%d
Stack trace:
#0 %sbug76534.php(%d): {closure}(2, 'Illegal string ...', '%s', %d)
#1 {main}
Expand Down
6 changes: 3 additions & 3 deletions Zend/tests/const_dereference_002.phpt
Expand Up @@ -6,12 +6,12 @@ error_reporting(E_ALL);

var_dump("foobar"[3]);
var_dump("foobar"[2][0]);
var_dump("foobar"["foo"]["bar"]);
var_dump("foobar"["0foo"]["0bar"]);
--EXPECTF--
string(1) "b"
string(1) "o"

Warning: Illegal string offset "foo" in %s on line %d
Warning: Illegal string offset "0foo" in %s on line %d

Warning: Illegal string offset "bar" in %s on line %d
Warning: Illegal string offset "0bar" in %s on line %d
string(1) "f"
2 changes: 1 addition & 1 deletion Zend/tests/constant_expressions_dynamic.phpt
Expand Up @@ -5,7 +5,7 @@ Dynamic Constant Expressions

const C_0 = 0;
const C_1 = 1;
const C_foo = "foo";
const C_foo = "0foo";
const C_arr = [0 => 0, "foo" => "foo"];

const T_1 = C_1 | 2;
Expand Down
14 changes: 4 additions & 10 deletions Zend/tests/indexing_001.phpt
Expand Up @@ -75,19 +75,13 @@ array(1) {
}
}

Warning: Illegal string offset "foo" in %s on line %d

Warning: Array to string conversion in %s on line %d

Warning: Only the first byte will be assigned to the string offset in %s on line %d
string(1) "A"

Warning: Illegal string offset "foo" in %s on line %d
Cannot access offset of type string on string
string(0) ""

Warning: Array to string conversion in %s on line %d

Warning: Only the first byte will be assigned to the string offset in %s on line %d
string(1) "A"
Cannot access offset of type string on string
string(1) " "
Cannot use a scalar value as an array
float(0.1)
array(1) {
Expand Down
8 changes: 4 additions & 4 deletions Zend/tests/int_conversion_exponents.phpt
Expand Up @@ -39,14 +39,14 @@ int(-1234500000)
int(1234500000)
int(-1234500000)

Notice: A non well formed numeric value encountered in %s on line %d
Warning: A non-numeric value encountered in %s on line %d
int(1234500000)

Notice: A non well formed numeric value encountered in %s on line %d
Warning: A non-numeric value encountered in %s on line %d
int(-1234500000)

Notice: A non well formed numeric value encountered in %s on line %d
Warning: A non-numeric value encountered in %s on line %d
int(1234500000)

Notice: A non well formed numeric value encountered in %s on line %d
Warning: A non-numeric value encountered in %s on line %d
int(-1234500000)
22 changes: 0 additions & 22 deletions Zend/tests/non_well_formed_param_exception.phpt

This file was deleted.

86 changes: 86 additions & 0 deletions Zend/tests/numeric_strings/array_offset.phpt
@@ -0,0 +1,86 @@
--TEST--
Using different sorts of numerical strings as an array offset
--FILE--
<?php

$arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

var_dump($arr["7"]);
var_dump($arr["7.5"]);
var_dump($arr[" 7"]);
var_dump($arr[" 7.5"]);
var_dump($arr[" 7 "]);
var_dump($arr[" 7.5 "]);
var_dump($arr["7 "]);
var_dump($arr["7.5 "]);
var_dump($arr["7str"]);
var_dump($arr["7.5str"]);
var_dump($arr[" 7str"]);
var_dump($arr[" 7.5str"]);
var_dump($arr[" 7 str"]);
var_dump($arr[" 7.5 str"]);
var_dump($arr["7 str"]);
var_dump($arr["7.5 str"]);
var_dump($arr["0xA"]);
var_dump($arr["0b10"]);
var_dump($arr["07"]);

echo "Done\n";
?>
--EXPECTF--
int(7)

Notice: Undefined array key "7.5" in %s on line 6
NULL

Notice: Undefined array key " 7" in %s on line 7
NULL

Notice: Undefined array key " 7.5" in %s on line 8
NULL

Notice: Undefined array key " 7 " in %s on line 9
NULL

Notice: Undefined array key " 7.5 " in %s on line 10
NULL

Notice: Undefined array key "7 " in %s on line 11
NULL

Notice: Undefined array key "7.5 " in %s on line 12
NULL

Notice: Undefined array key "7str" in %s on line 13
NULL

Notice: Undefined array key "7.5str" in %s on line 14
NULL

Notice: Undefined array key " 7str" in %s on line 15
NULL

Notice: Undefined array key " 7.5str" in %s on line 16
NULL

Notice: Undefined array key " 7 str" in %s on line 17
NULL

Notice: Undefined array key " 7.5 str" in %s on line 18
NULL

Notice: Undefined array key "7 str" in %s on line 19
NULL

Notice: Undefined array key "7.5 str" in %s on line 20
NULL

Notice: Undefined array key "0xA" in %s on line 21
NULL

Notice: Undefined array key "0b10" in %s on line 22
NULL

Notice: Undefined array key "07" in %s on line 23
NULL
Done