Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp4469466rwb; Sat, 21 Jan 2023 12:37:55 -0800 (PST) X-Google-Smtp-Source: AMrXdXtF9PsX5XGbvWS9V8KBQcVeogx6j4MAX8gIX336tH2InzeBAskfUGRFsyR9fKSoJIXEMqoT X-Received: by 2002:a17:902:7009:b0:194:614d:44a2 with SMTP id y9-20020a170902700900b00194614d44a2mr18693100plk.51.1674333475100; Sat, 21 Jan 2023 12:37:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674333475; cv=none; d=google.com; s=arc-20160816; b=vvXbbebaF/tNOQviNIHj5zG3D1MOjfmOHR0NIZKA3ttk646qEJF11W0NBxhH9PyNut suo2BVb3KLdYGyv4NxJvDQtLxREnP0L7hRdEwMJkuhRldJpZZcnZV4iSzQCBuvctXrk3 1XotcL/FD33gS0LgL+XMVHnldAwWsGAVUI5bRSmefuc+rWzf/jpT6sdS1qSos7NuqXDo r96T52zxolIGCdTzm4etQNa0BpS34mHJ4glaRITt4/fTLJSbceYXqw0hfBlE5NF+vRkN f5D2576GlHqTvC1LlghRqFJd5dnleWGaEUpMdrMcLRYpWPWeB32B8QmSxQQrEV4++3a6 Je2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=vRTDSyLoD1V2PORZye+oXO9w2E1zyNf4GuarCAou+QI=; b=T8TaLkjb0NMnQ3mKPEPLFFasWumz6xnIDviKNJxSTAH118AMREmYOyeAJ8EozzrNP8 9J/fuT08zI3rUHrSXmaSQXELlgk2p52WO4P4XSEoV/dpKEHDh6o4ZV37tSWHxyo+9Xnj hHFTqFdTxhc3911FKnYiIqtcmhVCebaCS1XXtawvcJ440TJY+LF7QffuUxS2eOBdQNzO 2ojDyXyRFPHRuw5bNJFEjqek4+qn3mibwxI6MsM71eJFHGyDKf7At9U7tI7In+kyDQ2k +MQ9CMmW6AbeqAJhM536BebpBsoSDnuKVi0YE5JB8GzpiuSFN7uhX00hur5ZFOWuhNfT W3tQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="RN0PH/lP"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j14-20020a170903024e00b0016efde92292si1068282plh.255.2023.01.21.12.37.42; Sat, 21 Jan 2023 12:37:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="RN0PH/lP"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230080AbjAUUhJ (ORCPT + 99 others); Sat, 21 Jan 2023 15:37:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229989AbjAUUgt (ORCPT ); Sat, 21 Jan 2023 15:36:49 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEB252916C for ; Sat, 21 Jan 2023 12:36:44 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 73B4160B76 for ; Sat, 21 Jan 2023 20:36:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C25DC4339C for ; Sat, 21 Jan 2023 20:36:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674333404; bh=IF0tzK9qzRjNcfSVZjyoBUt3GW1r1KZLNwT9shnLSiw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=RN0PH/lPg6F1oUwkbqvwSa2X2ZeXvzvTJeWqmKoWTl+iVPDqxCf3xjD2sfzJMBd37 9TVE4z4BNCFYqRKrPRu6Ekyxi+mL46rr+tsdoFHQxyoJ2dcUseArpzuyQH8i4bURD7 8Eg5W63g1E6xLzfsZCMlAToSV7ozC989NRPlezL7qLeRnZcdge54Tlt+2OX1aRSAOQ Pu/Ku1cMzjqZLy9kJ40tRPfZl6TZJjV9HQQMcccDvs7hLsTaaLXh8vs3RwBDuVc3yP hRjZKa4aiJZOfN89czSi/hVEJtEYI05B1sSzswB/gxeszA/X6Jd0U62XiAsPRbd33L HwXUtbVQiXUIg== From: Eric Biggers To: linux-ext4@vger.kernel.org Subject: [PATCH 29/38] misc/e4defrag: fix -Wstringop-truncation warnings Date: Sat, 21 Jan 2023 12:32:21 -0800 Message-Id: <20230121203230.27624-30-ebiggers@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230121203230.27624-1-ebiggers@kernel.org> References: <20230121203230.27624-1-ebiggers@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Eric Biggers Fix two -Wstringop-truncation warnings in is_ext4() by simplifying how how mnt_type is handled and by using the correct bound for mnt_fsname. Fix a -Wstringop-truncation warning in main() by replacing the fragile pattern 'strncpy(dst, src, strnlen(src, N))', which doesn't null-terminate the destination string, with a standard string copy. (It happened to work anyway because dst happens to be zero-initialized.) These warnings showed up when building with -Wall with gcc 8 or later. Signed-off-by: Eric Biggers --- misc/e4defrag.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/misc/e4defrag.c b/misc/e4defrag.c index 9ec265f2e..33bd05d2c 100644 --- a/misc/e4defrag.c +++ b/misc/e4defrag.c @@ -258,12 +258,12 @@ static int get_mount_point(const char *devname, char *mount_point, * * @file: the file's name. */ -static int is_ext4(const char *file, char *devname) +static int is_ext4(const char *file, char devname[PATH_MAX + 1]) { int maxlen = 0; int len, ret; + int type_is_ext4 = 0; FILE *fp = NULL; - char *mnt_type = NULL; /* Refer to /etc/mtab */ const char *mtab = MOUNTED; char file_path[PATH_MAX + 1]; @@ -307,26 +307,16 @@ static int is_ext4(const char *file, char *devname) maxlen = len; - mnt_type = realloc(mnt_type, strlen(mnt->mnt_type) + 1); - if (mnt_type == NULL) { - endmntent(fp); - return -1; - } - memset(mnt_type, 0, strlen(mnt->mnt_type) + 1); - strncpy(mnt_type, mnt->mnt_type, strlen(mnt->mnt_type)); + type_is_ext4 = !strcmp(mnt->mnt_type, FS_EXT4); strncpy(lost_found_dir, mnt->mnt_dir, PATH_MAX); - strncpy(devname, mnt->mnt_fsname, strlen(mnt->mnt_fsname) + 1); + strncpy(devname, mnt->mnt_fsname, PATH_MAX); } endmntent(fp); - if (mnt_type && strcmp(mnt_type, FS_EXT4) == 0) { - FREE(mnt_type); + if (type_is_ext4) return 0; - } else { - FREE(mnt_type); - PRINT_ERR_MSG(NGMSG_EXT4); - return -1; - } + PRINT_ERR_MSG(NGMSG_EXT4); + return -1; } /* @@ -1865,11 +1855,9 @@ int main(int argc, char *argv[]) /* fall through */ case DEVNAME: if (arg_type == DEVNAME) { - strncpy(lost_found_dir, dir_name, - strnlen(dir_name, PATH_MAX)); + strcpy(lost_found_dir, dir_name); strncat(lost_found_dir, "/lost+found/", - PATH_MAX - strnlen(lost_found_dir, - PATH_MAX)); + PATH_MAX - strlen(lost_found_dir)); } nftw64(dir_name, calc_entry_counts, FTW_OPEN_FD, flags); -- 2.39.0