Comments (6)
I'm confused about the documentation note.
I found 0bc9241334eaebd737eb0455e9739257e74d0713, which introduced the wrong note...
What changed is the return value when lengths differ.
In that case there was a bugfix in php/php-src#8220 so that it's now guaranteed to be smaller than 0 (-1), 0 or greater than 0 (1) if the length difference of two strings is greater than 2 GB.
The behaviour of strcmp & co is unchanged for strings where lengths match.
General question: Do we fix wrong notes in UPGRADING after the release?
from doc-en.
What changed is the return value when lengths differ.
Except that's not quite true either. https://3v4l.org/GUuA5
strcmp uses zend_binary_strcmp, whose relevant bit is
retval = memcmp(s1, s2, MIN(len1, len2));
if (!retval) {
return ZEND_THREEWAY_COMPARE(len1, len2);
} else {
return retval;
}
It will memcmp
using the smaller length of the two strings (comparing a "prefix", so to speak) and if they're different then it returns whatever that value is - which is only guaranteed to be negative/positive [1]. If the two prefixes are the same, only then will it go to ZEND_THREEWAY_COMPARE
, and that's the part which explicitly returns -1/0/+1 based on which string is shorter.
https://3v4l.org/mT8di
So the implementation of the "always returns -1/0/+1" feature missed out on normalizing memcmp's return value.
[1] On Windows, I always get -1/+1. And 3v4l's live preview (which is WASM and currently only on PHP 8.1) also returns -1/+1. And it sounds like valgrind may also substitute its own memcmp that returns only -1/+1.
from doc-en.
Yeah, I wasn't 100% precise in specifying the exact circumstances. What I really wanted to say is that there was no intent in normalizing that value, just that the change fixes the integer overflow for size differences >2GB.
from doc-en.
@bwoebi I think it makes sense to fix the migration page in the docs.
https://www.php.net/manual/en/migration82.other-changes.php#migration82.other-changes.functions.core
But more importantly, the docs themselves are also wrong.
https://www.php.net/manual/en/function.strcmp.php
Returns -1 if string1 is less than string2; 1 if string1 is greater than string2, and 0 if they are equal.
I'm moving this to the docs repo.
from doc-en.
I checked again by using the official docker image from https://hub.docker.com/_/php on my Linux machine. I used the changed test script from https://3v4l.org/GUuA5 and only added the output of phpversion().
I get the same result as on my machine:
PHP: 8.2.16
int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)
int(-4)
int(4)
The page 3v4l.org may has some issues by itself, maybe...
However, I also tested it on a different machine with Window 11 installed and with the packages:
- php-8.2.16-Win32-vs16-x64.zip
- php-8.2.16-nts-Win32-vs16-x64.zip
For both, comparing to my machine/Linux, I get a different, but regarding the return values -1, 0, 1 also "wrong" result:
int(-1)
int(1)
int(-4)
int(4)
int(-1)
int(1)
int(-4)
int(4)
So, I would assume that it isn't only a "wrong documentation" topic..
from doc-en.
While there looks to be some dispute over whether this is a docs of core bug above; surely it can be removed from the docs for now?
This is quite a serious inaccuracy as the docs led me to believe code should target ===
-1|0|1
.
from doc-en.
Related Issues (20)
- RFC: Disjunctive Normal Form Types - invalid intersection example HOT 2
- OpenSSL error cURL and MSSQL HOT 1
- PHP_OUTPUT_HANDLER_PROCESSED is only available as of PHP 8.4 HOT 4
- ReflectionProperty::getDefaultValue() and promoted properties HOT 1
- ob_start PHP_OUTPUT_HANDLER_FLUSHABLE wrong ob_end_flush instead of ob_flush HOT 2
- Support more than parameter attributes attributes HOT 7
- Make it clearer that Exceptions cannot be cloned HOT 6
- set_error_handler and set_exception_handler do not necessarily return a callable
- Empty `id` attribute in the `Examples` section HOT 5
- Dark mode or theme HOT 1
- Wrong return type for settype
- wordwrap() should mention that it only supports ASCII characters HOT 2
- in_array HOT 3
- curl_setopt page, the CURLOPT_XFERINFOFUNCTION constant
- docs fail to load HOT 1
- PHP 8.2.18 HOT 2
- DateTimeImmutable::createFromFormat doesn't mention "p"
- Why is documentation so 'lacking' for PHP source code? HOT 2
- Error in Changelog for the function "hash" (php.net) HOT 1
- `PREG_UNMATCHED_AS_NULL` description
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from doc-en.