From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964789AbcK3KWt (ORCPT ); Wed, 30 Nov 2016 05:22:49 -0500 Received: from mx2.suse.de ([195.135.220.15]:45386 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755693AbcK3KWf (ORCPT ); Wed, 30 Nov 2016 05:22:35 -0500 Date: Wed, 30 Nov 2016 11:22:31 +0100 From: Petr Mladek To: Michal Hocko Cc: Sebastian Duda , linux-kernel@i4.cs.fau.de, Tobias Baumeister , Andrew Morton , Sergey Senozhatsky , Borislav Petkov , Tejun Heo , Thierry Reding , open list Subject: Re: [PATCH] printk.c: removed unnecessary code Message-ID: <20161130102231.GE24060@pathway.suse.cz> References: <1480432743-8049-1-git-send-email-sebastian.duda@fau.de> <20161130091427.GB18437@dhcp22.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20161130091427.GB18437@dhcp22.suse.cz> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed 2016-11-30 10:14:28, Michal Hocko wrote: > [Resending with the full CC list as my email client has clobbered it in > the previous attempt for some reason] > > On Tue 29-11-16 16:19:01, Sebastian Duda wrote: > > snprintf((char *) ?, 0, ...); always returns Zero and doesn't change the data. > > Thus the execution of > > snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts); > > has no effect on program. > > The substitution with 0 increases the readability of the code. > > Are you sure this is correct. As per vsnprintf documentation: > " > * The return value is the number of characters which would > * be generated for the given input, excluding the trailing > * '\0', as per ISO C99. > " > > this should just work as 35dac27cedd1 ("printk: fix incorrect length > from print_time() when seconds > 99999") intended. > > I haven't checked the implementation though so I might be wrong here. print_time() actually is not used by printk() directly. Therefore the above reasoning does not apply. They key here is to look when the buf might be NULL. It is when we try to get an idea how much space will be needed for the message when it is printed to some output device, e.g. console or syslog. For example, there is the following chain of calls: syslog_print_all() msg_print_text(msg, prev, true, NULL, 0) print_prefix(msg, syslog, NULL) len += print_time(msg->ts_nsec, buf ? buf + len : NULL) It means that we really need to know the length of the printed string even when we do not write it to a buffer for the moment. By other words, the patch would break the functionality. > > > > Signed-off-by: Sebastian Duda > > Signed-off-by: Tobias Baumeister Nacked-by: Petr Mladek Best Regards, Petr