diff --git a/include/import/ist.h b/include/import/ist.h index af9bbac3c..0dc3008f5 100644 --- a/include/import/ist.h +++ b/include/import/ist.h @@ -281,6 +281,36 @@ static inline struct ist isttrim(const struct ist ist, size_t size) return ret; } +/* Sets the of the 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 to no more than -1 characters and ensures that a * zero is placed after (possibly reduced by one) and before , * unless is already zero. The string is returned. This is mostly aimed