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-39573: Use Py_SIZE() in s_set() of cfield.c #18419

Closed
wants to merge 1 commit into from

Conversation

shihai1991
Copy link
Member

@shihai1991 shihai1991 commented Feb 9, 2020

@shihai1991 shihai1991 changed the title Use Py_SIZE() in s_set() of cfield.c bpo-39573: Use Py_SIZE() in s_set() of cfield.c Feb 9, 2020
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

This change is unrelated to https://bugs.python.org/issue39573 If you consider that s_set() behavior is a bug, please open a separated issue.

Your change is backward incompatible. Example:

import ctypes

class MyStruct(ctypes.Structure):
    _fields_ = (
        ('char', ctypes.c_char * 5),
    )

s = MyStruct()
print(ascii(s.char), bytes(s))
s.char = b'a\0b\0'
print(ascii(s.char), bytes(s))

Current behavior:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00\x00\x00\x00'

=> only b'a\0' (2 bytes) are copied

With your PR:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00b\x00\x00'

=> only b'a\0b\0' (4 bytes) are copied

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@vstinner
Copy link
Member

vstinner commented Feb 9, 2020

cc @corona10

@shihai1991
Copy link
Member Author

shihai1991 commented Feb 9, 2020

This change is unrelated to https://bugs.python.org/issue39573 If you consider that s_set() behavior is a bug, please open a separated issue.

Your change is backward incompatible. Example:

import ctypes

class MyStruct(ctypes.Structure):
    _fields_ = (
        ('char', ctypes.c_char * 5),
    )

s = MyStruct()
print(ascii(s.char), bytes(s))
s.char = b'a\0b\0'
print(ascii(s.char), bytes(s))

Current behavior:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00\x00\x00\x00'

=> only b'a\0' (2 bytes) are copied

With your PR:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00b\x00\x00'

=> only b'a\0b\0' (4 bytes) are copied

Thanks for your quick feedback, victor.

This change is unrelated to https://bugs.python.org/issue39573

I found you do some macro porting operations, so I thought this macro porting operation relate to 39573 is fine(not the cause of a bug).

Your change is backward incompatible.

Great example, current tests not help me found this backward incompatible and your example have answer this question(/* XXX Why not Py_SIZE(value)? */).

I have debug it in my vm, the reason is:
value = b'a\0b\0'
data = PyBytes_AS_STRING(value) ==> "a"
strlen(data) ==> 1
Py_SIZE(value) ==> 4

image

Sorry for this noisy PR, guys!

@shihai1991 shihai1991 closed this Feb 9, 2020
@shihai1991
Copy link
Member Author

To prevent other developers break this backward incompatible again, i add victor's example in #18424

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants