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

Fix invalid pointer to urlpath argument #47

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions blosc2/blosc2_ext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ from enum import Enum
import blosc2


cdef extern int blosc2_remove_urlpath(const char *path)

cdef extern from "<stdint.h>":
ctypedef signed char int8_t
ctypedef signed short int16_t
Expand Down Expand Up @@ -357,6 +355,7 @@ cdef extern from "blosc2.h":
void blosc1_set_schunk(blosc2_schunk *schunk)

int blosc2_remove_dir(const char *path)
int blosc2_remove_urlpath(const char *path)


MAX_TYPESIZE = BLOSC_MAX_TYPESIZE
Expand Down Expand Up @@ -666,11 +665,10 @@ storage_dflts = {
cdef create_storage(blosc2_storage *storage, kwargs):
contiguous = kwargs.get('contiguous', storage_dflts['contiguous'])
urlpath = kwargs.get('urlpath', storage_dflts['urlpath'])
urlpath = urlpath.encode("utf-8") if isinstance(urlpath, str) else urlpath
if urlpath is None:
storage.urlpath = NULL
else:
storage.urlpath = <char *> urlpath
storage.urlpath = urlpath
if kwargs.get('cparams', None) is not None:
create_cparams_from_kwargs(storage.cparams, kwargs.get('cparams'))

Expand All @@ -687,15 +685,21 @@ cdef class SChunk:
cdef blosc2_schunk *schunk

def __init__(self, schunk=None, chunksize=8*10**6, data=None, mode="a", **kwargs):
# hold on to a bytestring of urlpath for the lifetime of the instance
# because its value is referenced via a C-pointer
urlpath = kwargs.get("urlpath", None)
if urlpath is not None:
self._urlpath = urlpath.encode() if isinstance(urlpath, str) else urlpath
kwargs["urlpath"] = self._urlpath

if schunk is not None:
self.schunk = <blosc2_schunk *> PyCapsule_GetPointer(schunk, <char *> "blosc2_schunk*")
if mode == "w" and kwargs["urlpath"] is not None:
blosc2.remove_urlpath(kwargs["urlpath"])
if mode == "w" and urlpath is not None:
blosc2.remove_urlpath(urlpath)
self.schunk = blosc2_schunk_new(self.schunk.storage)
return

if kwargs is not None:
urlpath = kwargs.get("urlpath", None)
if mode == "w":
blosc2.remove_urlpath(urlpath)
elif mode == "r" and urlpath is not None:
Expand Down Expand Up @@ -873,7 +877,8 @@ cdef class SChunk:
return rc

def __dealloc__(self):
blosc2_schunk_free(self.schunk)
if self.schunk != NULL:
blosc2_schunk_free(self.schunk)


def remove_urlpath(path):
Expand Down Expand Up @@ -910,6 +915,8 @@ cdef class vlmeta:
def schunk_open(urlpath, mode, **kwargs):
urlpath = urlpath.encode("utf-8") if isinstance(urlpath, str) else urlpath
cdef blosc2_schunk* schunk = blosc2_schunk_open(urlpath)
if schunk == NULL:
raise RuntimeError(f'blosc2_schunk_open({urlpath!r}) returned NULL')
kwargs["urlpath"] = urlpath
kwargs["contiguous"] = schunk.storage.contiguous
if mode != "w" and kwargs is not None:
Expand Down