]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
ply-buffer: Nullify bytes outside scope block
authorRay Strode <rstrode@redhat.com>
Mon, 11 Dec 2023 15:29:20 +0000 (10:29 -0500)
committerRay Strode <rstrode@redhat.com>
Mon, 11 Dec 2023 15:42:46 +0000 (10:42 -0500)
ply_buffer_borrow_bytes is a new macro to make it possible to
temporarily alter the allocation of the underlying bytes of
a `ply_buffer_t` outside of `ply_buffer_t` specific methods.

This macro works by forcing the caller to use a scope block
to clearly delineate where the allocation modification is
occurring. This macro was inspired by the python `with` feature.

Unfortunately, the macro leaves the bytes initialized outside
the scope block, so there is some temptation to continue messing
with the allocation when it's not allowed.

This commit improves the macro to address that problem by nullifying
the passed in variables at the conclusion of the borrowing scope
block.

src/libply/ply-buffer.h

index fc7f4a8966034a0f90f5d1349a1ba9edefcd4f0e..2140e44d529fd625acbfd9cc9ba8af091edc11cf 100644 (file)
@@ -55,12 +55,18 @@ void ply_buffer_remove_bytes_at_end (ply_buffer_t *buffer,
 const char *ply_buffer_get_bytes (ply_buffer_t *buffer);
 size_t ply_buffer_get_capacity (ply_buffer_t *buffer);
 char *ply_buffer_steal_bytes (ply_buffer_t *buffer);
-#define ply_buffer_borrow_bytes(buffer, bytes, size, capacity)                    \
-        for (bool _ran = false; *bytes = (char *) ply_buffer_get_bytes (buffer),    \
-             *size = ply_buffer_get_size (buffer),                                \
-             *capacity = ply_buffer_get_capacity (buffer),                        \
-             !_ran;                                                               \
-             ply_buffer_set_bytes (buffer, *bytes, *size, *capacity), _ran = true)
+#define ply_buffer_borrow_bytes(buffer, bytes, size, capacity)                 \
+        for (bool _ran = false;                                                \
+             !_ran && (*bytes = (char *) ply_buffer_get_bytes (buffer),        \
+                       *size = ply_buffer_get_size (buffer),                   \
+                       *capacity = ply_buffer_get_capacity (buffer)),          \
+             !_ran;                                                            \
+             ply_buffer_set_bytes (buffer, *bytes, *size, *capacity),          \
+             _ran = true,                                                      \
+             *bytes = NULL,                                                    \
+             *size = 0,                                                        \
+             *capacity = 0)
+
 size_t ply_buffer_get_size (ply_buffer_t *buffer);
 void ply_buffer_clear (ply_buffer_t *buffer);
 #endif