From patchwork Tue Nov 24 13:51:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 548081 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-wm0-x23a.google.com (mail-wm0-x23a.google.com [IPv6:2a00:1450:400c:c09::23a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 07ECF14031E for ; Wed, 25 Nov 2015 00:51:39 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b=iqjRh3vF; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b=jK7oAR1t; dkim-atps=neutral Received: by wmuu63 with SMTP id u63sf6641413wmu.0 for ; Tue, 24 Nov 2015 05:51:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :reply-to:content-type:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe; bh=WzRWzhCPtkRXquJHJLG0RQu13YK06uLHFvjl+uwVa9Y=; b=iqjRh3vFW+RsDf+JtZ7Ug9eOB6kjrAklKvsb14rmKPAtbKhNql13QFrnu45wyODWHh yEY88H8QlcpfebhBcrk/GiwC/+4huNq0JOxhzXivAM3x59C2kdU2a4VI/JFCbTGarRRU U3DeDcDCyMvDO54k0ySnHNorsYG4Y0zJEUpgb1iEt7v2sVm/FZPHNShC7QMZPWoX0Bi4 Vj4eWRIJYArv+wP3pSaacQAN6s7fBasE97pasi/dcARZnDXgzBjaU+ZgidRyPMfiKgbg PVOB2DxYYchFnwTzxjT8kOkZUOUJZ2lUZEuJedkM7rZENqqCHYmvsAA9ZwJX3yxS7t4G xnQw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :reply-to:content-type:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe; bh=WzRWzhCPtkRXquJHJLG0RQu13YK06uLHFvjl+uwVa9Y=; b=jK7oAR1trvhEND1cKYX8x2HEj/ga8+JkIbVbMN6oQNceOL7Pipx9uWvwZnJmRz4JiF 134ihiWtdTaH5wtDRujONt1NbpoTsNVSSAv5AlMEF+NBrFV2TupCivHA6V1fVKjkR2aG Rj9kJNFYgyv8X54KnAqIvbGPBdMrqOL2MbYKQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :in-reply-to:references:x-original-sender :x-original-authentication-results:reply-to:content-type:precedence :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help :list-archive:sender:list-subscribe:list-unsubscribe; bh=WzRWzhCPtkRXquJHJLG0RQu13YK06uLHFvjl+uwVa9Y=; b=JOe2oqOAsAf2c1KSRYdCs757OMEbNz0aYelqBrzNH4Qruy8eGeca84Nt/J2d4cIyfc SLsHsPFDiC7pzGCvtQMayal1ei56oe5TSR601ca0T7MWTMFl2BQ/RnzOJvkV1vOvY7tT XxlgipjYzY5Hucpnl5CMufd0DEE1+vIKWkdW1+Hg61kQZMBUUMXEaGR3HITLsFiL5loI 9G5JrnN/NkinY57NYjYspvTKuBbJREF3SCphGx1AQstfQ2U/ago0xlEgz81bgM1OqZJj AceHpqnJ2UHWamTOCuNg4jE+Kd44qVhIbBnGUDckr1apd9geY/zyuGrVBuIX4d8FVMQW Aybw== X-Received: by 10.28.85.132 with SMTP id j126mr31233wmb.3.1448373095475; Tue, 24 Nov 2015 05:51:35 -0800 (PST) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.28.87.15 with SMTP id l15ls593098wmb.52.canary; Tue, 24 Nov 2015 05:51:35 -0800 (PST) X-Received: by 10.28.63.23 with SMTP id m23mr4356253wma.0.1448373095130; Tue, 24 Nov 2015 05:51:35 -0800 (PST) Received: from mail-lf0-x22e.google.com (mail-lf0-x22e.google.com. [2a00:1450:4010:c07::22e]) by gmr-mx.google.com with ESMTPS id ea7si499429lbc.0.2015.11.24.05.51.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Nov 2015 05:51:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux@rasmusvillemoes.dk designates 2a00:1450:4010:c07::22e as permitted sender) client-ip=2a00:1450:4010:c07::22e; Received: by mail-lf0-x22e.google.com with SMTP id u14so20887540lff.1 for ; Tue, 24 Nov 2015 05:51:35 -0800 (PST) X-Gm-Message-State: ALoCoQkGQnQ3f3ip5bndWPS7XRNP57ip4ef5xo1tjYmd+tIF6CjByajUpyZHqglwEfhUZ52BUN/l X-Received: by 10.112.247.103 with SMTP id yd7mr13187103lbc.46.1448373095006; Tue, 24 Nov 2015 05:51:35 -0800 (PST) Received: from garcia.imf.au.dk ([130.225.20.51]) by smtp.gmail.com with ESMTPSA id j189sm2650975lfg.46.2015.11.24.05.51.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 24 Nov 2015 05:51:34 -0800 (PST) From: Rasmus Villemoes To: Joshua Kinard , Alessandro Zummo , Alexandre Belloni Cc: Rasmus Villemoes , rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org Subject: [rtc-linux] [PATCH 2/2] drivers/rtc/rtc-ds1685.c: don't try to micromanage sysfs output size Date: Tue, 24 Nov 2015 14:51:24 +0100 Message-Id: <1448373085-6353-2-git-send-email-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.6.1 In-Reply-To: <1448373085-6353-1-git-send-email-linux@rasmusvillemoes.dk> References: <1448373085-6353-1-git-send-email-linux@rasmusvillemoes.dk> X-Original-Sender: linux@rasmusvillemoes.dk X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of linux@rasmusvillemoes.dk designates 2a00:1450:4010:c07::22e as permitted sender) smtp.mailfrom=linux@rasmusvillemoes.dk Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: rtc-linux@googlegroups.com X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , ...and don't do it wrong. "not ok or N/A" has length 13. Add the trailing newline, and the snprintf return value will be 14. However, we lied to snprintf and told it that only 13 bytes were available. Hence snprintf has only written "not ok or N/" and a trailing '\0' to the buffer. Next we continue lying, this time to the upper sysfs layer, claiming that we wrote 14 meaningful bytes to the buffer. That'll make the upper layer copy "not ok or N/" plus two nul bytes to user space (one nul byte from snprintf, the other since sysfs takes care to clear the buffer before giving it to the ->show method). In the other cases, the claimed buffer size is closer to sufficient, but we'll still get a nul byte instead of a newline written to user space. There's absolutely no reason to try to predict the output size, and there's plenty of room in the buffer, so just use sprintf. Signed-off-by: Rasmus Villemoes --- drivers/rtc/rtc-ds1685.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/rtc/rtc-ds1685.c b/drivers/rtc/rtc-ds1685.c index 5038122aa8de..535050fc5e9f 100644 --- a/drivers/rtc/rtc-ds1685.c +++ b/drivers/rtc/rtc-ds1685.c @@ -1114,7 +1114,7 @@ ds1685_rtc_sysfs_battery_show(struct device *dev, ctrld = rtc->read(rtc, RTC_CTRL_D); - return snprintf(buf, 13, "%s\n", + return sprintf(buf, "%s\n", (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A"); } static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL); @@ -1137,7 +1137,7 @@ ds1685_rtc_sysfs_auxbatt_show(struct device *dev, ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); ds1685_rtc_switch_to_bank0(rtc); - return snprintf(buf, 13, "%s\n", + return sprintf(buf, "%s\n", (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A"); } static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL); @@ -1160,9 +1160,7 @@ ds1685_rtc_sysfs_serial_show(struct device *dev, ds1685_rtc_get_ssn(rtc, ssn); ds1685_rtc_switch_to_bank0(rtc); - return snprintf(buf, 24, "%8phC\n", ssn); - - return 0; + return sprintf(buf, "%8phC\n", ssn); } static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL); @@ -1285,7 +1283,7 @@ ds1685_rtc_sysfs_ctrl_regs_show(struct device *dev, tmp = rtc->read(rtc, reg_info->reg) & reg_info->bit; ds1685_rtc_switch_to_bank0(rtc); - return snprintf(buf, 2, "%d\n", (tmp ? 1 : 0)); + return sprintf(buf, "%d\n", (tmp ? 1 : 0)); } /** @@ -1621,7 +1619,7 @@ ds1685_rtc_sysfs_time_regs_show(struct device *dev, tmp = ds1685_rtc_bcd2bin(rtc, tmp, bcd_reg_info->mask, bin_reg_info->mask); - return snprintf(buf, 4, "%d\n", tmp); + return sprintf(buf, "%d\n", tmp); } /**