From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754860AbbK3TGb (ORCPT ); Mon, 30 Nov 2015 14:06:31 -0500 Received: from down.free-electrons.com ([37.187.137.238]:45622 "EHLO mail.free-electrons.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753471AbbK3TG2 (ORCPT ); Mon, 30 Nov 2015 14:06:28 -0500 Date: Mon, 30 Nov 2015 20:06:27 +0100 From: Alexandre Belloni To: Rasmus Villemoes Cc: Joshua Kinard , Alessandro Zummo , rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] drivers/rtc/rtc-ds1685.c: don't try to micromanage sysfs output size Message-ID: <20151130190627.GO22136@piout.net> References: <1448373085-6353-1-git-send-email-linux@rasmusvillemoes.dk> <1448373085-6353-2-git-send-email-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1448373085-6353-2-git-send-email-linux@rasmusvillemoes.dk> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 24/11/2015 at 14:51:24 +0100, Rasmus Villemoes wrote : > ...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(-) > Applied, thanks. -- Alexandre Belloni, Free Electrons Embedded Linux, Kernel and Android engineering http://free-electrons.com