From: Ray Strode Date: Mon, 11 Dec 2023 15:29:20 +0000 (-0500) Subject: ply-buffer: Nullify bytes outside scope block X-Git-Tag: 23.51.283~4^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=90a9174a810b317bec761f3f830fe3ae54f2d1f2;p=thirdparty%2Fplymouth.git ply-buffer: Nullify bytes outside scope block 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. --- diff --git a/src/libply/ply-buffer.h b/src/libply/ply-buffer.h index fc7f4a89..2140e44d 100644 --- a/src/libply/ply-buffer.h +++ b/src/libply/ply-buffer.h @@ -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