Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-32431: Ensure two bytes objects of zero length compare equal #5021

Closed
wants to merge 2 commits into from

Conversation

jonathanunderwood
Copy link

@jonathanunderwood jonathanunderwood commented Dec 27, 2017

With the current logic in Objects/bytesobject.c in the function bytes_compare_eq can be the case that zero length bytes object object created in an extension module like this:

val = PyBytes_FromStringAndSize (NULL, 20);
Py_SIZE(val) = 0;

won't compare equal to b'' because the memory is not initialized, so the first two bytes won't be equal. Nonetheless, the Python interpreter does return b'' for print(repr(val)), so this behaviour is very confusing. To get the correct behaviour, one would have to initialize the memory:

val = PyBytes_FromStringAndSize (NULL, 20);
c = PyBytes_AS_STRING (val);
c[0] = '\0';
Py_SIZE(val) = 0;

This PR ensures that two zero length byte objects always compare equal irrespecitve of whether the memory has been initialized for either object.

https://bugs.python.org/issue32431

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

@jonathanunderwood jonathanunderwood changed the title Ensure two bytes objects of zero length compare equal Ensure two bytes objects of zero length compare equal (Issue 32431) Dec 27, 2017
@jonathanunderwood jonathanunderwood changed the title Ensure two bytes objects of zero length compare equal (Issue 32431) bpo-32431: Ensure two bytes objects of zero length compare equal Dec 27, 2017
@jonathanunderwood
Copy link
Author

Have now signed the CLA, in case it matters.

@jonathanunderwood
Copy link
Author

jonathanunderwood commented Sep 22, 2018

I don't think this fix merits a NEWS item, does it? If not, could you add a "skip news" label.

return 0;

if (lena == 0 && lenb == 0)
return 1;

if (a->ob_sval[0] != b->ob_sval[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just drop this (a->ob_sval[0] != b->ob_sval[0]) check? That would be simpler and also solve your problem.

@brettcannon brettcannon added the type-feature A feature request or enhancement label Apr 2, 2019
@brettcannon
Copy link
Member

For any other core devs, there is some debate on the issue tracker about changing these semantics.

@methane methane closed this Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants