From patchwork Mon Dec 3 13:19:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 10709543 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 35BF51731 for ; Mon, 3 Dec 2018 13:19:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 26B032B6B3 for ; Mon, 3 Dec 2018 13:19:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1A86E2B6A4; Mon, 3 Dec 2018 13:19:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7E35E2B6B1 for ; Mon, 3 Dec 2018 13:19:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725976AbeLCNUa (ORCPT ); Mon, 3 Dec 2018 08:20:30 -0500 Received: from sauhun.de ([88.99.104.3]:55752 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725924AbeLCNUa (ORCPT ); Mon, 3 Dec 2018 08:20:30 -0500 Received: from localhost (p54B3353C.dip0.t-ipconnect.de [84.179.53.60]) by pokefinder.org (Postfix) with ESMTPSA id F42063E17CF; Mon, 3 Dec 2018 14:19:27 +0100 (CET) From: Wolfram Sang To: linux-mmc@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Chris Ball , Wolfram Sang Subject: [PATCH mmc-utils] fix GCC7 build by refactoring trimming routines Date: Mon, 3 Dec 2018 14:19:23 +0100 Message-Id: <20181203131923.1487-1-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.11.0 Sender: linux-renesas-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-renesas-soc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP I got a compile error with GCC7. When trimming white spaces from strings lsmmc uses strncpy with overlapping memory areas. This is not allowed. In addition, the implementation was not efficient with calling strlen and strncpy once per iteration. Refactor the code to be valid and more effective. Signed-off-by: Wolfram Sang --- lsmmc.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lsmmc.c b/lsmmc.c index c4faa00..9737b37 100644 --- a/lsmmc.c +++ b/lsmmc.c @@ -316,8 +316,9 @@ int parse_ids(struct config *config) /* MMC/SD file parsing functions */ char *read_file(char *name) { - char *preparsed; char line[4096]; + char *preparsed, *start = line; + int len; FILE *f; f = fopen(name, "r"); @@ -348,12 +349,17 @@ char *read_file(char *name) } line[sizeof(line) - 1] = '\0'; + len = strlen(line); - while (isspace(line[strlen(line) - 1])) - line[strlen(line) - 1] = '\0'; + while (len > 0 && isspace(line[len - 1])) + len--; - while (isspace(line[0])) - strncpy(&line[0], &line[1], sizeof(line)); + while (len > 0 && isspace(*start)) { + start++; + len--; + } + memmove(line, start, len); + line[len] = '\0'; return strdup(line); }