From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Cooper Subject: [PATCH] tools/xl: Fix trim() following c/s e316316 "xl: Rewrite trim()" Date: Thu, 16 Jul 2015 20:18:31 +0100 Message-ID: <1437074311-25446-1-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Xen-devel Cc: Andrew Cooper , Ian Jackson , Ian Campbell , Wei Liu List-Id: xen-devel@lists.xenproject.org c/s e316316 "xl: Rewrite trim()" uses the wrong indirection of 'output', causing memory corruption for all callers. Introduce a new local variable, making the code more obviously correct. Signed-off-by: Andrew Cooper CC: Ian Campbell CC: Ian Jackson CC: Wei Liu --- tools/libxl/xl_cmdimpl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 37d4af6..0cc9f8b 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -663,10 +663,12 @@ static void trim(char_predicate_t predicate, const char *input, char **output) ; size_t len_nonnull = after - first; + char *result = xmalloc(len_nonnull + 1); - *output = xmalloc(len_nonnull + 1); - memcpy(output, first, len_nonnull); - output[len_nonnull] = 0; + memcpy(result, first, len_nonnull); + result[len_nonnull] = 0; + + *output = result; } static int split_string_into_pair(const char *str, -- 1.7.10.4