Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp71876pxk; Sat, 29 Aug 2020 16:41:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcLa3wwj0n6l0JbwUn8/OnvX7ViNUskRzQtiSRvlvKYhFZXlJoeBpxwqnei3laXc9MqU6Z X-Received: by 2002:a05:6402:297:: with SMTP id l23mr5355121edv.145.1598744462497; Sat, 29 Aug 2020 16:41:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598744462; cv=none; d=google.com; s=arc-20160816; b=TRTfn59uEs/aMQ1g325ISUvF8LNOocHE60CG7Ydxm7xsYDsvy59TQLWZbYD9ScU3+B ous/UvQV0hzNbfHQpad2dnr4zTEHE6xsHDwxyPjHPpGHTU2Eg/CBPGVf5bRWkNuFN8J5 ySHlXG52O1Z8oWrJBlHV19z5blwX627GUi+R515LWARzdCHA2OowIfkMuAoOqVOnwJ31 9opkoW4oY6LOkE/bYpLzh6UDbDJBr1xsn/RFe67NjguViIBBHIbz6mzwFh3M4bHiFDg1 +ADsdioOyolJrEOm2JDvzW6qWyqKmBEOIlMZzduBoQ27Jwl48rPny7Bg6J7u21hrMfTR y/ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=gR6earlvAFNRcUw4sVCxPtBxPfjjAjdl9voNOlT11FA=; b=dEhIiom0WMomlNURwncyVwQbpuISePc87LHJANprEUZBrzxHFu9W6eOggczp/K1SUC 3bcbZhpp/6d0UyRoSIy7ttuWLSGP/5iS5mPR9zFyXWJFNTOL0BYkMOfC0zESuWCLnzov 1QZPv20MfSdNNo67Jwcz8iwt4enYP+NNqky0/Os9S1msRoQ6jqW2E4hB2pLSr5F3m8XS O+otzj0+W2EETw2M00YqeLz3baUYCJdxFAGUWp30BVmdmDpQ84njL1v6NDuZjveQ/KhS sv7YFi2BWt+A/wcfD1qrGOkTEcG1uJzWFByFlyUyR4QVdoH5CKPpFh8A9EeASgBJ6sLN YlfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=L+AkTBXd; 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=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 u19si2542123edt.139.2020.08.29.16.40.40; Sat, 29 Aug 2020 16:41:02 -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=fail header.i=@gmail.com header.s=20161025 header.b=L+AkTBXd; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728583AbgH2Xil (ORCPT + 99 others); Sat, 29 Aug 2020 19:38:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726995AbgH2Xik (ORCPT ); Sat, 29 Aug 2020 19:38:40 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A60FC061573 for ; Sat, 29 Aug 2020 16:38:40 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id c18so2496902wrm.9 for ; Sat, 29 Aug 2020 16:38:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gR6earlvAFNRcUw4sVCxPtBxPfjjAjdl9voNOlT11FA=; b=L+AkTBXdg68ilCOINEw94OLD2BbFpHlWVbCBHYY8HmaQC6KMdJE0SJ6vpUSl9XZhx1 R55uW70znp/kJHxBwFViNW1wMswqRPA3TSkiyVBPE6pSmoPU1sHIpRLmeylxlAUS32nI bOn1KmCvm+vGTN8UuujhpxB+wLAMLwIRGRBqYuVEuzyvvwEuwFAOdUISG2Fhkl0nAAJ+ yam4mE3a9oo4OBhN63ZzgkCSpQ/pWNIukOwjXsiUYwMICEWTpqLokM8G9yzpSOU271DA V+RQPFrWg0Oqi7MJdOwjE9UPC9SDLNqrXOE2Amr/vchGkIT88objXHHT2MIuSPMv47Xz qjxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gR6earlvAFNRcUw4sVCxPtBxPfjjAjdl9voNOlT11FA=; b=tbQNgzKMwvjiWuOlRvkzaeKOtasj1PgAwsMQ0IhG/zYb8xoXH0e0PrZvExt0v6tE6U yHtH+toRe+uNi7xn0H4PD4zrQ1nlE+ShcaeMB+Pmb7WU1d7yIJBYYc62aI3GW0TS92gD rplvZyS4gw5YV2bv6ZpAffD5UTm5CHIUgSKJ3weIUZoV0Ev7Zs+kOmrw/jCzaOjqUrDT JJb1IOmvJaTNuZ8cVh3YJKqedqljvJr19848OPnsHbLYRnbYCx05FClzq30n7h7ztakJ FZvX49NQfoUbgl08rczkWzJViEvUy9d3D+6sjX8USx0v3Zo6n8G/W4nAN4v6DE2vJ/hQ /58A== X-Gm-Message-State: AOAM530DiONHRvgFQRJdJVZ+8MSAKerfLWCXODeQlqPyKSPp6CDQuf2B OPUgMGCMp3hmd17oYQcip3Q= X-Received: by 2002:adf:80c7:: with SMTP id 65mr5128865wrl.35.1598744319223; Sat, 29 Aug 2020 16:38:39 -0700 (PDT) Received: from localhost.localdomain (cpc83661-brig20-2-0-cust443.3-3.cable.virginm.net. [82.28.105.188]) by smtp.gmail.com with ESMTPSA id t13sm5253878wru.65.2020.08.29.16.38.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Aug 2020 16:38:38 -0700 (PDT) From: Alex Dewar Cc: Alex Dewar , Greg Kroah-Hartman , "Rafael J. Wysocki" , Christian Brauner , "David S. Miller" , Nayna Jain , Dan Williams , Mauro Carvalho Chehab , Sourabh Jain , linux-kernel@vger.kernel.org Subject: [PATCH RFC 2/2] sysfs: add helper macro for showing simple integer values Date: Sun, 30 Aug 2020 00:37:17 +0100 Message-Id: <20200829233720.42640-3-alex.dewar90@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200829233720.42640-1-alex.dewar90@gmail.com> References: <20200829233720.42640-1-alex.dewar90@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(+) diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 26e7d9f69dfd..763316788153 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -197,6 +197,37 @@ sysfs_strscpy((dest), (src), \ ) \ ) +/** + * sysfs_sprinti - emit an integer-type value from a sysfs show method + * @buf: destination buffer + * @x: the variable whose value is to be shown + * + * The appropriate format is passed to sprintf() according to the type of + * x, preventing accidental misuse of format strings. + */ +#define sysfs_sprinti(buf, x) \ +({ \ + BUILD_BUG_ON(!__builtin_types_compatible_p(typeof(x), unsigned int) && \ + !__builtin_types_compatible_p(typeof(x), unsigned long) && \ + !__builtin_types_compatible_p(typeof(x), unsigned long long) && \ + !__builtin_types_compatible_p(typeof(x), int) && \ + !__builtin_types_compatible_p(typeof(x), short) && \ + !__builtin_types_compatible_p(typeof(x), unsigned short)); \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(typeof(x), unsigned int), \ + sprintf(buf, "%u\n", (unsigned int)(x)), \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(typeof(x), unsigned long), \ + sprintf(buf, "%lu\n", (unsigned long)(x)), \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(typeof(x), unsigned long long), \ + sprintf(buf, "%llu\n", (unsigned long long)(x)), \ + sprintf(buf, "%d\n", (int)(x)) \ + ) \ + ) \ + ); \ +}) + struct file; struct vm_area_struct; -- 2.28.0