]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: make the bool type cast to bin
authorWilly Tarreau <w@1wt.eu>
Mon, 25 Apr 2022 08:46:16 +0000 (10:46 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 25 Apr 2022 14:09:26 +0000 (16:09 +0200)
Any type except bool could cast to bin, while it can cast to string.
That's a bit inconsistent, and prevents a boolean from being used as
the entry of a hash function while any other type can. This is a
problem when passing via variable where someone could use:

    ... set-var(txn.bar) always_false

to temporarily disable something, but this would result in an empty
hash output when later doing:

    ... var(txn.bar),sdbm

Instead of using c_int2bin() as is done for the string output, better
enfore an set of inputs or exactly 0 or 1 so that a poorly written sample
fetch function does not result in a difficult to debug hash output.

src/sample.c

index 1278ef9387c85f626d1a02ee378c92f9f690532a..870f5dcd854a0179d20c97e73902487d5adb166d 100644 (file)
@@ -958,6 +958,17 @@ static int c_int2bin(struct sample *smp)
        return 1;
 }
 
+static int c_bool2bin(struct sample *smp)
+{
+       struct buffer *chk = get_trash_chunk();
+
+       *(unsigned long long int *)chk->area = my_htonll(!!smp->data.u.sint);
+       chk->data = 8;
+       smp->data.u.str = *chk;
+       smp->data.type = SMP_T_BIN;
+       return 1;
+}
+
 
 /*****************************************************************/
 /*      Sample casts matrix:                                     */
@@ -968,7 +979,7 @@ static int c_int2bin(struct sample *smp)
 sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
 /*            to:  ANY     BOOL       SINT       ADDR        IPV4      IPV6        STR         BIN         METH */
 /* from:  ANY */ { c_none, c_none,    c_none,    c_none,     c_none,   c_none,     c_none,     c_none,     c_none,     },
-/*       BOOL */ { c_none, c_none,    c_none,    NULL,       NULL,     NULL,       c_int2str,  NULL,       NULL,       },
+/*       BOOL */ { c_none, c_none,    c_none,    NULL,       NULL,     NULL,       c_int2str,  c_bool2bin, NULL,       },
 /*       SINT */ { c_none, c_none,    c_none,    c_int2ip,   c_int2ip, c_int2ipv6, c_int2str,  c_int2bin,  NULL,       },
 /*       ADDR */ { c_none, NULL,      NULL,      NULL,       NULL,     NULL,       NULL,       NULL,       NULL,       },
 /*       IPV4 */ { c_none, NULL,      c_ip2int,  c_none,     c_none,   c_ip2ipv6,  c_ip2str,   c_addr2bin, NULL,       },