]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/_helpers.c
bup_gethostname: always null terminate
[bup.git] / lib / bup / _helpers.c
index fc1b69b75b45832614447ba29f9d701d3a1bbf24..330187cb82c6dabb291c00a727bcfc9d54194f87 100644 (file)
@@ -620,8 +620,14 @@ static PyObject *bitmatch(PyObject *self, PyObject *args)
        }
     }
     
-    assert(byte <= (INT_MAX >> 3));
-    return Py_BuildValue("i", byte*8 + bit);
+    unsigned long long result;
+    if (!INT_MULTIPLY_OK(byte, 8, &result)
+        || !INT_ADD_OK(result, bit, &result))
+    {
+        PyErr_Format(PyExc_OverflowError, "bitmatch bit count too large");
+        return NULL;
+    }
+    return PyLong_FromUnsignedLongLong(result);
 }
 
 
@@ -1889,6 +1895,7 @@ static PyObject *bup_gethostname(PyObject *mod, PyObject *ignore)
 
     if (gethostname(buf, sizeof(buf) - 1))
         return PyErr_SetFromErrno(PyExc_IOError);
+    buf[sizeof(buf) - 1] = 0;
     return PyBytes_FromString(buf);
 }