Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp551748pxk; Wed, 2 Sep 2020 08:34:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBT/B/tq6Bm8wqawS1evf+gJx7Toz2T/LMRqU4HtRgKSDQ6+2tgvumSEnbadXMohi/PrjX X-Received: by 2002:aa7:d8d8:: with SMTP id k24mr606688eds.97.1599060868753; Wed, 02 Sep 2020 08:34:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599060868; cv=none; d=google.com; s=arc-20160816; b=AIDq53BtjTZ3Zh9XknO5eoWeCT4JN0Z7yKb9FxV3IZtlPDVxzYJDX/B6Oqp+k2y5uX qsJlYqO7GMVEIJOi0WO4MJF8pMPVyRAFxWzqkgv2PjQ/dMSWabJmrcl88fgw5onx2ChF Ub20Fs3uHyQQq8a9BYHA5XdXo5C3aYizo0m6vgWEMi9/Vu8CsTUxrHNVE9QuPAGe1Iri OpdNWrlFPKV5IfPeGK3HyTK8cIaAFL8uPykr6TW7nPE87G3bqrIPBK9u3gqmHkYWv/Bm BZKxhyfV6te1NasYnQ4sUc9JVvn5Se37l1ahb/eG3FM9CvyfFphd69q9VEuuLSQpAH72 2OQA== 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:date:from :dkim-signature; bh=MRzTXOeTGeZAyaIQ1e/ex4zUgkS94k274JL7swyOwLU=; b=KPxW1suy0tn1Bj7LgH6nYW0g0dGCaGiAEnnp2pBf7uoLDjt7cEK5n+RbzK+r+2NFJe CYkNHlaLJTwn0wDoKmiitgF57m627lESuNHvrDWK8xIUJ4BFsc7eFTx5yqrrD1IScm6p Sh9AbISCZpOVmqIBwmN7FAdKQKgclUC4pYi40shWWtXmARfWwDgVMhAmK7NW3PYEpAKU I/BAiBrRsDvbnFancgOjbV5PPM0XuR8wSvNMeuDp8P1JyE+i9FEt1vpLCB1qynag9pBr 718MxEUjOkKPxOK9IJu6acxqFVRYiYR9f8Euqn1r5lOUSVbb6t0EzgVR9E/aK4pEvUei Y4zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=n3thLXKY; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si2807462eds.225.2020.09.02.08.34.05; Wed, 02 Sep 2020 08:34:28 -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=@gmail.com header.s=20161025 header.b=n3thLXKY; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728334AbgIBPbk (ORCPT + 99 others); Wed, 2 Sep 2020 11:31:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728324AbgIBPbT (ORCPT ); Wed, 2 Sep 2020 11:31:19 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3ECFC061244 for ; Wed, 2 Sep 2020 08:31:17 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id a9so5013853wmm.2 for ; Wed, 02 Sep 2020 08:31:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=MRzTXOeTGeZAyaIQ1e/ex4zUgkS94k274JL7swyOwLU=; b=n3thLXKYL8UCpmS6P7xcRU/DJXJUDnk8aYygxAO8urgHvALVBB7anUMPuOdfx6L6FL DFWTY6fdgsiThCKCKQ9BZhYT7je3gARNBiJl8e16I2zxO4YBTBqJwaW2Dpfd1PVlYlZ2 4gMUf2BVVo9Hq1PcccnAaoDzNhroAexNnhslqEnjnzMwXtYbtpzSXb7HjeM55GvzZlyB eiBKxf4vyYsliXxqPikXFOUpc5Nz88zpnUN3nH9di9BxBEP/9vrFY9ehkHQHhTFRLSlB iCwllHPuNAxqIWRZGLbsVg94r+4I8ywcyZbZLnpZdVgC3pCtFBmEETr9TgrIqog9K/AR LB3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=MRzTXOeTGeZAyaIQ1e/ex4zUgkS94k274JL7swyOwLU=; b=knUDTGMpZ97Oz/lLAyFnMa9ZFFZETRSjVwNdnXC6t3ytRJtts2c7TcFaeUQYCrYwvi QtncsgOZ0errmFjs8L1k+aq5Ul2kG+KZG6wW2xYlPy4oUiNwG7E4ubxaApUvGWUdV4W8 A6L0PLTFKoI/LYl5Mko+7ejMUaVtCOR11XF/xrgwuuKjevcIBsv5wY8a/cnuuISdB6Z6 TzlNTBXx77L+bzkWPDk+K47/tokyhvC8ClO06WTyiS3Y5uWVCp753NxMnCk628p73MOb dQVs4pIk0R3YCzwrdTdUDxCBcKJKsBuQliN/hPM8rBokEuVVIC6U/6haNoBtjvRIAFSq WWQw== X-Gm-Message-State: AOAM5336V/ozOyFLptw8aCBMHTA/7uPijIpKbTpudu6/8ClgdB9otKvz sy5wgJoP5F0p5W0Ck7uCGis= X-Received: by 2002:a1c:b60b:: with SMTP id g11mr1271474wmf.48.1599060676417; Wed, 02 Sep 2020 08:31:16 -0700 (PDT) Received: from medion (cpc83661-brig20-2-0-cust443.3-3.cable.virginm.net. [82.28.105.188]) by smtp.gmail.com with ESMTPSA id z9sm74349wma.4.2020.09.02.08.31.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Sep 2020 08:31:15 -0700 (PDT) From: Alex Dewar X-Google-Original-From: Alex Dewar Date: Wed, 2 Sep 2020 16:31:13 +0100 To: Greg Kroah-Hartman Cc: Alex Dewar , "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: <20200902153113.qxqkpm2o6a6pgoka@medion> References: <20200829233720.42640-1-alex.dewar90@gmail.com> <20200829233720.42640-3-alex.dewar90@gmail.com> <20200830091353.GA119062@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200830091353.GA119062@kroah.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 11:13:53AM +0200, Greg Kroah-Hartman wrote: > 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 tried it out, but maybe not thoroughly enough ;-) > > I like Joe's patches better, this feels like more work... Fair enough. As Joe's pointed out, even for single numbers the formatting is sometimes more complicated, so his approach does seem best. Thanks for looking though :-) > > thanks, > > greg k-h