diff options
author | Lars Wirzenius <liw@liw.fi> | 2014-03-05 18:57:06 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2014-03-05 18:57:06 +0000 |
commit | 9e926fd7d88afcad9ea6abcde9c826ebe432f485 (patch) | |
tree | d989afaa4dc414ae40982d6ba72cfe2048d90224 | |
parent | 93eb916b7315f5f52e8fa5e169916d4c75707a5e (diff) | |
download | obnam-9e926fd7d88afcad9ea6abcde9c826ebe432f485.tar.gz |
Catch and handle malloc errors in _obnammodule.py
-rw-r--r-- | _obnammodule.c | 12 | ||||
-rw-r--r-- | obnamlib/vfs_local.py | 9 |
2 files changed, 21 insertions, 0 deletions
diff --git a/_obnammodule.c b/_obnammodule.c index 75cf3960..94dea0fc 100644 --- a/_obnammodule.c +++ b/_obnammodule.c @@ -183,6 +183,10 @@ llistxattr_wrapper(PyObject *self, PyObject *args) #ifdef __FreeBSD__ bufsize = extattr_list_link(filename, EXTATTR_NAMESPACE_USER, NULL, 0); buf = malloc(bufsize); + if (buf == NULL) { + Py_INCREF(Py_None); + return Py_None; + } n = extattr_list_link(filename, EXTATTR_NAMESPACE_USER, buf, bufsize); if (n >= 0) { /* Convert from length-prefixed BSD style to '\0'-suffixed @@ -205,6 +209,10 @@ llistxattr_wrapper(PyObject *self, PyObject *args) do { bufsize += 1024; buf = malloc(bufsize); + if (buf == NULL) { + Py_INCREF(Py_None); + return Py_None; + } n = llistxattr(filename, buf, bufsize); if (n >= 0) @@ -234,6 +242,10 @@ lgetxattr_wrapper(PyObject *self, PyObject *args) do { bufsize += 1024; char *buf = malloc(bufsize); + if (buf == NULL) { + Py_INCREF(Py_None); + return Py_None; + } #ifdef __FreeBSD__ int n = extattr_get_link(filename, EXTATTR_NAMESPACE_USER, attrname, buf, bufsize); #else diff --git a/obnamlib/vfs_local.py b/obnamlib/vfs_local.py index 7453786b..e549d069 100644 --- a/obnamlib/vfs_local.py +++ b/obnamlib/vfs_local.py @@ -33,6 +33,11 @@ import obnamlib EXTRA_OPEN_FLAGS = getattr(os, "O_NOATIME", 0) +class MallocError(obnamlib.ObnamError): + + msg = 'malloc out of memory while calling {function}' + + class LocalFSFile(file): def read(self, amount=-1): @@ -179,12 +184,16 @@ class LocalFS(obnamlib.VirtualFileSystem): def llistxattr(self, filename): # pragma: no cover ret = obnamlib._obnam.llistxattr(self.join(filename)) + if ret is None: + raise MallocError(function='llistxattr') if type(ret) is int: raise OSError(ret, os.strerror(ret), filename) return [s for s in ret.split('\0') if s] def lgetxattr(self, filename, attrname): # pragma: no cover ret = obnamlib._obnam.lgetxattr(self.join(filename), attrname) + if ret is None: + raise MallocError(function='llistxattr') if type(ret) is int: raise OSError(ret, os.strerror(ret), filename) return ret |