Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp573878lqh; Thu, 28 Mar 2024 09:50:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXfHeyJtlvU9OgQwYSs/L32gVuJDn2Q64qNVaPMTIMVrSXOYtQjzIaIY3+ZVHdq+7RSUkQXNuucIlFhzoiD8GRLGsGrWgZRlBpPGYAAHw== X-Google-Smtp-Source: AGHT+IHnEi7y6fdZOUfIYeXDJf2qHr3cb+jIpd71KO9GoNktlVs4gr/H4xY1iyMqAfhY7urtFMc1 X-Received: by 2002:a05:6808:128e:b0:3c3:e436:881f with SMTP id a14-20020a056808128e00b003c3e436881fmr4045209oiw.36.1711644629775; Thu, 28 Mar 2024 09:50:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711644629; cv=pass; d=google.com; s=arc-20160816; b=yyNDRGnSB0gIs0/w3Meamp2hYF9rqd0kPnWdqnWw7D7XHS4Ci+XTgDfRrV6vQ9Oz00 C2sKdUL2FgpchKIAzeY5J+TMnR1bDT8qfOoOwLpSq1Wkvq+sNA3X+c7cMxV7JPHCj5Pe oidbW79hk1UhM73w7KMeTuOrCMAhJOA9pkDQ6LDKu5l9xFSv5cKY2qk7VEqaJfsxW6Pd jWcLtNTPn8A3ZrImB2C+NKSWzWz0iUiBJtfF+4mpuYR0LszQOqj/pRVyETRLiWvgHwuv upcSToDfE2VBJDcSHpaEA5qO9Ra+jR62IQ4unUsizEBnOZwTcaenb5BSDXa9xsktP1GZ tQVw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=/CRDH2chuqf02kQLgOdJTq8keMEI7k1Ir0dHZtq22A0=; fh=0y3sCPcuatUNQr9RtuJCjlYU5oD6tobUgsKsxkGFXwc=; b=sIDfzl1HhmjtjX1fT18hpmjj258SuGRZtoUbMhTOWnCOOed6JH2SnxEiZQl7Ie/dCi 6duLLlyV5aspDl4P4/+yk0Fi94rVQhhG+sorFRg57km7+WxHqhfCQM5W9ECJVOFyRFoQ woLZGs+K8/alvq0DTxIko4B0mGWVdaQIZjXPVS2aC48vT0LOV4ppUDnpVatkotttnKgR FicXbMXF7/gB4SV8m00wyUP+9CdxB0zkB1b/dc7U5VB7EGLKO0U+B1gOPg0q0tYpSEeg oOKXohCO0150eJlNl9q/6hQ+1txBTxzA4TQUyDIBRuLRBSP6ysUqO8PrcEMz/o7usNWd 0sjQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@acm.org header.s=mr01 header.b="GF/LGX26"; arc=pass (i=1 spf=pass spfdomain=acm.org dkim=pass dkdomain=acm.org dmarc=pass fromdomain=acm.org); spf=pass (google.com: domain of linux-kernel+bounces-123299-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123299-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=acm.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id s7-20020a05622a178700b00431757d8669si1736777qtk.36.2024.03.28.09.50.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 09:50:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123299-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=@acm.org header.s=mr01 header.b="GF/LGX26"; arc=pass (i=1 spf=pass spfdomain=acm.org dkim=pass dkdomain=acm.org dmarc=pass fromdomain=acm.org); spf=pass (google.com: domain of linux-kernel+bounces-123299-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123299-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=acm.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 7C15F1C30870 for ; Thu, 28 Mar 2024 16:50:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 25E69131E51; Thu, 28 Mar 2024 16:47:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b="GF/LGX26" Received: from 008.lax.mailroute.net (008.lax.mailroute.net [199.89.1.11]) (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 C6EDF2C6AD; Thu, 28 Mar 2024 16:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=199.89.1.11 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711644435; cv=none; b=hMiDjZBLZFh3MYxV4mPNUREzxmuNGuOihaws8LcR95PuT9lT7+xT41A+9tGCaNIuPWYWq+Soq1mOi78rUREhGjqQE3hgdxUiL/nddSYcLX+CNXnI53oM8HVmeWzC4EDyrLe7OC48iXCWCg9I4GQxi0uDFUtu4108pxQsyR0OtZY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711644435; c=relaxed/simple; bh=zYBptg0Q4ids4Qp/xFxn+pDwZGbc5lhp6j/rsmPcXwk=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Tgw3OFZkdq9uwsLm9CduPcFKFJ6YHmhzlM6QHqcmJUIAbJRjrimcXEfKrXplT4UC5baAYO4xFDMQmfom166F+DouHmg54zIceQieURdU5qOG8i8RnoUZ4bghKg31a2IVLmWTrVD0OzBiVJd1r9TdsaHm+yeUxmGbPyzgOoUkifs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b=GF/LGX26; arc=none smtp.client-ip=199.89.1.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Received: from localhost (localhost [127.0.0.1]) by 008.lax.mailroute.net (Postfix) with ESMTP id 4V58Zy5wtlz6Cnk8t; Thu, 28 Mar 2024 16:47:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:content-type:content-type:in-reply-to :from:from:references:content-language:subject:subject :user-agent:mime-version:date:date:message-id:received:received; s=mr01; t=1711644423; x=1714236424; bh=/CRDH2chuqf02kQLgOdJTq8k eMEI7k1Ir0dHZtq22A0=; b=GF/LGX26GsWodplyf+K20fwnLrdN8nvelpJmkcXl bU7JBclDhPB5Y/x9kk67Z+M0A+fW6YaQI0WqUn44TyCApwy4dw/oFVFFXF7jNpWa MnN9LRPM/vrEBtTWn734zCzjqdW+PQ4PyEvVLRWmc3Eo/GmffaYx+PHqaMgr09XI /FwAJqOnBI8Gjw3JV6jZhLGisluYTVhpgwHQRj+fs0tVY+HnKbizWMpzhCuZ1RN4 AYNfV1v2eu26gpXbiv1oVJGleUltd+Gw/poNIr8vwYwJV1clM31tY/uy3noMHyUv VkduUjI4u2oCWoq8TGR592QFzwz+OcRL9u/QQ0rLHhi0jw== X-Virus-Scanned: by MailRoute Received: from 008.lax.mailroute.net ([127.0.0.1]) by localhost (008.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id XfpEE5bIkp4V; Thu, 28 Mar 2024 16:47:03 +0000 (UTC) Received: from [100.96.154.173] (unknown [104.132.1.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 008.lax.mailroute.net (Postfix) with ESMTPSA id 4V58Zs2P7Cz6Cnk8m; Thu, 28 Mar 2024 16:47:00 +0000 (UTC) Message-ID: <73de2cd5-9770-453c-b002-7cd561bdbc7b@acm.org> Date: Thu, 28 Mar 2024 09:46:58 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 02/11] scsi: devinfo: rework scsi_strcpy_devinfo() Content-Language: en-US To: Arnd Bergmann , linux-kernel@vger.kernel.org, "James E.J. Bottomley" , "Martin K. Petersen" Cc: Arnd Bergmann , Chris Down , Petr Mladek , linux-scsi@vger.kernel.org References: <20240328140512.4148825-1-arnd@kernel.org> <20240328140512.4148825-3-arnd@kernel.org> From: Bart Van Assche In-Reply-To: <20240328140512.4148825-3-arnd@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 3/28/24 07:04, Arnd Bergmann wrote: > 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); > } Please eliminate the variable 'ret'. I think that will improve readability of the new code. Thanks, Bart.