Skip to content

Commit 8348ca7

Browse files
authored
str2int (#405)
1 parent 742265e commit 8348ca7

File tree

8 files changed

+34
-34
lines changed

8 files changed

+34
-34
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ repos:
103103
entry: bash ./test/assert-utils-used.bash
104104
language: system
105105
files: \.php$
106-
exclude: ^resources/lib/utils\.php$
106+
exclude: |
107+
(?x)^(
108+
^resources/lib/utils\.php$|
109+
^test/.*\.php$|
110+
)$
107111
- id: assert-exceptions-used
108112
name: Assert exceptions are used
109113
entry: bash ./test/assert-exceptions-used.bash

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
This will throw an exception rather than returning `false`.
2121
- No code should call `mb_detect_encoding()`, instead `\mbDetectEncoding()`.
2222
This will enable strict mode and throw an exception rather than returning `false`.
23+
- No code should call `intval()`, instead `\str2int()`.
24+
This will enable strict mode and throw an exception rather than issuing a warning.
2325
- `UnityHTTPD`'s user-facing error functionality (ex: `badRequest`) should only be called from `webroot/**/*.php`.
2426
`resources/**/*.php` should throw exceptions instead.
2527
- all pages under `webroot/admin/` must check for `$USER->isAdmin()` and call `UnityHTTPD::forbidden()` if not admin.

resources/lib/UnityLDAP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private function getCustomIDMappings(): array
160160
}
161161
$output_map = [];
162162
foreach ($output as [$uid, $uidNumber_str]) {
163-
$output_map[$uid] = intval($uidNumber_str);
163+
$output_map[$uid] = str2int($uidNumber_str);
164164
}
165165
return $output_map;
166166
}

resources/lib/utils.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use UnityWebPortal\lib\exceptions\ArrayKeyException;
43
use UnityWebPortal\lib\exceptions\EnsureException;
54
use UnityWebPortal\lib\exceptions\EncodingUnknownException;
65
use UnityWebPortal\lib\exceptions\EncodingConversionException;
@@ -83,3 +82,16 @@ function getHyperlink($text, ...$url_components)
8382
$url = getURL(...$url_components);
8483
return "<a href='$url'>$text</a>";
8584
}
85+
86+
/**
87+
* extra args (ex: base) are passed along to intval()
88+
* @throws ValueError
89+
*/
90+
function str2int(string $x, ...$args): int
91+
{
92+
if (ctype_digit($x)) {
93+
return intval($x, ...$args);
94+
} else {
95+
throw new ValueError("not digits: $x");
96+
}
97+
}

test/assert-utils-used.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare -A utils=(
1010
["json_encode"]="jsonEncode"
1111
["mb_detect_encoding"]="mbDetectEncoding"
1212
["mb_convert_encoding"]="mbConvertEncoding"
13+
["intval"]="str2int"
1314
)
1415

1516
rc=0

test/functional/SSHKeyDeleteTest.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,30 @@ private function deleteKey(string $index): void
2424
public static function getGarbageIndexArgs()
2525
{
2626
global $HTTP_HEADER_TEST_INPUTS;
27-
return array_map(function ($x) {
28-
return [$x];
29-
}, $HTTP_HEADER_TEST_INPUTS);
27+
$http_header_test_inputs_no_ints = array_filter(
28+
$HTTP_HEADER_TEST_INPUTS,
29+
fn($x) => !ctype_digit($x),
30+
);
31+
$http_header_test_inputs_no_ints_2d = array_map(
32+
fn($x) => [$x],
33+
$http_header_test_inputs_no_ints,
34+
);
35+
return array_merge([["-1"], ["0.5"]], $http_header_test_inputs_no_ints_2d);
3036
}
3137

3238
#[DataProvider("getGarbageIndexArgs")]
3339
public function testDeleteKeyGarbageInput(string $index)
3440
{
3541
global $USER;
3642
try {
43+
$this->expectException(ValueError::class);
3744
$this->deleteKey($index);
3845
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys());
3946
} finally {
4047
$USER->setSSHKeys(self::$initialKeys);
4148
}
4249
}
4350

44-
public function testDeleteKeyNegativeIndex()
45-
{
46-
global $USER;
47-
try {
48-
$this->deleteKey("-1");
49-
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys());
50-
} finally {
51-
$USER->setSSHKeys(self::$initialKeys);
52-
}
53-
}
54-
5551
public function testDeleteKeyIndexTooLarge()
5652
{
5753
global $USER;
@@ -63,17 +59,6 @@ public function testDeleteKeyIndexTooLarge()
6359
}
6460
}
6561

66-
public function testDeleteKeyDecimal()
67-
{
68-
global $USER;
69-
try {
70-
$this->deleteKey("0.5");
71-
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys());
72-
} finally {
73-
$USER->setSSHKeys(self::$initialKeys);
74-
}
75-
}
76-
7762
public function testDeleteKey()
7863
{
7964
global $USER;

webroot/api/content/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
require_once __DIR__ . "/../../../resources/autoload.php";
88

9-
$CHAR_WRAP = UnityHTTPD::getQueryParameter("line_wrap", false) ?? 80;
9+
$CHAR_WRAP = str2int(UnityHTTPD::getQueryParameter("line_wrap", false) ?? "80");
1010
$content_name = UnityHTTPD::getQueryParameter("content_name");
1111
echo $SQL->getPage($content_name)["content"];

webroot/panel/account.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@
5252
break;
5353
case "delKey":
5454
$keys = $USER->getSSHKeys();
55-
$indexStr = $_POST["delIndex"];
56-
if (!preg_match("/^[0-9]+$/", $indexStr)) {
57-
break;
58-
}
59-
$index = intval($indexStr);
55+
$index = str2int(UnityHTTPD::getPostData("delIndex"));
6056
if ($index >= count($keys)) {
6157
break;
6258
}

0 commit comments

Comments
 (0)