mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-09-21 22:01:31 +02:00
MINOR: chunks: ensure that chunk_strcpy() adds a trailing zero
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.
This commit is contained in:
parent
6c25e9e83a
commit
0b6044fa24
@ -84,17 +84,20 @@ static inline void chunk_initstr(struct chunk *chk, char *str)
|
|||||||
chk->size = 0; /* mark it read-only */
|
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)
|
static inline int chunk_strcpy(struct chunk *chk, const char *str)
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
|
|
||||||
if (unlikely(len > chk->size))
|
if (unlikely(len >= chk->size))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
chk->len = len;
|
chk->len = len;
|
||||||
memcpy(chk->str, str, len);
|
memcpy(chk->str, str, len + 1);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user