Title: _ctypes: from_address(0) triggers fatal slice::from_raw_parts panic and process abort · Issue #9975 · RustPython/RustPython · GitHub
Open Graph Title: _ctypes: from_address(0) triggers fatal slice::from_raw_parts panic and process abort · Issue #9975 · RustPython/RustPython
X Title: _ctypes: from_address(0) triggers fatal slice::from_raw_parts panic and process abort · Issue #9975 · RustPython/RustPython
Description: Summary Calling .from_address(0) on any ctypes type (such as ctypes.c_int.from_address(0)) immediately triggers a fatal Rust standard library panic and non-unwinding process abort due to an unsafe precondition violation: thread 'main' pa...
Open Graph Description: Summary Calling .from_address(0) on any ctypes type (such as ctypes.c_int.from_address(0)) immediately triggers a fatal Rust standard library panic and non-unwinding process abort due to an unsafe ...
X Description: Summary Calling .from_address(0) on any ctypes type (such as ctypes.c_int.from_address(0)) immediately triggers a fatal Rust standard library panic and non-unwinding process abort due to an unsafe ...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9975
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_ctypes: from_address(0) triggers fatal slice::from_raw_parts panic and process abort","articleBody":"## Summary\n\nCalling `.from_address(0)` on any ctypes type (such as `ctypes.c_int.from_address(0)`) immediately triggers a fatal Rust standard library panic and non-unwinding process abort due to an `unsafe` precondition violation:\n\n```text\nthread 'main' panicked at crates/host_env/src/ctypes.rs:2300:14:\nunsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`\n\nThis indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.\nthread caused non-unwinding panic. aborting.\n```\n\n## Reproduction\n\n```python\nimport ctypes\n\nc = ctypes.c_int.from_address(0)\nprint(ctypes.addressof(c))\n```\n\n## Expected\n\nIn CPython 3.12 / 3.13 / 3.14, `.from_address(0)` succeeds and returns a ctypes instance pointing to address 0:\n```text\n0\n```\n\nIn RustPython, Rust slices (`&[u8]`) cannot wrap a null pointer (`0 as *const u8`). Creating such an instance executes `core::slice::from_raw_parts(ptr, size)` with `ptr.is_null()`, which is undefined behavior and aborts the interpreter. RustPython should guard against NULL addresses (similar to the NULL address checks already implemented in `in_dll`, `PyObj_FromPtr`, and `pointer.contents`) and raise `ValueError: NULL pointer access` (or handle NULL buffer storage cleanly) rather than crashing the process.\n\n## Actual\n\nThe process aborts immediately upon calling `from_address(0)`:\n```text\nthread 'main' panicked at crates\\host_env\\src\\ctypes.rs:2300:14:\nunsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`\n```\n\n## Cause\n\nIn `crates/vm/src/stdlib/_ctypes/base.rs`:\n```rust\n #[pyclassmethod]\n pub(super) fn from_address(\n cls: PyTypeRef,\n address: ArgStrictInt\u003cisize>,\n vm: &VirtualMachine,\n ) -> PyResult {\n let address = address.value;\n let size = {\n let stg_info = cls.stg_info(vm)?;\n stg_info.size\n };\n\n if size == 0 {\n return Err(vm.new_type_error(\"abstract class\"));\n }\n\n // PyCData_AtAddress\n let cdata = unsafe { PyCData::at_address(address as *const u8, size) };\n cdata.into_ref_with_type(vm, cls).map(Into::into)\n }\n```\n\nWhen `address == 0`, `address as *const u8` is passed directly to `PyCData::at_address`:\n```rust\n pub unsafe fn at_address(ptr: *const u8, size: usize) -> Self {\n let slice = unsafe { rustpython_host_env::ctypes::borrow_memory(ptr, size) };\n Self {\n buffer: PyRwLock::new(Cow::Borrowed(slice)),\n ...\n }\n }\n```\nWhich calls `borrow_memory`:\n```rust\npub unsafe fn borrow_memory(ptr: *const u8, size: usize) -> &'static [u8] {\n unsafe { core::slice::from_raw_parts(ptr, size) }\n}\n```\nBecause `ptr` is `0 as *const u8`, `core::slice::from_raw_parts` violates Rust's non-null pointer invariant and aborts. By contrast, `in_dll` in the same file explicitly guards against NULL before calling `PyCData::at_address`:\n```rust\n if ptr.is_null() {\n return Err(vm.new_value_error(format!(\"symbol '{}' not found\", name.as_wtf8())));\n }\n```\n","author":{"url":"https://github.com/alok-108","@type":"Person","name":"alok-108"},"datePublished":"2026-11-20T21:59:25Z","url":"https://github.com/RustPython/RustPython/issues/9975"}
| 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 | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ef0e96f2-3a94-4369-b19a-a4a94caf72f2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC30:2E3FEE:4DF44BB:625FC03:6B025E66 |
| html-safe-nonce | d106d12390117f1fce6ec8d4cbc3bbc1e2f79d8f5e695490db11fe63acb568ef |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzMwOjJFM0ZFRTo0REY0NEJCOjYyNUZDMDM6NkIwMjVFNjYiLCJ2aXNpdG9yX2lkIjoiNDAzMzI2NDc4NzE0NzM0OTYwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4901a3771228b005f6742bee16e809487f6fe38278df437eedf2f1e1e0722d00 |
| hovercard-subject-tag | issue:6520375039 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/RustPython/RustPython/9975/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a47582e9370da698f21a3d2c00a9156eb658df6afa6643b7a40ba5d81eb381c6/RustPython/RustPython/issues/9975 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a47582e9370da698f21a3d2c00a9156eb658df6afa6643b7a40ba5d81eb381c6/RustPython/RustPython/issues/9975 |
| og:image:alt | Summary Calling .from_address(0) on any ctypes type (such as ctypes.c_int.from_address(0)) immediately triggers a fatal Rust standard library panic and non-unwinding process abort due to an unsafe ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alok-108 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width