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

Segfault on buffers with non-trivial strides #93

Closed
mhasself opened this issue Nov 15, 2022 · 2 comments
Closed

Segfault on buffers with non-trivial strides #93

mhasself opened this issue Nov 15, 2022 · 2 comments
Assignees
Labels

Comments

@mhasself
Copy link
Member

Instantiating G3Timestream from a numpy array works, but only if the buffer is close-packed in memory. Passing a view into an array, where strides are not 1, yields a segfault.

import spt3g
from spt3g import core
import numpy as np

print(spt3g.version.gitrevision)

for n in [1, 2, -2]:
    print(n)
    data = np.zeros(100, 'float64')[::n]
    print(core.G3Timestream(data))

Results in:

c45570b13da0cdaaab0b519344892d5053f99b20
1
100 samples at inf Hz
2
Segmentation fault (core dumped)

Expected result: the non-trivial stride should either quietly work, or raise an exception.

@arahlin
Copy link
Member

arahlin commented Nov 15, 2022

Does this fix it?

diff --git a/core/src/G3Timestream.cxx b/core/src/G3Timestream.cxx
index 62d5c40..84fec55 100644
--- a/core/src/G3Timestream.cxx
+++ b/core/src/G3Timestream.cxx
@@ -967,6 +967,7 @@ timestream_from_iterable(boost::python::object v,
                PyBuffer_Release(&view);
        } else {
                PyErr_Clear();
+               x = G3TimestreamPtr(new G3Timestream());
                boost::python::container_utils::extend_container(*x, v);
        }

@mhasself
Copy link
Member Author

It fixes the segfault but now I see random data:

for n in [1, 2, -2]:
    print(n)
    data = np.zeros(100, 'float64')[::n]
    ts = core.G3Timestream(data)
    print(ts[0], ts[1])

Output:

1
0.0 0.0
2
4.6811727725461e-310 4.68117277256586e-310
-2
4.68117272068817e-310 4.68117272070794e-310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants