1--TEST--
2Test zend_ini_parse_quantity() overflow handling
3--EXTENSIONS--
4zend_test
5--FILE--
6<?php
7
8function increment(string $s): string {
9    if (strlen($s) === 0) {
10        return '1';
11    }
12
13    $digit = intval($s[strlen($s)-1]);
14    $prefix = substr($s, 0, -1);
15
16    if ($digit === 9) {
17        return increment($prefix) . '0';
18    }
19
20    return $prefix . strval($digit+1);
21}
22
23function decrement(string $s): string {
24    assert($s[0] === '-');
25    return '-' . increment(substr($s, 1));
26}
27
28$tests = [
29    'No overflow 001' => '0',
30    'No overflow 002' => '1',
31    'No overflow 003' => '100',
32    'No overflow 004' => strval(PHP_INT_MAX),
33    'No overflow 005' => strval(PHP_INT_MIN),
34    'No overflow 006' => '2K',
35    'No overflow 007' => '-2K',
36    'Subject overflow 001' => increment(strval(PHP_INT_MAX)),
37    'Subject overflow 002' => decrement(strval(PHP_INT_MIN)),
38    'Multiplier overflow 001' => strval(PHP_INT_MAX).'K',
39    'Multiplier overflow 002' => strval(PHP_INT_MIN).'K',
40];
41
42foreach ($tests as $name => $value) {
43    printf("# %s: \"%s\"\n", $name, $value);
44    printf("%d\n", zend_test_zend_ini_parse_quantity($value));
45    print "\n";
46    print "----------\n";
47}
48
49--EXPECTF--
50# No overflow 001: "0"
510
52
53----------
54# No overflow 002: "1"
551
56
57----------
58# No overflow 003: "100"
59100
60
61----------
62# No overflow 004: "%d"
63%d
64
65----------
66# No overflow 005: "-%d"
67-%d
68
69----------
70# No overflow 006: "2K"
712048
72
73----------
74# No overflow 007: "-2K"
75-2048
76
77----------
78# Subject overflow 001: "%d"
79
80Warning: Invalid quantity "%d": value is out of range, using overflow result for backwards compatibility in %s on line %d
81%s
82
83----------
84# Subject overflow 002: "-%d"
85
86Warning: Invalid quantity "-%d": value is out of range, using overflow result for backwards compatibility in %s on line %d
87%s
88
89----------
90# Multiplier overflow 001: "%dK"
91
92Warning: Invalid quantity "%dK": value is out of range, using overflow result for backwards compatibility in %s on line %d
93%s
94
95----------
96# Multiplier overflow 002: "-%dK"
97
98Warning: Invalid quantity "-%dK": value is out of range, using overflow result for backwards compatibility in %s on line %d
99%s
100
101----------
102