From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_2 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58CE8C4338F for ; Fri, 20 Aug 2021 14:25:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40D2E61101 for ; Fri, 20 Aug 2021 14:25:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240878AbhHTOZj (ORCPT ); Fri, 20 Aug 2021 10:25:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:37616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240802AbhHTOZh (ORCPT ); Fri, 20 Aug 2021 10:25:37 -0400 Received: from oasis.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 55B5E610FF; Fri, 20 Aug 2021 14:24:59 +0000 (UTC) Date: Fri, 20 Aug 2021 10:24:52 -0400 From: Steven Rostedt To: Cc: , , , , , Subject: Re: [PATCH linux-next] tools/tracing: fix application of sizeof to pointer Message-ID: <20210820102452.341d25f8@oasis.local.home> In-Reply-To: References: <8fd4bb65ef3da67feac9ce3258cdbe9824752cf1.1629198502.git.jing.yangyang@zte.com.cn> X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 20 Aug 2021 09:00:09 +0000 wrote: > > Reported-by: Zeal Robot > > Signed-off-by: jing yangyang > > --- > > tools/tracing/latency/latency-collector.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/tools/tracing/latency/latency-collector.c > > b/tools/tracing/latency/latency-collector.c > > index 3a2e6bb..64d531d 100644 > > --- a/tools/tracing/latency/latency-collector.c > > +++ b/tools/tracing/latency/latency-collector.c > > @@ -1538,7 +1538,7 @@ static void tracing_loop(void) > > mutex_lock(&print_mtx); > > check_signals(); > > write_or_die(fd_stdout, queue_full_warning, > > - sizeof(queue_full_warning)); > > + sizeof(*queue_full_warning)); > > The old code would give a size of 8, i.e. the size of the pointer. Your > suggestion will give a size of 1, i.e. the size of the first character of the > error message. So instead of ouputing "Could no" we would only write out "C". Which is obviously incorrect to use sizeof(*queue_full_warning), and just makes the current bug into an even worse bug. > > What we want is the length of the error message. This could be achieved in two > ways: > > 1. By changing the sizeof(queue_full_warning) to strlen(queue_full_warning). > > 2. By changing the definition of queue_full_warning to be an array, in that case > we would like to use sizeof(queue_full_warning) - 1, the "- 1" comes from the > fact that we don't want to write out the terminating null character. > > I think the first approach with strlen() is the better solution because it's > shorter and modern compilers will do the strlen() calculation of constant > strings at compile time anyway. Either approach is fine. But it needs to fix the issue, and not just blindly follow what Coccinelle tells you. Tools like Coccinelle can help point you where a problem is. But people still need to use their brain to actually fix the issue. -- Steve