From patchwork Sun Nov 4 13:21:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Heimpold X-Patchwork-Id: 10666867 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 89C6F175A for ; Sun, 4 Nov 2018 13:28:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6972C29968 for ; Sun, 4 Nov 2018 13:28:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58FC32998D; Sun, 4 Nov 2018 13:28:12 +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.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 D8D5929968 for ; Sun, 4 Nov 2018 13:28:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727537AbeKDWnJ (ORCPT ); Sun, 4 Nov 2018 17:43:09 -0500 Received: from mo4-p01-ob.smtp.rzone.de ([85.215.255.54]:17118 "EHLO mo4-p01-ob.smtp.rzone.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726570AbeKDWnJ (ORCPT ); Sun, 4 Nov 2018 17:43:09 -0500 X-Greylist: delayed 366 seconds by postgrey-1.27 at vger.kernel.org; Sun, 04 Nov 2018 17:43:07 EST DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1541338087; s=strato-dkim-0002; d=heimpold.de; h=Message-Id:Date:Subject:Cc:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=xca7LQqvwknuA1aV4b4hucG8LwW/yot9lbXirk6f2pk=; b=tljifBNo84JodDRprkq5/h6ZdOazrJ8XA9SZ3gEX3/vUwz57au5D1UXwDWNnZvNwS2 j4jHaCtlbC7rqAcYMkT4hF5gK1kHQcVJ+Wsd8F8lx6VplO6nRMB/piuM5yrCXPawjFwd AXl/Q1hrjzYHJkY2jF4LzAW7hhqMEwgixA3b/UC6DRn1mnu+jxbpT2IMJGxyW8+p3hNg jKdwaYnlpF+932Nq/OwI5FsvQtQgvv02SW30F3zXkeVvrEw6FahGiD7Vx8+1zQdCsPq6 AWzytXHZODDdyOR5dW7AFyY131L9qQzwPN+QzbGaXXppNsO4VGrdNGSnvs8rQq2Q4X/Y NzzQ== X-RZG-AUTH: ":O2kGeEG7b/pS1EW8QnKjhhg/vO4pzqdNytq77N6ZPUGwIpLOmsCM+BB/Jumg6xovrMWyi9ETLm+4n1smFcEOTm5t73dqDR7712k5dmGu88Y=" X-RZG-CLASS-ID: mo00 Received: from tonne.mhei.heimpold.itr by smtp.strato.de (RZmta 44.3 AUTH) with ESMTPSA id u0bfd3uA4DM2RXQ (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Sun, 4 Nov 2018 14:22:02 +0100 (CET) Received: from kerker.mhei.heimpold.itr (kerker.mhei.heimpold.itr [192.168.8.1]) by tonne.mhei.heimpold.itr (Postfix) with ESMTP id 0116415181F; Sun, 4 Nov 2018 14:22:01 +0100 (CET) From: Michael Heimpold To: linux-mmc@vger.kernel.org, chris@printf.net Cc: u.kleine-koenig@pengutronix.de, Michael Heimpold Subject: [PATCH] lsmmc: rework string trimming Date: Sun, 4 Nov 2018 14:21:49 +0100 Message-Id: <20181104132149.19215-1-mhei@heimpold.de> X-Mailer: git-send-email 2.17.1 Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP gcc8 complains with: lsmmc.c: In function 'read_file': lsmmc.c:356:3: error: 'strncpy' accessing 4096 bytes at \ offsets 0 and 1 overlaps 4095 bytes at offset 1 [-Werror=restrict] strncpy(&line[0], &line[1], sizeof(line)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The man page for strncpy() does not clearly state whether it's safe to use overlapping memory regions while copying; for the similar strcpy() this is prohibited, so I would also assume this for strncpy(). So let's refactor this part. Reported-by: Simon Tretter Signed-off-by: Michael Heimpold --- lsmmc.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lsmmc.c b/lsmmc.c index c4faa00..d352e74 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 *start, *end; char line[4096]; + int len; FILE *f; f = fopen(name, "r"); @@ -326,8 +327,8 @@ char *read_file(char *name) return NULL; } - preparsed = fgets(line, sizeof(line), f); - if (!preparsed) { + start = fgets(line, sizeof(line), f); + if (!start) { if (ferror(f)) fprintf(stderr, "Could not read MMC/SD file '%s'.\n", name); @@ -347,15 +348,19 @@ char *read_file(char *name) return NULL; } - line[sizeof(line) - 1] = '\0'; + /* skip leading white-space */ + while (isspace(*start)) + start++; - while (isspace(line[strlen(line) - 1])) - line[strlen(line) - 1] = '\0'; - - while (isspace(line[0])) - strncpy(&line[0], &line[1], sizeof(line)); + /* trim trailing white-space */ + len = strlen(start); + if (len) { + end = &start[len - 1]; + while (isspace(*end)) + *end-- = '\0'; + } - return strdup(line); + return strdup(start); } /* Hexadecimal string parsing functions */