]> arthur.barton.de Git - bup.git/commitdiff
Handle sysconf results more carefully
authorRob Browning <rlb@defaultvalue.org>
Tue, 14 Jul 2015 00:33:24 +0000 (19:33 -0500)
committerRob Browning <rlb@defaultvalue.org>
Tue, 14 Jul 2015 00:49:59 +0000 (19:49 -0500)
Sysconf indicates that there's no definite limit by returning -1 and
leaving errno unchanged.  In that case, for SC_ARG_MAX, guess 4096.  For
SC_PAGE_SIZE, die, since various operations currently require a
page_size.

Thanks to Mark J Hewitt for reporting the issue, and to Mark and Nix for
investigating the cause.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/_helpers.c
lib/bup/helpers.py

index 88653ac94a53ebcdf184ba96e88b9e2cdf5980c2..ee216f4cb75601e9ab038d15277ebdcb00a97c13 100644 (file)
@@ -1339,6 +1339,17 @@ static PyObject *bup_fmincore(PyObject *self, PyObject *args)
     if (!PyArg_ParseTuple(args, "i", &fd))
        return NULL;
 
+    errno = 0;
+    const long sc_page_size = sysconf(_SC_PAGESIZE);
+    if (sc_page_size == -1) // Stymied - mincore works in page_size chunks
+    {
+        if (errno)
+            return PyErr_SetFromErrno(PyExc_OSError);
+        else
+            PyErr_Format(PyExc_RuntimeError,
+                         "cannot determine memory page size");
+    }
+    
     struct stat st;
     rc = fstat(fd, &st);
     if (rc != 0)
@@ -1347,7 +1358,10 @@ static PyObject *bup_fmincore(PyObject *self, PyObject *args)
     if (st.st_size == 0)
         return Py_BuildValue("s", "");
 
-    const size_t page_size = (size_t) sysconf (_SC_PAGESIZE);
+    size_t page_size;
+    if (!INTEGRAL_ASSIGNMENT_FITS(&page_size, sc_page_size))
+        return PyErr_Format(PyExc_OverflowError, "page size too large");
+
     const off_t pref_chunk_size = 64 * 1024 * 1024;
     off_t chunk_size = page_size;
     if (page_size < pref_chunk_size)
index a9142c64581b1f8e6314fe89fcd8658bfae7e184..4cbd2dc3c36f21dcc1e58db073381035b9af609b 100644 (file)
@@ -10,7 +10,11 @@ import hashlib, heapq, math, operator, time, grp, tempfile
 from bup import _helpers
 
 sc_page_size = os.sysconf('SC_PAGE_SIZE')
+assert(sc_page_size > 0)
+
 sc_arg_max = os.sysconf('SC_ARG_MAX')
+if sc_arg_max == -1:
+    sc_arg_max = 4096
 
 # This function should really be in helpers, not in bup.options.  But we
 # want options.py to be standalone so people can include it in other projects.