]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ist: Add `istclear(struct ist*)`
authorTim Duesterhus <tim@bastelstu.be>
Wed, 14 Apr 2021 17:14:30 +0000 (19:14 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 14 Apr 2021 17:49:33 +0000 (19:49 +0200)
istclear allows one to easily reset an ist to zero-size, while preserving the
previous size, indicating the length of the underlying buffer.

include/import/ist.h

index af9bbac3cf38a1fa48d0b6e5c502a2ece7da1015..0dc3008f5c26ce569a59cb103751b165563fdf81 100644 (file)
@@ -281,6 +281,36 @@ static inline struct ist isttrim(const struct ist ist, size_t size)
        return ret;
 }
 
+/* Sets the <len> of the <ist> to zero and returns the previous length.
+ *
+ * This function is meant to be used in functions that receive an ist containing
+ * the destination buffer and the buffer's size. The returned size must be stored
+ * to prevent an overflow of such a destination buffer.
+ *
+ * If you simply want to clear an ist and do not care about the previous length
+ * then you should use `isttrim(ist, 0)`.
+ *
+ * Example Usage (fill the complete buffer with 'x'):
+ *
+ * void my_func(struct ist* dst)
+ * {
+ *     size_t dst_size = istclear(dst);
+ *     size_t i;
+ *
+ *     for (i = 0; i < dst_size; i++)
+ *             *dst = __istappend(*dst, 'x');
+ * }
+ */
+__attribute__((warn_unused_result))
+static inline size_t istclear(struct ist* ist)
+{
+       size_t len = ist->len;
+
+       ist->len = 0;
+
+       return len;
+}
+
 /* trims string <ist> to no more than <size>-1 characters and ensures that a
  * zero is placed after <ist.len> (possibly reduced by one) and before <size>,
  * unless <size> is already zero. The string is returned. This is mostly aimed