@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix exception handling a numeric git branch ("Call to phutil_nonempty_string() expected null or a string, got: int" in PhabricatorRepository)

Summary:
PHP casts an array key which is a string that contains a decimal integer automatically into int type.
Quoting https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax: "Strings containing valid decimal ints, unless the number is preceded by a + sign, will be cast to the int type."

As we check git branch names via `phutil_nonempty_string($key)`, this throws an exception.
Thus explicitly cast the int to string.

Closes T15640.

Test Plan:
* Have a git repository with a branch whose name consists of digits only and does not start with 0.
* Go to a commit which belongs both to the main/master branch and such a numeric branch via http://phorge.localhost/diffusion/ABCD/branches/main/;1234567890abcdef1234567890abcdef12345678
* Additionally, visit the commit and see that the "Branches" does not load as it also belongs to a numeric branch.

Reviewers: O1 Blessed Committers, valerio.bozzolan

Reviewed By: O1 Blessed Committers, valerio.bozzolan

Subscribers: tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Maniphest Tasks: T15640

Differential Revision: https://we.phorge.it/D25945

+30 -3
+1 -1
src/applications/diffusion/conduit/DiffusionBranchQueryConduitAPIMethod.php
··· 56 56 $refs = array(); 57 57 foreach ($ref_map as $ref => $commit) { 58 58 $refs[] = id(new DiffusionRepositoryRef()) 59 - ->setShortName($ref) 59 + ->setShortName((string)$ref) 60 60 ->setCommitIdentifier($commit); 61 61 } 62 62 } else {
+7 -2
src/applications/diffusion/data/DiffusionGitBranch.php
··· 80 80 } 81 81 82 82 /** 83 - * As above, but with no `-r`. Used for bare repositories. 83 + * Parse the output of 'git branch --verbose --no-abbrev' or similar into a 84 + * map. As parseRemoteBranchOutput but no `-r`. Used for bare repositories. 85 + * 86 + * @return map Map of branch name (string or int) and its hash (string). 84 87 */ 85 88 public static function parseLocalBranchOutput($stdout) { 86 89 $map = array(); ··· 101 104 if ($branch == '(no branch)') { 102 105 continue; 103 106 } 104 - 107 + // Note: If the $branch name string is numeric containing only decimal 108 + // ints and does not start with 0, PHP will cast it from string to int: 109 + // https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax 105 110 $map[$branch] = $branch_head; 106 111 } 107 112
+22
src/docs/flavor/php_pitfalls.diviner
··· 374 374 // ... 375 375 } 376 376 ``` 377 + 378 + = PHP casts all-digit array keys from string to int = 379 + 380 + An array key which is a string that contains a decimal int will be cast to the 381 + int type: 382 + 383 + ```lang=php 384 + $key0 = "main"; 385 + $key1 = "123"; 386 + $key2 = "0123"; 387 + $array = array($key0 => "foo", $key1 => "foo", $key2 => "foo"); 388 + foreach ($array as $key => $value) { 389 + print(gettype($key)."\n"); 390 + } 391 + ``` 392 + prints `string`, `integer`, `string`. 393 + 394 + Thus running `phutil_nonempty_string($key)` complains that it expected null or 395 + a string but got int. 396 + 397 + Avoid this by either explicitly casting via `(string)$key`, or by using 398 + `phutil_nonempty_scalar($key)` instead of `phutil_nonempty_string($key)`.