Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp260509pxk; Sun, 30 Aug 2020 02:15:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJRWlDqe3fbR6w/FCOtXJ8h6JB5u/ZiDV3WwZ4rwxzt4GKGrxbjw4QpHgnba0vtN9/mal3 X-Received: by 2002:a17:906:fa01:: with SMTP id lo1mr6471128ejb.394.1598778925215; Sun, 30 Aug 2020 02:15:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598778925; cv=none; d=google.com; s=arc-20160816; b=plN0xCpsTAbey/s+/mV2pd2DRmALqbFvamijZy5fNTqFM1bz6mbk3LrYJVdfZGenHg kMT6qJdrRa1IYm337a4YgQOkl8D3xNxSl6aoY+bSCQK8BTIMS44CIU+ojZJ9cgKV9l8N q27d78EVU61Pj5w0GJyKb4/RCr1HY0CkpkSqUx5RnRAYyl0VTk5NujjEIqYWq8QIZumO h+8U3SmWUSnRFXm3fH6RPnPrNrW+1Lvyw3HOQw2iqPyhnONol5Ay01NsHZ5yQT+1q1yU BfgIyCFkdLxcyjC20Ih3NBWWobHTNM6yNzRE3kAeY2t1X1L4uLRbt9EoYbamzxm3xn8u d+Mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=aXq6hVDUlu8q20HcmHMHfNysh2ef7pj6wO3e8eHZm2I=; b=WUdu8XJsk0Vcv3PNGXfyylJqF2R08vHrj5vui8kjq7n++Sr4pCofqIXCKprpWT4x3H i34EpjslFJk7iPwcKhcb/i1lq9t7vW0c79UBNxYf37yDypEAn0fAyfCeCFu2nU8J+n0z ++xXKiu3qWoWVuGO+wLyMcwrldTMHxK1G8qMSgQrIRKEstm1WXbA3LZNsfp8Nrj/+SfG PzS36v2lmipbz+VdTGyeOJ3gGvESViJB8nLRidddFdLGfhgeLdQeQCA9ne1qhw97xgrG y5kuAXUiDwdIuXQa+F4AexWufcF65y+gTPnBhlDuc7Tn9SzXn3KGi+GRgpNBKlC7BEZB D7fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Vt3ZG0nC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t26si3239769ejb.695.2020.08.30.02.15.01; Sun, 30 Aug 2020 02:15:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Vt3ZG0nC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728727AbgH3JN7 (ORCPT + 99 others); Sun, 30 Aug 2020 05:13:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:33820 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725869AbgH3JN4 (ORCPT ); Sun, 30 Aug 2020 05:13:56 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A06EB207BB; Sun, 30 Aug 2020 09:13:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598778836; bh=xQzXOcqDtmUYQq7g3pMECZEmtAKxdxMbgh4yLXmTE70=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Vt3ZG0nCWhS8rihVO00dchU6iwISPLq1crtaRf26NVt8zoKFc+n4ey8kMT9cDRKSL 13qmHL/JJYtwn71WWmZqQJODP24wz+yz45+atDy5VHYlzSmZdf5dRbB16fIZFeT34i iS2B5y5cFTSWuNWBwhCHM3JeVmfj65GLRlAYvAA0= Date: Sun, 30 Aug 2020 11:13:53 +0200 From: Greg Kroah-Hartman To: Alex Dewar Cc: "Rafael J. Wysocki" , Christian Brauner , "David S. Miller" , Nayna Jain , Dan Williams , Mauro Carvalho Chehab , Sourabh Jain , linux-kernel@vger.kernel.org Subject: Re: [PATCH RFC 2/2] sysfs: add helper macro for showing simple integer values Message-ID: <20200830091353.GA119062@kroah.com> References: <20200829233720.42640-1-alex.dewar90@gmail.com> <20200829233720.42640-3-alex.dewar90@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200829233720.42640-3-alex.dewar90@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Aug 30, 2020 at 12:37:17AM +0100, Alex Dewar wrote: > sysfs attributes are supposed to be only single values, which are > printed into a buffer of PAGE_SIZE. Accordingly, for many simple > attributes, sprintf() can be used like so: > static ssize_t my_show(..., char *buf) > { > ... > return sprintf("%d\n", my_integer); > } > > The problem is that whilst this use of sprintf() is memory safe, other > cases where e.g. a possibly unterminated string is passed as input, are > not and so use of sprintf() here might make it more difficult to > identify these problematic cases. > > Define a macro, sysfs_sprinti(), which outputs the value of a single > integer to a buffer (with terminating "\n\0") and returns the size written. > This way, we can convert over the some of the trivially correct users of > sprintf() and decrease its usage in the kernel source tree. > > Another advantage of this approach is that we can now statically check > the type of the integer so that e.g. an unsigned long long will be > formatted as %llu. This will fix cases where the wrong format string has > been passed to sprintf(). > > Signed-off-by: Alex Dewar > --- > include/linux/sysfs.h | 31 +++++++++++++++++++++++++++++++ > 1 file changed, 31 insertions(+) Did you try this out? Don't you need to return the number of bytes written? I like Joe's patches better, this feels like more work... thanks, greg k-h