]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: sample: add smp_prepare_out() to prepare an output buffer
authorWilly Tarreau <w@1wt.eu>
Wed, 21 Oct 2026 21:01:06 +0000 (23:01 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Nov 2026 10:08:46 +0000 (11:08 +0100)
Converters that transform their input without making it grow (case
conversion, character deletion etc) usually call smp_make_rw() then work
in place. When the input is read-only, which is the most common case,
smp_dup() first copies the whole input into a trash chunk, and the
converter then performs a second pass over it.

smp_prepare_out() avoids this copy: for a R/W sample of type STR or BIN,
it leaves the sample as-is, otherwise it replaces the sample's buffer with
a new trash chunk at least as large as the input (with the same truncation
rules as smp_dup()) and clears the "const" flag. The caller saves the
input's area pointer before the call, then reads from it and writes into
the sample's area. Since both may alias when the sample was R/W, the
caller must never write ahead of where it reads.

include/haproxy/sample.h
src/sample.c

index 96ae06757b475adcea3625014873f74b51b74ada..c0baaca04d25841224faa56d1aa35e78213d3f7d 100644 (file)
@@ -63,6 +63,7 @@ int smp_expr_output_type(struct sample_expr *expr);
 int c_none(struct sample *smp);
 int c_pseudo(struct sample *smp);
 int smp_dup(struct sample *smp);
+int smp_prepare_out(struct sample *smp);
 int sample_check_arg_base64(struct arg *arg, char **err);
 
 /*
index a9dfcdb1c81fc2e18825d0a9282ed44997c962dc..06f3d75e3dc0ef985e68bdd06cc9081d0fd6b3d1 100644 (file)
@@ -940,6 +940,55 @@ int smp_dup(struct sample *smp)
        return 1;
 }
 
+/* Prepares a sample of type STR or BIN to receive the output of a
+ * transformation of its own contents which never produces more bytes than it
+ * consumes (e.g. case conversion, character deletion). The caller must first
+ * save the input's area pointer. On success, the function returns 1 and the
+ * sample's buffer describes the output area: its <data> is set to the number
+ * of input bytes to process, and the "const" flag is cleared. The caller then
+ * writes the output into this area and updates <data> accordingly. The type is
+ * left unchanged, and the trailing zero of strings will be appended by any
+ * further call to smp_is_rw(). If the sample is R/W, the output area is the
+ * input one, so that the caller must be careful never to write ahead of where
+ * it reads. Otherwise it is a new trash chunk at least as large as the input,
+ * avoiding the copy that smp_make_rw() would perform. As with smp_dup(), the
+ * input length may be truncated to the trash size (minus one for strings).
+ * Returns 0 on failure or for other types, in which case the sample is left
+ * unmodified.
+ */
+int smp_prepare_out(struct sample *smp)
+{
+       struct buffer *trash;
+       size_t len;
+
+       if (smp->data.type != SMP_T_STR && smp->data.type != SMP_T_BIN)
+               return 0;
+
+       if (smp_is_rw(smp))
+               return 1;
+
+       len = smp->data.u.str.data;
+       if (smp->data.type == SMP_T_STR) {
+               trash = get_best_trash_chunk(&smp->data.u.str, len + 1);
+               if (!trash)
+                       return 0;
+               if (len > trash->size - 1)
+                       len = trash->size - 1;
+       }
+       else {
+               trash = get_trash_chunk_sz(len);
+               if (!trash)
+                       return 0;
+               if (len > trash->size)
+                       len = trash->size;
+       }
+
+       trash->data = len;
+       smp->data.u.str = *trash;
+       smp->flags &= ~SMP_F_CONST;
+       return 1;
+}
+
 int c_none(struct sample *smp)
 {
        return 1;