From patchwork Wed Feb 17 00:01:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 583777 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 41BD6140291; Wed, 17 Feb 2016 11:01:47 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1aVpYv-0001Ep-Rk; Wed, 17 Feb 2016 00:01:45 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.76) (envelope-from ) id 1aVpYr-0001EK-I0 for fwts-devel@lists.ubuntu.com; Wed, 17 Feb 2016 00:01:41 +0000 Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1aVpYr-0000hK-8D; Wed, 17 Feb 2016 00:01:41 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] lib: fwts_klog: handle the case where klog_old is empty list Date: Wed, 17 Feb 2016 00:01:40 +0000 Message-Id: <1455667300-21365-1-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 2.7.0 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.14 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: fwts-devel-bounces@lists.ubuntu.com From: Colin Ian King scan-build detected that an empty klog_old list would lead to l_old_last being null and hence we get a null pointer dereference when assigning old to the data in the null list. Check for a empty list and ensure l_new is initialized to NULL for this corner case. Signed-off-by: Colin Ian King Acked-by: Alex Hung Acked-by: Ivan Hu --- src/lib/src/fwts_klog.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c index 48a0e45..b5f0965 100644 --- a/src/lib/src/fwts_klog.c +++ b/src/lib/src/fwts_klog.c @@ -51,7 +51,7 @@ void fwts_klog_free(fwts_list *klog) */ fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new) { - fwts_list_link *l_old, *l_new; + fwts_list_link *l_old, *l_new = NULL; fwts_list *klog_diff; if (klog_new == NULL) { @@ -74,14 +74,17 @@ fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new) fwts_list_foreach(l_old, klog_old) l_old_last = l_old; - /* And now look for that last line in the new log */ - old = fwts_list_data(char *, l_old_last); - fwts_list_foreach(l_new, klog_new) { - char *new = fwts_list_data(char *, l_new); - if (!strcmp(new, old)) { - /* Found last line that matches, bump to next */ - l_new = l_new->next; - break; + if (l_old_last) { + /* And now look for that last line in the new log */ + old = fwts_list_data(char *, l_old_last); + fwts_list_foreach(l_new, klog_new) { + const char *new = fwts_list_data(char *, l_new); + + if (!strcmp(new, old)) { + /* Found last line that matches, bump to next */ + l_new = l_new->next; + break; + } } } }