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

ctypes string pointer fields should accept embedded null characters #76926

Closed
theller opened this issue Feb 1, 2018 · 7 comments
Closed

ctypes string pointer fields should accept embedded null characters #76926

theller opened this issue Feb 1, 2018 · 7 comments
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@theller
Copy link

theller commented Feb 1, 2018

BPO 32745
Nosy @theller, @amauryfa, @abalkin, @ned-deily, @meadori, @ambv, @serhiy-storchaka, @eryksun, @ZackerySpytz, @miss-islington
PRs
  • bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type #8721
  • [3.8] bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (GH-8721) #25811
  • [3.9] bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (GH-8721) #25812
  • Files
  • nullchars.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-05-02.11:03:54.108>
    created_at = <Date 2018-02-01.19:54:50.057>
    labels = ['ctypes', 'type-bug', '3.8', '3.9', '3.10']
    title = 'ctypes string pointer fields should accept embedded null characters'
    updated_at = <Date 2021-05-02.11:03:54.107>
    user = 'https://github.com/theller'

    bugs.python.org fields:

    activity = <Date 2021-05-02.11:03:54.107>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-05-02.11:03:54.108>
    closer = 'lukasz.langa'
    components = ['ctypes']
    creation = <Date 2018-02-01.19:54:50.057>
    creator = 'theller'
    dependencies = []
    files = ['47420']
    hgrepos = []
    issue_num = 32745
    keywords = ['patch', '3.6regression']
    message_count = 7.0
    messages = ['311462', '311468', '314567', '314579', '392685', '392688', '392689']
    nosy_count = 10.0
    nosy_names = ['theller', 'amaury.forgeotdarc', 'belopolsky', 'ned.deily', 'meador.inge', 'lukasz.langa', 'serhiy.storchaka', 'eryksun', 'ZackerySpytz', 'miss-islington']
    pr_nums = ['8721', '25811', '25812']
    priority = 'critical'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32745'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @theller
    Copy link
    Author

    theller commented Feb 1, 2018

    ctypes Structure fields of type c_char_p or c_wchar_p used to accept strings with embedded null characters. I noticed that Python 3.6.4 does refuse them. It seems this has been changed in recent version(s).

    There ARE use-cases for this: The Windows-API OPENFILENAME structure is one example. The Microsoft docs for the lpstrFilter field:

    """
    lpstrFilter

    Type: LPCTSTR
    
    A buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters.
    

    """

    I have attached a simple script which demonstrates this new behaviour; the output with Python 3.6.4 is this:

    Traceback (most recent call last):
      File "nullchars.py", line 8, in <module>
        t.unicode = u"foo\0bar"
    ValueError: embedded null character

    @eryksun
    Copy link
    Contributor

    eryksun commented Feb 1, 2018

    PyUnicode_AsWideCharString was updated to raise ValueError for embedded nulls if the size output parameter is NULL. Z_set in cfield.c should be updated to get the size, which can be ignored here. For example:

    Py_ssize_t size; 
    buffer = PyUnicode_AsWideCharString(value, &size);
    

    @eryksun eryksun added 3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Feb 1, 2018
    @ned-deily
    Copy link
    Member

    The change mentioned was made in #46714 for bpo-13617 and was released in 3.6.3 (and 3.5.4 now in security-fix-only mode).

    @serhiy-storchaka
    Copy link
    Member

    This is a regression. Eryk's solution LGTM. Do you mind to create a PR?

    But u"foo\0bar" is not terminated by two NULL characters. If this is used in real code, it contains a bug. And the getter of this field will return the string only to the first null character. More work is needed for making this more reliable.

    @eryksun eryksun added 3.9 only security fixes 3.10 only security fixes and removed 3.7 (EOL) end of life labels Mar 19, 2021
    @ambv
    Copy link
    Contributor

    ambv commented May 2, 2021

    New changeset 73766b0 by Zackery Spytz in branch 'master':
    bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (bpo-8721)
    73766b0

    @ambv
    Copy link
    Contributor

    ambv commented May 2, 2021

    New changeset cf6a796 by Miss Islington (bot) in branch '3.9':
    bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (GH-8721) (bpo-25812)
    cf6a796

    @ambv
    Copy link
    Contributor

    ambv commented May 2, 2021

    New changeset db3ce79 by Miss Islington (bot) in branch '3.8':
    bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (GH-8721) (bpo-25811)
    db3ce79

    @ambv ambv closed this as completed May 2, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes topic-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants