From patchwork Sat Apr 2 17:52:03 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: 605445 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 3qcm6f6d76z9sBm; Sun, 3 Apr 2016 03:52:26 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1amPij-0002CV-Mh; Sat, 02 Apr 2016 17:52:25 +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 1amPiR-00027y-Fd for fwts-devel@lists.ubuntu.com; Sat, 02 Apr 2016 17:52:07 +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 1amPiR-0001kh-2o; Sat, 02 Apr 2016 17:52:07 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH 4/5] lib: fwts_olog: ensure buffer is '\0' terminated per read, minor cleanups Date: Sat, 2 Apr 2016 18:52:03 +0100 Message-Id: <1459619524-12635-5-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1459619524-12635-1-git-send-email-colin.king@canonical.com> References: <1459619524-12635-1-git-send-email-colin.king@canonical.com> 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 CoverityScan warned that the buffer should be terminated with a '\0' on each read, so fix this. Also, clean up the code a bit (minor white space changes) and ensure read/write sizes use size_t and not long. Signed-off-by: Colin Ian King Acked-by: Alex Hung Acked-by: Ivan Hu --- src/lib/src/fwts_olog.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/lib/src/fwts_olog.c b/src/lib/src/fwts_olog.c index a685d70..132efc4 100644 --- a/src/lib/src/fwts_olog.c +++ b/src/lib/src/fwts_olog.c @@ -47,12 +47,12 @@ static const char msglog_outfile[] = "/var/log/opal_msglog"; */ fwts_list *fwts_olog_read(fwts_framework *fw) { - long len; fwts_list *list; char *buffer; struct stat filestat; - long read_actual=0; - long write_actual=0; + long len; + size_t read_actual = 0; + size_t write_actual = 0; FILE* msglog_f; FILE* msglog_outfile_f; @@ -62,7 +62,7 @@ fwts_list *fwts_olog_read(fwts_framework *fw) * and not just for PPC. We don't use compiler flags since we want * to run this as a custom job cross platform */ - if (stat(msglog,&filestat)) { + if (stat(msglog, &filestat)) { /* * stat fails so not PPC with OPAL msglog and * no -o OLOG sent @@ -88,7 +88,7 @@ fwts_list *fwts_olog_read(fwts_framework *fw) goto olog_cleanup_msglog; } - if ((buffer = calloc(1,len+1)) == NULL) { + if ((buffer = calloc(1, len + 1)) == NULL) { goto olog_cleanup_msglog; } @@ -98,11 +98,13 @@ fwts_list *fwts_olog_read(fwts_framework *fw) goto olog_cleanup_msglog; } - while (!feof (msglog_f)) { - read_actual = fread(buffer,1,len,msglog_f); - if (read_actual == len) { + while (!feof(msglog_f)) { + read_actual = fread(buffer, 1, len, msglog_f); + buffer[read_actual] = '\0'; + + if (read_actual == (size_t)len) { write_actual = fwrite(buffer, 1, len, msglog_outfile_f); - if (!(write_actual == len)) { + if (!(write_actual == (size_t)len)) { free(buffer); goto olog_cleanup_common; } @@ -133,20 +135,20 @@ fwts_list *fwts_olog_read(fwts_framework *fw) if (!(msglog_outfile_f = fopen(msglog_outfile, "r"))) goto olog_common_exit; - if (fseek(msglog_outfile_f,0,SEEK_END)) + if (fseek(msglog_outfile_f, 0, SEEK_END)) goto olog_cleanup_msglog_outfile; if ((len = ftell(msglog_outfile_f)) == -1) goto olog_cleanup_msglog_outfile; - if ((fseek(msglog_outfile_f,0,SEEK_SET)) != 0) + if ((fseek(msglog_outfile_f, 0, SEEK_SET)) != 0) goto olog_cleanup_msglog_outfile; if ((buffer = calloc(1, len+1)) == NULL) goto olog_cleanup_msglog_outfile; - read_actual = fread(buffer,1,len,msglog_outfile_f); - if (read_actual == len) { + read_actual = fread(buffer, 1, len, msglog_outfile_f); + if (read_actual == (size_t)len) { list = fwts_list_from_text(buffer); free(buffer); (void)fclose(msglog_outfile_f);