]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[BUG] stick-table: use the private buffer when padding strings
authorWilly Tarreau <w@1wt.eu>
Tue, 4 Jan 2011 05:29:44 +0000 (06:29 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 4 Jan 2011 05:29:44 +0000 (06:29 +0100)
Commit 035da6d1b0c436b85add48bc22120aa814c9cab9 was incorrect as it
could modify a live buffer. We must first ensure that we're on the
private buffer or perform a copy before modifying the data.

src/stick_table.c

index 40b2bff07e08ada33666a18c5276b8f71eeadb89..f080095b195f97f993f5c6e3ba38b93126e1c13a 100644 (file)
@@ -561,10 +561,22 @@ struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, st
                return NULL;
 
        if (t->type == STKTABLE_TYPE_STRING) {
-               /* The string MUST be terminated by a '\0' after the key_len bytes */
+               /* The string MUST be terminated by a '\0' after the key_len bytes. The problem
+                * is that we cannot modify the input data if it comes from the original buffer,
+                * so we copy it to a private buffer if required.
+                */
                if (static_table_key.key_len > t->key_size - 1)
                        static_table_key.key_len = t->key_size - 1;
-               ((char *)static_table_key.key)[static_table_key.key_len] = 0;
+
+               if (((char *)static_table_key.key)[static_table_key.key_len] != 0) {
+                       if ((char *)static_table_key.key < (char *)&static_table_key.data ||
+                           (char *)static_table_key.key >  (char *)&static_table_key.data + sizeof(static_table_key.data)) {
+                               /* key definitly not part of the static_table_key private data buffer */
+                               memcpy(static_table_key.data.buf, static_table_key.key, static_table_key.key_len);
+                               static_table_key.key = static_table_key.data.buf;
+                       }
+                       ((char *)static_table_key.key)[static_table_key.key_len] = 0;
+               }
        }
        else if (static_table_key.key_len < t->key_size) {
                /* need padding with null */