]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: chunks: ensure that chunk_strcpy() adds a trailing zero
authorWilly Tarreau <w@1wt.eu>
Mon, 4 Jan 2016 19:21:33 +0000 (20:21 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 6 Jan 2016 12:53:37 +0000 (13:53 +0100)
Since thus function bears the name of a well-known string function, it
must at least promise compatible semantics. Here it means always adding
the trailing zero so that anyone willing to use chunk->str as a regular
string can do it. Of course the zero is not counted in the chunk's length.

include/common/chunk.h

index 35e47fb383228a2bd2f50e6ba2fee8bcc664bd03..00f0489113949ac540404d2b144741604ce3c490 100644 (file)
@@ -84,17 +84,20 @@ static inline void chunk_initstr(struct chunk *chk, char *str)
        chk->size = 0;                  /* mark it read-only */
 }
 
+/* copies str into <chk> followed by a trailing zero. Returns 0 in
+ * case of failure.
+ */
 static inline int chunk_strcpy(struct chunk *chk, const char *str)
 {
        size_t len;
 
        len = strlen(str);
 
-       if (unlikely(len > chk->size))
+       if (unlikely(len >= chk->size))
                return 0;
 
        chk->len  = len;
-       memcpy(chk->str, str, len);
+       memcpy(chk->str, str, len + 1);
 
        return 1;
 }