[PATCH 2/5] Raise ValueError when asked to encode negative vuint values

0 views
Skip to first unread message

Rob Browning

unread,
Mar 14, 2026, 4:57:45 PM (10 days ago) Mar 14
to bup-...@googlegroups.com
And use %zd for Py_ssize_t values.

Signed-off-by: Rob Browning <r...@defaultvalue.org>
Tested-by: Rob Browning <r...@defaultvalue.org>
---
lib/bup/_helpers.c | 10 +++++-----
lib/bup/vint.py | 2 +-
2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c
index 224a2ebf..ce6b6e9f 100644
--- a/lib/bup/_helpers.c
+++ b/lib/bup/_helpers.c
@@ -1097,7 +1097,7 @@ static unsigned int vuint_encode(long long val, char *buf)
unsigned int len = 0;

if (val < 0) {
- PyErr_SetString(PyExc_Exception, "vuints must not be negative");
+ PyErr_Format(PyExc_ValueError, "cannot encode negative value %llu", val);
return 0;
}

@@ -1709,8 +1709,8 @@ static PyObject *bup_limited_vint_pack(PyObject *self, PyObject *args)
case 'V': {
long long val = PyLong_AsLongLong(item);
if (val == -1 && PyErr_Occurred())
- return PyErr_Format(PyExc_OverflowError,
- "pack arg %d invalid", (int)i);
+ return PyErr_Format(PyExc_OverflowError, "pack arg %zd invalid",
+ i);
if (end - pos < 10)
goto overflow;
pos += vuint_encode(val, pos);
@@ -1719,8 +1719,8 @@ static PyObject *bup_limited_vint_pack(PyObject *self, PyObject *args)
case 'v': {
long long val = PyLong_AsLongLong(item);
if (val == -1 && PyErr_Occurred())
- return PyErr_Format(PyExc_OverflowError,
- "pack arg %d invalid", (int)i);
+ return PyErr_Format(PyExc_OverflowError, "pack arg %zd invalid",
+ i);
if (end - pos < 10)
goto overflow;
pos += vint_encode(val, pos);
diff --git a/lib/bup/vint.py b/lib/bup/vint.py
index 5c4b92e9..00f5b3c1 100644
--- a/lib/bup/vint.py
+++ b/lib/bup/vint.py
@@ -21,7 +21,7 @@ def encode_vuint(x):
return _helpers.vuint_encode(x)
except OverflowError:
if x < 0:
- raise Exception(f'cannot encode negative vuint {x}') from None
+ raise ValueError(f'cannot encode negative value {x}') from None
assert x != 0, 'the C code did not handle zero'
ret = b''
while True:
--
2.47.3

Reply all
Reply to author
Forward
0 new messages