Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp456468lqh; Thu, 28 Mar 2024 07:06:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXQTxeKS688dkr93wI9POyx9lUo8eUcjwb/rioos2cqn/8GwEwiRJhJp71ArZCh6yrFMO0+qbaYvPeeDAFkohPjhSkFKGZRm9cxWHXwKw== X-Google-Smtp-Source: AGHT+IEjIHX21K+iEUTZgCfS5ekXu1k0H+7zAuiCBMxYGIVvdmz37QMwu962/co0GRjGJUGn8GzU X-Received: by 2002:a05:620a:3908:b0:78a:74a2:5009 with SMTP id qr8-20020a05620a390800b0078a74a25009mr3455622qkn.9.1711634783531; Thu, 28 Mar 2024 07:06:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711634783; cv=pass; d=google.com; s=arc-20160816; b=TsSJhWWFAWJniPKOUrCUcwjVzCgYlxVWqXJdOR0UQgTuVyYPuFVb3BDdvOxFfo0CyF BTLFjJpX4FK6GfIwiIq5dOsE8CuAMw8QhvL8zMCzZ40jMi+JihKTc6dhai4NmVFwtmUf b1f04DU6x+9IejFnjykSRABEW5c3M7AziJ9GMW/rxjO3wMRA2xTa9KLkMOQXiu9PWiBu J+bdgpc7AusVfPwh6YgSafabmtoqrnN1W6KCAzzN8bTny/9UCqvsquOW0FCTkwv8Bz7N qM6hgNj+cfYFKNUzJS3t3RiBtPMCvZOPkO6MByYHEggImNddRXWtpNyUUny7J4uwk2hn fp2A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=jxb1FfrNSahIMNlH7WEl6wK7AGswgUALG6BfXqSdigs=; fh=YUCsO5SByNZQUTq/cwLmyj+n6Feue6BSpfLO8alZGLY=; b=jmaW26AlkjevlIQUk9X1ju4uTH+00b+dgEWo+Aa+p1oX7YVbhCrukPqyRXWySeEhq2 lKUXKPFMq7HAnFyerhD4O/DR/Zz8LMmkBoBZr43J2bXxfc6K7onLbhVpMK4/MlrWW2oZ Q6yWztgftLOF/ME4srew7U2mIwpSXtezkH4xMf2JGlTkbmglA8gxV6TIRTgnHVN0JqL8 5X3ejEzgxibT0CdF25vpDWzbzBCG69BX4y+3/r3Op8aqwpswpSJ+LB3o245e8+9br9zZ me+05rNhT3tjbcSmifSyvRz/s0AQ3JBcPmUNR0DKX2N0aimjkyfPqUAUeK8sLvzmWMu4 x17A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=V8I3yIcn; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-123017-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123017-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id da49-20020a05620a363100b0078a00267a7esi1446037qkb.482.2024.03.28.07.06.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 07:06:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123017-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=V8I3yIcn; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-123017-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123017-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 44C1C1C27DBB for ; Thu, 28 Mar 2024 14:06:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 63FB783CC7; Thu, 28 Mar 2024 14:05:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="V8I3yIcn" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 897D382880; Thu, 28 Mar 2024 14:05:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711634746; cv=none; b=pN1rcF6129xR3vkULVnwu8ddyxHrRVphYLXirm1bDch+e/VPlmi2CIeshOxQHxPquAzDBGNBSHtb9wSn2/ZlPo3Bq2v1aQtHZiT4Nnkvwra9QPiqAds6Z74BQYaGw2vzAb/ikOjj5ACK9Fyv02IDrBgCxaSPJuNwil3p75AzwPA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711634746; c=relaxed/simple; bh=zNvaXNiAxujiBLsJut5bqHVAOcViyLttpUXYqlyY1zw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YnUY5BWxDwW6pJVerghyXcLb40NJP6OI/IaHQRER1Szhy3KYM93sAZRX4lr54kO6DEun1NP0+xpxF/iqZsSFDd1xnDyPlKOxngrcW2A+/55okVRzBe8e1WHIDq6np5dZ79tzA06lrPcjM5sEwlTjNrsmi5z8wFN3QDq2UGijxYQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=V8I3yIcn; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E7EBC43399; Thu, 28 Mar 2024 14:05:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711634746; bh=zNvaXNiAxujiBLsJut5bqHVAOcViyLttpUXYqlyY1zw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V8I3yIcnVZWurSiHgMev/PaIZPsXUsNqVQKwHJ7+mgOKCcTPUXHUqEknMBntcAITu lBy02fiZlzDY1HrIVoQ6ESWZpX1lmaA3l6htVPoAbw69TmEc+E9473jJ6QlohLlyqS UvnWqh6+PSPbvVAseM27BsB1r34XDSYPszmmLgTuVSzOnsTLT0YhDQcHTrBtUGsHQz x5iqy1CFrKZrcmoPc07sHv5F+4iVgPeDzXgUiw2CpeHLueJ835FfTGSTEMJzTp8nqF H6czJFBuYYnPeGiMFd3KYXKRYt8+RywOmtLP0YP1mcjwGDZT8kfs3lYxjMUscxR1q+ tDvHoVJ3e2TIQ== From: Arnd Bergmann To: linux-kernel@vger.kernel.org, "James E.J. Bottomley" , "Martin K. Petersen" Cc: Arnd Bergmann , Chris Down , Petr Mladek , Bart Van Assche , linux-scsi@vger.kernel.org Subject: [PATCH 02/11] scsi: devinfo: rework scsi_strcpy_devinfo() Date: Thu, 28 Mar 2024 15:04:46 +0100 Message-Id: <20240328140512.4148825-3-arnd@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240328140512.4148825-1-arnd@kernel.org> References: <20240328140512.4148825-1-arnd@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Arnd Bergmann scsi_strcpy_devinfo() appears to work as intended but its semantics are so confusing that gcc warns about it when -Wstringop-truncation is enabled: In function 'scsi_strcpy_devinfo', inlined from 'scsi_dev_info_list_add_keyed' at drivers/scsi/scsi_devinfo.c:370:2: drivers/scsi/scsi_devinfo.c:297:9: error: 'strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation] 297 | strncpy(to, from, to_length); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ Reorganize the function to completely separate the nul-terminated from the space-padded/non-terminated case. The former is just strscpy_pad(), while the latter does not have a standard function. Signed-off-by: Arnd Bergmann --- drivers/scsi/scsi_devinfo.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c index ba7237e83863..58726c15ebac 100644 --- a/drivers/scsi/scsi_devinfo.c +++ b/drivers/scsi/scsi_devinfo.c @@ -290,18 +290,28 @@ static struct scsi_dev_info_list_table *scsi_devinfo_lookup_by_key(int key) static void scsi_strcpy_devinfo(char *name, char *to, size_t to_length, char *from, int compatible) { - size_t from_length; + int ret; - from_length = strlen(from); - /* This zero-pads the destination */ - strncpy(to, from, to_length); - if (from_length < to_length && !compatible) { - /* - * space pad the string if it is short. - */ - memset(&to[from_length], ' ', to_length - from_length); + if (compatible) { + /* This zero-pads and nul-terminates the destination */ + ret = strscpy_pad(to, from, to_length); + } else { + /* no nul-termination but space-padding for short strings */ + size_t from_length = strlen(from); + ret = from_length; + + if (from_length > to_length) { + from_length = to_length; + ret = -E2BIG; + } + + memcpy(to, from, from_length); + + if (from_length < to_length) + memset(&to[from_length], ' ', to_length - from_length); } - if (from_length > to_length) + + if (ret < 0) printk(KERN_WARNING "%s: %s string '%s' is too long\n", __func__, name, from); } -- 2.39.2