Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PySlice integer overflow on Windows #2840

Closed
breckognize opened this issue Dec 27, 2022 · 2 comments
Closed

PySlice integer overflow on Windows #2840

breckognize opened this issue Dec 27, 2022 · 2 comments
Labels

Comments

@breckognize
Copy link

breckognize commented Dec 27, 2022

Bug Description

In src/types/slice.rs, a PySlice is constructed like this:

impl PySlice {
    /// Constructs a new slice with the given elements.
    pub fn new(py: Python<'_>, start: isizbe, stop: isize, step: isize) -> &PySlice {
        unsafe {
            let ptr = ffi::PySlice_New(
                ffi::PyLong_FromLong(start as c_long),
                ffi::PyLong_FromLong(stop as c_long),
                ffi::PyLong_FromLong(step as c_long),
            );
            py.from_owned_ptr(ptr)
        }
    }
...

The isize arguments are cast to std::os::raw::c_long. The issue is that, surprisingly, on Windows c_long is only 4 bytes. This causes slices to indices greater than i32::MAX to return incorrect results (see repro below).

To be clear, slices above i32::MAX work in vanilla Python on Windows:

import numpy
array = numpy.arange(3_000_000_000)
array[2_999_999_997:9_223_372_036_854_775_807]   # Slice goes to i64::MAX. This works in Python but fails in PyO3
array([2999999997, 2999999998, 2999999999], dtype=int64)

Steps to Reproduce

use pyo3::{types::PySlice, Python};

fn main() {
    Python::with_gil(|py| {
        let slice = PySlice::new(py, 0, isize::MAX, 1);
        println!("{}", slice.call_method0("__str__").unwrap().extract::<String>().unwrap());
        // "slice(0, 9223372036854775807, 1)" on Linux
        // "slice(0, -1, 1)" on Windows
    });
}

Backtrace

No response

Your operating system and version

Windows

Your Python version (python --version)

3.10.5

Your Rust version (rustc --version)

rustc 1.65.0 (897e37553 2022-11-02)

Your PyO3 version

0.16.5

How did you install python? Did you use a virtualenv?

python.org installer

Additional Info

No response

@davidhewitt
Copy link
Member

@breckognize can you try against PyO3 main? I believe this is already fixed in #2769 and due to be released soon in 0.18.

@davidhewitt
Copy link
Member

Closing as duplicate.

@davidhewitt davidhewitt closed this as not planned Won't fix, can't repro, duplicate, stale Jan 4, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants