GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test_unicode test_raiseMemError miscalculates struct size when string has UTF-8 representation. · Issue #93575 · python/cpython

test_unicode test_raiseMemError miscalculates struct size when string has UTF-8 representation. #93575

New issue
Copy link
New issue
Copy link
Closed
Closed
test_unicode test_raiseMemError miscalculates struct size when string has UTF-8 representation.#93575
Copy link
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixestestsTests in the Lib/test dirTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@tiran
tiran
opened on Jun 7, 2022
Issue body actions

The test case test_raiseMemError assumes that all structs have a NULL byte of length 1. However compact_struct_size allocate 2 or 4 bytes space for NULL bytes: (PyUnicode_GET_LENGTH(self) + 1) * PyUnicode_KIND(self). Note that it's (len(s) + 1) * char_size, not len(s) * char_size + 1. The bug introduces an off-by-one / off-by-three error that sometimes leads to failing test on WASI, because code does not raise a memory error.

Activity

  1. tiran
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    testsTests in the Lib/test dirTests in the Lib/test dir
    3.11only security fixesonly security fixes
    3.12only security fixesonly security fixes
    on Jun 7, 2022
  2. tiran
    added a commit that references this issue on Jun 7, 2022

    pythongh-93575: Correctly calculate NULL bytes in test_raiseMemError

    8e89228
  3. serhiy-storchaka commented on Jun 7, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Jun 7, 2022
    Member
    More actions

    No, it is not correct. null_byte is the size of the null character in string "a". Does WASI use 2 or 4 bytes for character in the ASCII strings?

  4. tiran commented on Jun 8, 2022

    @tiran
    tiran
    on Jun 8, 2022
    MemberAuthor
    More actions

    It's a different issue. sys.getsizeof() returns different values when one of the strings happened to have a utf-8 representation.

    Full test run:

    0:15:27 Re-running test_unicode in verbose mode (matching: test_raiseMemError)
    test_raiseMemError (test.test_unicode.UnicodeTest.test_raiseMemError) ... 
      test_raiseMemError (test.test_unicode.UnicodeTest.test_raiseMemError) (char='€', maxlen=1073741808, struct_size=31, char_size=2) ... FAIL
    
    ======================================================================
    FAIL: test_raiseMemError (test.test_unicode.UnicodeTest.test_raiseMemError) (char='€', maxlen=1073741808, struct_size=31, char_size=2)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Lib/test/test_unicode.py", line 2399, in test_raiseMemError
        self.assertRaises(MemoryError, alloc)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: MemoryError not raised by <lambda>
    

    Test run of just test_unicode

    ERROR: test_raiseMemError (test.test_unicode.UnicodeTest.test_raiseMemError) (char='€', maxlen=1073741809, struct_size=28, char_size=2)
    

    struct_size=31 != struct_size=28. The extra 3 bytes are char_size + null_byte. I'm going to change the test case.

  5. tiran
    changed the title [-]test_unicode test_raiseMemError miscalculates length of NULL byte[/-] [+]test_unicode test_raiseMemError miscalculates struct size when string has UTF-8 representation.[/+] on Jun 8, 2022
  6. serhiy-storchaka commented on Jun 8, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Jun 8, 2022
    Member
    More actions

    Ah, that's the thing! Nice catch.

    I was going to use

    struct_size = sys.getsizeof(char * n) - char_size * (n+1)

    where n is an arbitrary number.

    In case we add new fields the test will still pass successfully with your approach, but will no longer test the exact lower limit. Could you at least add a self-check for sys.getsizeof(char * n) == struct_size + char_size * (n+1)?

  7. tiran commented on Jun 8, 2022

    @tiran
    tiran
    on Jun 8, 2022
    MemberAuthor
    More actions

    The assertion fails for char='é': 71 != 63 (on 32 bit), 99 != 83 (on 64 bit)

  8. tiran
    added a commit that references this issue on Jun 8, 2022

    pythongh-93575: Use correct way to calculate PyUnicode struct sizes

    18ecceb
  9. serhiy-storchaka commented on Jun 8, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Jun 8, 2022
    Member
    More actions

    Because there is yet one bug here! The correct code for struct_size:

    struct_size = ascii_struct_size if code < 0x80 else compact_struct_size
  10. tiran commented on Jun 8, 2022

    @tiran
    tiran
    on Jun 8, 2022
    MemberAuthor
    More actions

    Yeah, the test was mishandling code between 128 and 256.

  11. tiran
    added 3 commits that reference this issue on Jun 8, 2022

    gh-93575: Use correct way to calculate PyUnicode struct sizes (GH-93602)

    5442561

    [3.11] pythongh-93575: Use correct way to calculate PyUnicode struct …

    93c34da

    [3.11] gh-93575: Use correct way to calculate PyUnicode struct sizes (G…

    47a7855
  12. tiran
    closed this as completedon Jun 9, 2022
  13. serhiy-storchaka
    added a commit that references this issue on Jun 26, 2022

    Revert 30935 defer bpo45162 to 312 (#94285)

    1ffde3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixesonly security fixes3.12only security fixesonly security fixestestsTests in the Lib/test dirTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.