]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/_helpers.c
cstr_from_bytes: fix null termination error
[bup.git] / 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;
 }