1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2025-04-05 00:48:20 +02:00
phorge-arcanist/src/utils/PhutilSortVector.php
Andre Klapper ef73b12b58 Fix "strpos(): Non-string needles will be interpreted as strings" in PhutilSortVector
Summary:
Code checking if the needle string `$value` is somewhere in the haystack `"\0"` makes no sense for a single byte (if it did, then `strcmp` instead of `strpos` should have been used) and the created exception output implies that it's supposed to check that a string does not contain NULL bytes.
Thus switch the order of arguments passed to `strpos()` to be correct.

```
EXCEPTION: (RuntimeException) strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior at [<arcanist>/src/error/PhutilErrorHandler.php:261]
  #0 <#2> PhutilErrorHandler::handleError(integer, string, string, integer, array) called at [<arcanist>/src/error/PhutilErrorHandler.php:261]
  #1 <#2> strpos(string, integer) called at [<arcanist>/src/utils/PhutilSortVector.php:33]
```

Closes T15755

Test Plan: Read the surrounding code carefully.

Reviewers: O1 Blessed Committers, valerio.bozzolan, speck

Reviewed By: O1 Blessed Committers, valerio.bozzolan, speck

Subscribers: tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Maniphest Tasks: T15755

Differential Revision: https://we.phorge.it/D25557
2024-03-22 13:34:55 +01:00

54 lines
1.4 KiB
PHP

<?php
final class PhutilSortVector
extends Phobject {
private $parts = array();
public function addInt($value) {
// We need to produce strings for each integer which sort naturally. This
// requires some careful manipulation.
if ($value === ~PHP_INT_MAX) {
// For the minimum integer value (usually -9223372036854775808 on 64
// bit systems) we just give it a special "A" prefix to make sure it
// sorts first.
$prefix = 'A';
} else if ($value < 0) {
// For all other negative values, we give them a "B" prefix, then
// subtract the value from the maximum integer. This sorts values
// in ascending order when printed.
$prefix = 'B';
$value = PHP_INT_MAX + $value;
} else {
// For zero and positive values, we give them a "C" prefix.
$prefix = 'C';
}
$this->parts[] = sprintf('%s%020u', $prefix, $value);
return $this;
}
public function addString($value) {
if (strlen($value) && (strpos($value, "\0") !== false)) {
throw new Exception(
pht(
'String components of a sort vector must not contain NULL bytes.'));
}
$this->parts[] = $value;
return $this;
}
public function __toString() {
return implode("\0", $this->parts);
}
/**
* This allows you to sort a list of sort vectors using @{function:msortv}.
*/
public function getSelf() {
return $this;
}
}