From 0899d02b4cba33f02e8572bccb5dbfc10199b3fb Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Sun, 3 Oct 2021 14:35:15 -0500 Subject: [PATCH] cstr_from_bytes: fix null termination error Thanks to Johannes Berg for reporting the problem. Signed-off-by: Rob Browning Tested-by: Rob Browning --- lib/bup/_helpers.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c index 391597f..fc1b69b 100644 --- a/lib/bup/_helpers.c +++ b/lib/bup/_helpers.c @@ -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; } -- 2.39.2