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?>
50--EXPECTF--
51# No overflow 001: "0"
520
53
54----------
55# No overflow 002: "1"
561
57
58----------
59# No overflow 003: "100"
60100
61
62----------
63# No overflow 004: "%d"
64%d
65
66----------
67# No overflow 005: "-%d"
68-%d
69
70----------
71# No overflow 006: "2K"
722048
73
74----------
75# No overflow 007: "-2K"
76-2048
77
78----------
79# Subject overflow 001: "%d"
80
81Warning: Invalid quantity "%d": value is out of range, using overflow result for backwards compatibility in %s on line %d
82%s
83
84----------
85# Subject overflow 002: "-%d"
86
87Warning: Invalid quantity "-%d": value is out of range, using overflow result for backwards compatibility in %s on line %d
88%s
89
90----------
91# Multiplier overflow 001: "%dK"
92
93Warning: Invalid quantity "%dK": value is out of range, using overflow result for backwards compatibility in %s on line %d
94%s
95
96----------
97# Multiplier overflow 002: "-%dK"
98
99Warning: Invalid quantity "-%dK": value is out of range, using overflow result for backwards compatibility in %s on line %d
100%s
101
102----------
103