]> arthur.barton.de Git - bup.git/commitdiff
cstr_from_bytes: fix null termination error
authorRob Browning <rlb@defaultvalue.org>
Sun, 3 Oct 2021 19:35:15 +0000 (14:35 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sun, 10 Oct 2021 17:01:28 +0000 (12:01 -0500)
Thanks to Johannes Berg for reporting the problem.

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

index 391597ff04a6eba86d1a253b7d310a396bf30245..fc1b69b75b45832614447ba29f9d701d3a1bbf24 100644 (file)
@@ -1902,10 +1902,18 @@ static char *cstr_from_bytes(PyObject *bytes)
     int rc = PyBytes_AsStringAndSize(bytes, &buf, &length);
     if (rc == -1)
         return NULL;
-    char *result = checked_malloc(length, sizeof(char));
+    size_t c_len;
+    if (!INT_ADD_OK(length, 1, &c_len)) {
+        PyErr_Format(PyExc_OverflowError,
+                     "Cannot convert ssize_t sized bytes object (%zd) to C string",
+                     length);
+        return NULL;
+    }
+    char *result = checked_malloc(c_len, sizeof(char));
     if (!result)
         return NULL;
     memcpy(result, buf, length);
+    result[length] = 0;
     return result;
 }