From 031ad23c47dea81fd5980c4c6823884abd406515 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 11 Dec 2013 17:32:08 +0100 Subject: [PATCH] MINOR: chunks: always initialize the output chunk in get_trash_chunk() The get_trash_chunk() function is convenient and is sometimes used even to get a temporary string. While the chunk is initialized, the string may contain some random garbage that some code might retrieve if it uses chunk->str directly without checking ->len. This is what happened in checks after commit 25e2ab5 (MEDIUM: checks: centralize error reporting). It's not easy to guess it at first so better pre-initialize the string with a zero. --- src/chunk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/chunk.c b/src/chunk.c index f84ab1051..d7d8501d6 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -33,7 +33,9 @@ static char *trash_buf2; * type of conversion. Two chunks and their respective buffers are alternatively * returned so that it is always possible to iterate data transformations without * losing the data being transformed. The blocks are initialized to the size of -* a standard buffer, so they should be enough for everything. +* a standard buffer, so they should be enough for everything. For convenience, +* a zero is always emitted at the beginning of the string so that it may be +* used as an empty string as well. */ struct chunk *get_trash_chunk(void) { @@ -47,6 +49,7 @@ struct chunk *get_trash_chunk(void) trash_chunk = &trash_chunk1; trash_buf = trash_buf1; } + *trash_buf = 0; chunk_init(trash_chunk, trash_buf, trash_size); return trash_chunk; }