From a31244d098b7f249b0d0f2d89d3020ebd10d8ead Mon Sep 17 00:00:00 2001 From: Murai Takashi Date: Thu, 14 Feb 2019 06:28:33 +0900 Subject: [PATCH] mail: Fix potential memory leak Fix memory leak when realloc() failed. Pointed out by Clang Static Analyzer. Change-Id: I13f758c4d89275651e22785652d0880a6d5b1a8e Reviewed-on: https://review.haiku-os.org/c/1064 Reviewed-by: Adrien Destugues --- src/apps/mail/Content.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/apps/mail/Content.cpp b/src/apps/mail/Content.cpp index bd6f3bf0c1..716d97c282 100644 --- a/src/apps/mail/Content.cpp +++ b/src/apps/mail/Content.cpp @@ -3090,11 +3090,16 @@ TTextView::AddQuote(int32 start, int32 finish) // add quote to this line int32 lineLength = index - lastLine + 1; - target = (char *)realloc(target, targetLength + lineLength + quoteLength); - if (target == NULL) { - // free the old buffer? + char* newTarget = (char *)realloc(target, + targetLength + lineLength + quoteLength); + if (newTarget == NULL) { + // free the old buffer + free(target); + target = NULL; free(text); return; + } else { + target = newTarget; } // copy the quote sign