Title: Report a bad `struct` format before converting the other arguments by jseop-lim · Pull Request #9928 · RustPython/RustPython · GitHub
Open Graph Title: Report a bad `struct` format before converting the other arguments by jseop-lim · Pull Request #9928 · RustPython/RustPython
X Title: Report a bad `struct` format before converting the other arguments by jseop-lim · Pull Request #9928 · RustPython/RustPython
Description: Part of #9913 (item 5) This PR follows our AI policy Summary The module-level struct functions, such as pack_into, bind every argument first and parse the format in the function body, so a buffer or offset that fails to convert hides the struct.error for a bad format. CPython's argument clinic runs cache_struct_converter on the format before it converts the arguments after it. AS-IS >>> import struct >>> struct.pack_into("Z", bytearray(4), 10**100) OverflowError: Python int too large to convert to Rust isize >>> struct.unpack_from("Z", b"", 10**100) OverflowError: Python int too large to convert to Rust isize >>> struct.unpack("Z", 5) TypeError: a bytes-like object is required, not 'int' TO-BE >>> struct.pack_into("Z", bytearray(4), 10**100) struct.error: bad char in struct format >>> struct.unpack_from("Z", b"", 10**100) struct.error: bad char in struct format >>> struct.unpack("Z", 5) struct.error: bad char in struct format (matching CPython) Changes In crates/stdlib/src/pystruct.rs, add CachedFormatSpec, which parses the format through the cache while the argument is bound, and bind the format of pack, pack_into, unpack, unpack_from, iter_unpack and calcsize as it. Extend extra_tests/snippets/stdlib_struct.py with a bad format next to an offset that does not fit and next to a buffer that is not bytes-like. Test plan Built and run on macOS aarch64 against upstream/main 7abccf0, using the CI feature set --no-default-features --features stdlib,importlib,stdio,encodings,sqlite,ssl-rustls-aws-lc,host_env,doc,ucd_3_2_0, plus threading, compared with CPython 3.15.2. The extended extra_tests/snippets/stdlib_struct.py fails on an unpatched build of that commit (at struct.pack_into("Z", bytearray(4), 10**100)) and passes with the patch and under CPython 3.15.2. A probe of 14 calls over pack_into, unpack_from, unpack and iter_unpack, mixing a bad format, a non-buffer and an offset that does not fit, gives the same exception class as CPython 3.15.2 for all 14. Eight of them differ on the unpatched build. rustpython -m test test_struct test_memoryview test_array test_buffer: 1,415 run, 161 skipped, all pass, as on the unpatched build. cargo fmt --check clean, cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targets reports nothing in the changed files (one warning in _ctypes/base.rs that the unpatched tree also has), and the pre-commit hooks other than cspell and prettier pass on both changed files. I used Claude Code (claude-fable-5-2) to draft the analysis, the patch and this description; Claude Code ran the commands above; I reviewed the diff and their output. Summary by CodeRabbit Bug Fixes Invalid struct formats are checked across packing and unpacking operations, including cases with extreme offsets or incompatible input. These cases raise struct.error.
Open Graph Description: Part of #9913 (item 5) This PR follows our AI policy Summary The module-level struct functions, such as pack_into, bind every argument first and parse the format in the function body, so a buffer...
X Description: Part of #9913 (item 5) This PR follows our AI policy Summary The module-level struct functions, such as pack_into, bind every argument first and parse the format in the function body, so a buffer...
Opengraph URL: https://github.com/RustPython/RustPython/pull/9928
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:27adbb1e-9860-c728-383c-4d915c86d2aa |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B816:FC5BE:2E8D76C:3DDF094:6B01D7EE |
| html-safe-nonce | f59572c01b6b59ca3c603d53f98d3c25dd897467c76b73098cc7df94600ce854 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODE2OkZDNUJFOjJFOEQ3NkM6M0RERjA5NDo2QjAxRDdFRSIsInZpc2l0b3JfaWQiOiI1NjMwODkxMzI0NDU1NjM5MDIyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 95edb9277a8f495829739454667a90e6d572cd49c46f6518e15a7f8d9b4d3265 |
| hovercard-subject-tag | pull_request:5361514554 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/RustPython/RustPython/pull/9928/files |
| twitter:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| og:image:alt | Part of #9913 (item 5) This PR follows our AI policy Summary The module-level struct functions, such as pack_into, bind every argument first and parse the format in the function body, so a buffer... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width