From: Willy Tarreau Date: Wed, 21 Oct 2026 21:01:06 +0000 (+0200) Subject: MINOR: sample: add smp_prepare_out() to prepare an output buffer X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/gitweb.js?a=commitdiff_plain;h=4d8d922e262264878646c2d5e9c033f28ba6317e;p=haproxy.git MINOR: sample: add smp_prepare_out() to prepare an output buffer 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. --- diff --git a/include/haproxy/sample.h b/include/haproxy/sample.h index 96ae06757..c0baaca04 100644 --- a/include/haproxy/sample.h +++ b/include/haproxy/sample.h @@ -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); /* diff --git a/src/sample.c b/src/sample.c index a9dfcdb1c..06f3d75e3 100644 --- a/src/sample.c +++ b/src/sample.c @@ -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 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 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;