]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: sample: change the behavior of the bin2str cast
authorThierry FOURNIER <tfournier@exceliance.fr>
Wed, 12 Mar 2014 14:07:59 +0000 (15:07 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 17 Mar 2014 16:31:46 +0000 (17:31 +0100)
The bin2str cast gives the hexadecimal representation of the binary
content when it is used as string. This was inherited from the
stick-table casts without realizing that it was a mistake. Indeed,
it breaks string processing on binary contents, preventing any _reg,
_beg, etc from working.

For example, with an HTTP GET request, the fetch "req.payload(0,3)"
returns the 3 bytes "G", "E", and "T" in binary. If this fetch is
used with regex, it is automatically converted to "474554" and the
regex is applied on this string, so it never matches.

This commit changes the cast so that bin2str does not convert the
contents anymore, and returns a string type. The contents can thus
be matched as is, and the NULL character continues to mark the end
of the string to avoid any issue with some string-based functions.

This commit could almost have been marked as a bug fix since it
does what the doc says.

Note that in case someone would rely on the hex encoding, then the
same behaviour could be achieved by appending ",hex" after the sample
fetch function (brought by previous patch).

src/sample.c

index 3812913c902b35c82a89f3e7135d54043e45b591..8584dbe8e2a6d1ba0fe094f7671d7ff5edbdb6c8 100644 (file)
@@ -477,18 +477,24 @@ static int c_str2ipv6(struct sample *smp)
        return 1;
 }
 
+/* The sample is always copied into a new one so that smp->size is always
+ * valid. The NULL char always enforces the end of string if it is met.
+ */
 static int c_bin2str(struct sample *smp)
 {
        struct chunk *trash = get_trash_chunk();
        unsigned char c;
        int ptr = 0;
 
-       trash->len = 0;
-       while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
-               c = smp->data.str.str[ptr++];
-               trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
-               trash->str[trash->len++] = hextab[c & 0xF];
+       while (ptr < smp->data.str.len) {
+               c = smp->data.str.str[ptr];
+               if (!c)
+                       break;
+               trash->str[ptr] = c;
+               ptr++;
        }
+       trash->len = ptr;
+       trash->str[ptr] = 0;
        smp->data.str = *trash;
        smp->type = SMP_T_STR;
        return 1;