Re: [PATCH] MINOR: ist: Add `istclear(struct ist*)`

2021-04-14 Thread Willy Tarreau
On Wed, Apr 14, 2021 at 07:14:30PM +0200, Tim Duesterhus wrote:
> > You can pass a pointer on dst. In the normalizer, you can update its 
> > size. It is thus possible to use dst when calling 
> > http_replace_req_path() or http_replace_req_query().
> > 
> 
> I see, that makes sense to me. I thought of the size being the current length
> of the ist and did not think of simply resetting it to 0 before starting the
> processing.
> 
> Here's a preparation patch to the ist library, allowing this pattern to be
> cleanly implemented. I've already incorporated it into my local normalization
> branch, but I'd like to implement the remaining feedback before sending a new
> series for it.

I like it. I thought we already had something to reset an ist that we
could repurpose, but no, right now resets are done by assigning so there
was nothing to do that yet.

Now applied, thanks!
Willy



[PATCH] MINOR: ist: Add `istclear(struct ist*)`

2021-04-14 Thread Tim Duesterhus
Christopher,
Willy,

On 4/13/21 6:34 PM, Christopher Faulet wrote:
>> Thus I can't simply take a `struct ist*` for the destination, as an ist
>> cannot communicate the size of the underlying buffer. I could
>> technically take a `struct buffer`, but I'd still like the result to
>> reside in an ist, because this is what the HTX API expects.
> 
> Hum, I don't understand. If you create an ist using the trash buffer 
> this way:
> 
> struct ist dst = ist2(replace->area, replace->size);
> 
> You can pass a pointer on dst. In the normalizer, you can update its 
> size. It is thus possible to use dst when calling 
> http_replace_req_path() or http_replace_req_query().
> 

I see, that makes sense to me. I thought of the size being the current length
of the ist and did not think of simply resetting it to 0 before starting the
processing.

Here's a preparation patch to the ist library, allowing this pattern to be
cleanly implemented. I've already incorporated it into my local normalization
branch, but I'd like to implement the remaining feedback before sending a new
series for it.

So: Please already take this patch if you think it is good.

Best regards
Tim Düsterhus

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

-- >8 --
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 | 30 ++
 1 file changed, 30 insertions(+)

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
-- 
2.31.1