Willy,

take note of the parameter of `istfree()`. It takes a pointer to adjust the
to-be-freed `ist` to be equivalent to `IST_NULL` (preventing use-after-free).
This usually requires the addition of the `&` operator within the call. I can
adjust this if you strongly dislike it.

Best regards
Tim Düsterhus

Apply with `git am --scissors` to automatically cut the commit message.

-- >8 --
`istalloc` allocates memory and returns an `ist` with the size `0` that points
to this allocation.

`istfree` frees the pointed memory and clears the pointer.
---
 include/common/ist.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/include/common/ist.h b/include/common/ist.h
index aa25a1f5c..db31544b4 100644
--- a/include/common/ist.h
+++ b/include/common/ist.h
@@ -32,6 +32,10 @@
 #include <string.h>
 #include <unistd.h>
 
+#ifndef IST_FREESTANDING
+#include <stdlib.h>
+#endif
+
 #include <common/config.h>
 
 /* ASCII to lower case conversion table */
@@ -729,4 +733,29 @@ static inline struct ist iststop(const struct ist ist, 
char chr)
                ;
        return ist2(ist.ptr, len - 1);
 }
+
+#ifndef IST_FREESTANDING
+/* This function allocates <size> bytes and returns an `ist` pointing to
+ * the allocated area with size `0`.
+ *
+ * If this function fails to allocate memory the return value is equivalent
+ * to IST_NULL.
+ */
+static inline struct ist istalloc(const size_t size)
+{
+       return ist2(malloc(size), 0);
+}
+
+/* This function performs the equivalent of free() on the given <ist>.
+ *
+ * After this function returns the value of the given <ist> will be
+ * modified to be equivalent to IST_NULL.
+ */
+static inline void istfree(struct ist *ist)
+{
+       free(ist->ptr);
+       *ist = IST_NULL;
+}
+#endif
+
 #endif
-- 
2.25.1


Reply via email to