[PATCH 0/8] Move py C utility code to pyutil; guard overflow

6 views
Skip to first unread message

Rob Browning

unread,
May 30, 2023, 9:55:54 PM5/30/23
to bup-...@googlegroups.com
Proposed for main.

This starts moving some of the utility functions to src/bup/pyutil.*,
so that we can use them everywhere, e.g. hashsplit, bupsplit...

Rob Browning (8):
bup_shared_cflags: add -Winline
Create src/pyutil.c for utility functions
pyutil: add INTEGER_TO_PY as BUP_LONGISH_TO_PY
pyutil: add bup_uint_from_py bup_ulong_from_py bup_ullong_from_py
pyutil: add BUP_ASSIGN_PYLONG_TO_INTEGRAL; use EXPR_SIGNED
_helpers: remove vestigial py2 utimes related code
HashSplitter_init: guard against bits/fanbits overflow
Reject bup_getpwuid bup_getgrgid argument overflow

GNUmakefile | 4 +-
lib/bup/_hashsplit.c | 14 +-
lib/bup/_helpers.c | 368 +++++--------------------------------------
src/bup/pyutil.c | 93 +++++++++++
src/bup/pyutil.h | 54 +++++++
5 files changed, 196 insertions(+), 337 deletions(-)
create mode 100644 src/bup/pyutil.c
create mode 100644 src/bup/pyutil.h

--
2.39.2

Rob Browning

unread,
May 30, 2023, 9:55:54 PM5/30/23
to bup-...@googlegroups.com
Signed-off-by: Rob Browning <r...@defaultvalue.org>
---
GNUmakefile | 2 +-
lib/bup/_helpers.c | 29 ++---------------------------
src/bup/pyutil.c | 37 +++++++++++++++++++++++++++++++++++++
src/bup/pyutil.h | 4 ++++
4 files changed, 44 insertions(+), 28 deletions(-)
create mode 100644 src/bup/pyutil.c
create mode 100644 src/bup/pyutil.h

diff --git a/GNUmakefile b/GNUmakefile
index d762f32b7..d218698df 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -197,7 +197,7 @@ lib/cmd/bup: lib/cmd/bup.c src/bup/compat.c src/bup/io.c

clean_paths += lib/bup/_helpers$(soext)
generated_dependencies += lib/bup/_helpers.d
-lib/bup/_helpers$(soext): lib/bup/_helpers.c lib/bup/bupsplit.c lib/bup/_hashsplit.c
+lib/bup/_helpers$(soext): lib/bup/_helpers.c src/bup/pyutil.c lib/bup/bupsplit.c lib/bup/_hashsplit.c
$(CC) $(helpers_cflags) $(CPPFLAGS) $(CFLAGS) $^ \
$(helpers_ldflags) $(LDFLAGS) -o $@

diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c
index 507b5842e..6874a2bc6 100644
--- a/lib/bup/_helpers.c
+++ b/lib/bup/_helpers.c
@@ -64,8 +64,9 @@
# pragma GCC diagnostic pop
#endif

-#include "bupsplit.h"
#include "bup/intprops.h"
+#include "bup/pyutil.h"
+#include "bupsplit.h"
#include "_hashsplit.h"

#if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS)
@@ -108,32 +109,6 @@ typedef struct {
#define rbuf_argf "y#"
#define wbuf_argf "y*"

-
-static void *checked_calloc(size_t n, size_t size)
-{
- void *result = calloc(n, size);
- if (!result)
- PyErr_NoMemory();
- return result;
-}
-
-static void *checked_malloc(size_t n, size_t size)
-{
- size_t total;
- if (!INT_MULTIPLY_OK(n, size, &total))
- {
- PyErr_Format(PyExc_OverflowError,
- "request to allocate %zu items of size %zu is too large",
- n, size);
- return NULL;
- }
- void *result = malloc(total);
- if (!result)
- return PyErr_NoMemory();
- return result;
-}
-
-
#ifndef htonll
// This function should technically be macro'd out if it's going to be used
// more than ocasionally. As of this writing, it'll actually never be called
diff --git a/src/bup/pyutil.c b/src/bup/pyutil.c
new file mode 100644
index 000000000..53ca39c1d
--- /dev/null
+++ b/src/bup/pyutil.c
@@ -0,0 +1,37 @@
+#define _LARGEFILE64_SOURCE 1
+#define PY_SSIZE_T_CLEAN 1
+#undef NDEBUG
+#include "../../config/config.h"
+
+// According to Python, its header has to go first:
+// http://docs.python.org/3/c-api/intro.html#include-files
+#include <Python.h>
+
+#include "bup/pyutil.h"
+
+#include "bup/intprops.h"
+
+
+void *checked_calloc(size_t n, size_t size)
+{
+ void *result = calloc(n, size);
+ if (!result)
+ PyErr_NoMemory();
+ return result;
+}
+
+void *checked_malloc(size_t n, size_t size)
+{
+ size_t total;
+ if (!INT_MULTIPLY_OK(n, size, &total))
+ {
+ PyErr_Format(PyExc_OverflowError,
+ "request to allocate %zu items of size %zu is too large",
+ n, size);
+ return NULL;
+ }
+ void *result = malloc(total);
+ if (!result)
+ return PyErr_NoMemory();
+ return result;
+}
diff --git a/src/bup/pyutil.h b/src/bup/pyutil.h
new file mode 100644
index 000000000..680ca5460
--- /dev/null
+++ b/src/bup/pyutil.h
@@ -0,0 +1,4 @@
+#pragma once
+
+void *checked_calloc(size_t n, size_t size);
+void *checked_malloc(size_t n, size_t size);
--
2.39.2

Rob Browning

unread,
Sep 3, 2023, 12:40:44 PM9/3/23
to bup-...@googlegroups.com

Rob Browning <r...@defaultvalue.org> writes:

> Proposed for main.

dropped bup_shared_cflags: add -Winline
pushed Create src/pyutil.c for utility functions
pushed pyutil: add INTEGER_TO_PY as BUP_LONGISH_TO_PY
pushed pyutil: add bup_uint_from_py bup_ulong_from_py bup_ullong_from_py
pushed pyutil: add BUP_ASSIGN_PYLONG_TO_INTEGRAL; use EXPR_SIGNED
pushed _helpers: remove vestigial py2 utimes related code
pushed HashSplitter_init: guard against bits/fanbits overflow
pushed Reject bup_getpwuid bup_getgrgid argument overflow

--
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4
Reply all
Reply to author
Forward
0 new messages