Git Product home page Git Product logo

Comments (6)

bwoebi avatar bwoebi commented on August 17, 2024

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.

damianwadley avatar damianwadley commented on August 17, 2024

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.

bwoebi avatar bwoebi commented on August 17, 2024

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.

iluuu1994 avatar iluuu1994 commented on August 17, 2024

@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.

sheinrich177 avatar sheinrich177 commented on August 17, 2024

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.

ChrisHSandN avatar ChrisHSandN commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.