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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions Objects/bytesobject.c
Expand Up @@ -1547,16 +1547,21 @@ static int
bytes_compare_eq(PyBytesObject *a, PyBytesObject *b)
{
int cmp;
Py_ssize_t len;
Py_ssize_t lena, lenb;

len = Py_SIZE(a);
if (Py_SIZE(b) != len)
lena = Py_SIZE(a);
lenb = Py_SIZE(b);

if (lena != lenb)
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.

return 0;

cmp = memcmp(a->ob_sval, b->ob_sval, len);
cmp = memcmp(a->ob_sval, b->ob_sval, lena);
return (cmp == 0);
}

Expand Down