]> git.kaiwu.me - haproxy.git/commitdiff
OPTIM: sample: use smp_prepare_out() in the lower and upper converters
authorWilly Tarreau <w@1wt.eu>
Wed, 21 Oct 2026 21:05:05 +0000 (23:05 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Nov 2026 10:08:46 +0000 (11:08 +0100)
These converters used to call smp_make_rw() then convert the string in
place. Most of the time their input is read-only (e.g. a header), in
which case smp_dup() first copied it into a trash chunk before the
conversion performed a second pass over it. By relying on
smp_prepare_out() instead, the converted string is directly written into
the trash chunk while reading the input, saving one pass. Writable inputs
are still converted in place.

src/sample.c

index 06f3d75e3dc0ef985e68bdd06cc9081d0fd6b3d1..bf5bc34a4fea3a8ad271718bdd655aa26cdb6097 100644 (file)
@@ -2477,28 +2477,40 @@ static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void
 
 static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
 {
+       const char *src = smp->data.u.str.area;
+       char *dst;
        int i;
 
-       if (!smp_make_rw(smp))
+       /* <dst> may alias <src>, we never write ahead of where we read */
+       if (!smp_prepare_out(smp))
                return 0;
 
+       dst = smp->data.u.str.area;
        for (i = 0; i < smp->data.u.str.data; i++) {
-               if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
-                       smp->data.u.str.area[i] += 'a' - 'A';
+               if ((src[i] >= 'A') && (src[i] <= 'Z'))
+                       dst[i] = src[i] + 'a' - 'A';
+               else
+                       dst[i] = src[i];
        }
        return 1;
 }
 
 static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
 {
+       const char *src = smp->data.u.str.area;
+       char *dst;
        int i;
 
-       if (!smp_make_rw(smp))
+       /* <dst> may alias <src>, we never write ahead of where we read */
+       if (!smp_prepare_out(smp))
                return 0;
 
+       dst = smp->data.u.str.area;
        for (i = 0; i < smp->data.u.str.data; i++) {
-               if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
-                       smp->data.u.str.area[i] += 'A' - 'a';
+               if ((src[i] >= 'a') && (src[i] <= 'z'))
+                       dst[i] = src[i] + 'A' - 'a';
+               else
+                       dst[i] = src[i];
        }
        return 1;
 }