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

Add range check for %c in PyUnicode_FromFormat #62384

Closed
serhiy-storchaka opened this issue Jun 10, 2013 · 5 comments
Closed

Add range check for %c in PyUnicode_FromFormat #62384

serhiy-storchaka opened this issue Jun 10, 2013 · 5 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 18184
Nosy @vstinner, @ezio-melotti, @serhiy-storchaka
Files
  • format_c.diff: Patch for 3.3+
  • format_c-2.7.diff: Patch for 2.7
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2013-06-23.18:35:55.945>
    created_at = <Date 2013-06-10.21:00:13.157>
    labels = ['interpreter-core', 'type-bug', 'expert-unicode']
    title = 'Add range check  for %c in PyUnicode_FromFormat'
    updated_at = <Date 2013-06-23.18:35:55.944>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2013-06-23.18:35:55.944>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2013-06-23.18:35:55.945>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core', 'Unicode']
    creation = <Date 2013-06-10.21:00:13.157>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['30535', '30536']
    hgrepos = []
    issue_num = 18184
    keywords = ['patch']
    message_count = 5.0
    messages = ['190935', '191437', '191710', '191713', '191718']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'ezio.melotti', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue18184'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @serhiy-storchaka
    Copy link
    Member Author

    Currently PyUnicode_FromFormat doesn't check an argument for %c and can raise SystemError only due maxchar check in PyUnicode_New. On 2.7 an error doesn't raised, but %c argument can be silently wrapped (on narrow build) or illegal Unicode string can be created (on wide build). The proposed patch adds explicit range check for %c argument.

    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error labels Jun 10, 2013
    @vstinner
    Copy link
    Member

    Both patches look good to me. Do you feel motivated to check if all formating methods have a test? Here is a list of format methods:

    PyUnicode_Format():

    • 3.3, 3.4: formatchar() raises OverflowError if x > MAX_UNICODE
    • 2.7: formatchar() raises OverflowError if x > 0x10ffff (or x > 0xffff, in narrow mode)

    PyUnicode_FromFromatV():

    • 2.7: no check, *s++ = va_arg(vargs, int); => BUG
    • 3.3: indirect check, maxchar = Py_MAX(maxchar, ordinal); and then PyUnicode_New(n, maxchar); should fail => (ok)
    • 3.4: raise ValueError if ordinal > MAX_UNICODE => OK

    int.__format__('c'):

    • 3.3, 3.4: format_long_internal() raises OverflowError if x > 0x10ffff => OK
    • 2.7: format_int_or_long_internal() raises OverflowError if x > 0x10ffff (or x > 0xffff in narrow mode) => OK

    IMO a ValueError would be better than OverflowError, it's not really an overflow (limitation of the C language, or a C type). It is maybe too late to change this.

    @serhiy-storchaka
    Copy link
    Member Author

    No, I don't feel motivated right now (and this is a case when even never is better than right now).

    I checked other format methods and found a bug only in PyUnicode_FromFormatV(). I had added test for 3.3+ because 3.3+ already have a test for PyUnicode_FromFormatV(). I hadn't add test for 2.7 because there is no test for PyUnicode_FromFormatV() on 2.7.

    @serhiy-storchaka serhiy-storchaka self-assigned this Jun 23, 2013
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 23, 2013

    New changeset f8ede55cf92b by Serhiy Storchaka in branch '3.3':
    Issue bpo-18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
    http://hg.python.org/cpython/rev/f8ede55cf92b

    New changeset 42def600210e by Serhiy Storchaka in branch 'default':
    Issue bpo-18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
    http://hg.python.org/cpython/rev/42def600210e

    New changeset 2f1e8b7fa534 by Serhiy Storchaka in branch '2.7':
    Issue bpo-18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
    http://hg.python.org/cpython/rev/2f1e8b7fa534

    @serhiy-storchaka
    Copy link
    Member Author

    I agree, a ValueError may be better than OverflowError, but all other formattings raise OverflowError, and we should support them consistent.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants