From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A568F3858401 for ; Tue, 11 Oct 2022 14:22:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A568F3858401 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-344-4fEEWLuPOQaVsXlAZxhbqA-1; Tue, 11 Oct 2022 10:22:38 -0400 X-MC-Unique: 4fEEWLuPOQaVsXlAZxhbqA-1 Received: by mail-wm1-f71.google.com with SMTP id ay21-20020a05600c1e1500b003b45fd14b53so769076wmb.1 for ; Tue, 11 Oct 2022 07:22:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GQV16OtYWHRgd9rJ43MDznmSk1+OMKF7QVNVAnSQ0lI=; b=gxO2iY34NPhXGWnLxJLDXOoQsUo4C68UKjkzpQX0fxnURRGY96je+86HAW8v65ERjq 0ocPGe+QmwG2EziqMuH4mjeUrzaCVNL6fcVGe3bm6kxzLIo2JzFuGA0elRAyxghzHr2Z YxA81KshkAv7jkk0AQKbMDE/pE5eIrHXazLgW3ux5iVJnfZW2bWGWfwO51GegpR72kXN +ltiPlNkVMVDTI5hyfuosXQz0HcBJ8fYpKgccDzlugIpLMVwNWe55XlqYGwA+zS2ZWGK fse4xK6sIRiRYrgXxlaRJpOFK1p4T1WsE/S6UELPnb7K/88kGOpfOEA6snm3gUmQoFSd 9/fg== X-Gm-Message-State: ACrzQf3LzGnKXP5lqrcJfZddtitkpRiwImRKrWM/IshiiPSfLpMrfZ+N jWAG8cria2if+Fw2CIYlrC+QBRftmZpm1yXqBL+PQ5uNQRaOY3Pixk9hm6/i1yFEHHqAVpHBPpf L0Tr5ZfNU5/M8p2+ArJnFIg== X-Received: by 2002:a05:600c:4ed2:b0:3c6:c1ff:222 with SMTP id g18-20020a05600c4ed200b003c6c1ff0222mr3725305wmq.163.1665498156921; Tue, 11 Oct 2022 07:22:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM50vz6+MqPl7b5gReQjPLKUDLlaJ/TMLQV6BlNCBA28EDLhSqoAEb71ZCYXCdbKT5alKDeutA== X-Received: by 2002:a05:600c:4ed2:b0:3c6:c1ff:222 with SMTP id g18-20020a05600c4ed200b003c6c1ff0222mr3725295wmq.163.1665498156729; Tue, 11 Oct 2022 07:22:36 -0700 (PDT) Received: from localhost (52.72.115.87.dyn.plus.net. [87.115.72.52]) by smtp.gmail.com with ESMTPSA id o18-20020a1c7512000000b003b492753826sm12656042wmc.43.2022.10.11.07.22.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Oct 2022 07:22:36 -0700 (PDT) From: Andrew Burgess To: Tsukasa OI , Tsukasa OI , Mike Frysinger Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v3 3/5] sim: Suppress non-literal printf warning In-Reply-To: <1e125faf59c101508372275aa7b9d00b8d32a7e9.1665038297.git.research_trasio@irq.a4lg.com> References: <1e125faf59c101508372275aa7b9d00b8d32a7e9.1665038297.git.research_trasio@irq.a4lg.com> Date: Tue, 11 Oct 2022 15:22:35 +0100 Message-ID: <87edvecudw.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Oct 2022 14:23:18 -0000 Tsukasa OI writes: > Clang generates a warning if the format string of a printf-like function is > not a literal ("-Wformat-nonliteral"). On the default configuration, it > causes a build failure (unless "--disable-werror" is specified). > > To avoid this warning, this commit now uses vsnprintf to format error > message and pass the message to sim_engine_abort function with another > printf-style formatting. > > This patch is mostly authored by Andrew Burgess and slightly modified by > Tsukasa OI. > > Co-authored-by: Andrew Burgess > Signed-off-by: Tsukasa OI > --- > sim/common/sim-hw.c | 26 ++++++++++++++++---------- > 1 file changed, 16 insertions(+), 10 deletions(-) > > diff --git a/sim/common/sim-hw.c b/sim/common/sim-hw.c > index cece5638bc9..c70770893c9 100644 > --- a/sim/common/sim-hw.c > +++ b/sim/common/sim-hw.c > @@ -403,13 +403,16 @@ sim_hw_io_write_buffer (struct sim_state *sd, > > /* Abort the simulation specifying HW as the reason */ > > -void > +void ATTRIBUTE_PRINTF (2, 0) > hw_vabort (struct hw *me, > const char *fmt, > va_list ap) I've pushed this patch, but I moved the ATTRIBUTE_PRINTF to the function declaration. Thanks, Andrew --- commit 96894c19ad2b91db76b9b606d48c56ad354b4801 Author: Tsukasa OI Date: Thu Oct 6 06:43:51 2022 +0000 sim: Suppress non-literal printf warning Clang generates a warning if the format string of a printf-like function is not a literal ("-Wformat-nonliteral"). On the default configuration, it causes a build failure (unless "--disable-werror" is specified). To avoid this warning, this commit now uses vsnprintf to format error message and pass the message to sim_engine_abort function with another printf-style formatting. This patch is mostly authored by Andrew Burgess and slightly modified by Tsukasa OI. Co-authored-by: Andrew Burgess Signed-off-by: Tsukasa OI diff --git a/sim/common/hw-device.h b/sim/common/hw-device.h index 7a36f2f518c..451f88723de 100644 --- a/sim/common/hw-device.h +++ b/sim/common/hw-device.h @@ -437,10 +437,8 @@ void hw_abort const char *fmt, ...) ATTRIBUTE_PRINTF (2, 3) ATTRIBUTE_NORETURN; -void hw_vabort -(struct hw *me, - const char *fmt, - va_list ap) ATTRIBUTE_NORETURN; +extern void hw_vabort (struct hw *me, const char *fmt, va_list ap) + ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0); void hw_halt (struct hw *me, diff --git a/sim/common/sim-hw.c b/sim/common/sim-hw.c index cece5638bc9..7bfe91e4ae2 100644 --- a/sim/common/sim-hw.c +++ b/sim/common/sim-hw.c @@ -408,8 +408,11 @@ hw_vabort (struct hw *me, const char *fmt, va_list ap) { + int len; const char *name; char *msg; + va_list cpy; + /* find an identity */ if (me != NULL && hw_path (me) != NULL && hw_path (me) [0] != '\0') name = hw_path (me); @@ -419,16 +422,19 @@ hw_vabort (struct hw *me, name = hw_family (me); else name = "device"; - /* construct an updated format string */ - msg = alloca (strlen (name) + strlen (": ") + strlen (fmt) + 1); - strcpy (msg, name); - strcat (msg, ": "); - strcat (msg, fmt); + + /* Expand FMT and AP into MSG buffer. */ + va_copy (cpy, ap); + len = vsnprintf (NULL, 0, fmt, cpy) + 1; + va_end (cpy); + msg = alloca (len); + vsnprintf (msg, len, fmt, ap); + /* report the problem */ - sim_engine_vabort (hw_system (me), - STATE_HW (hw_system (me))->cpu, - STATE_HW (hw_system (me))->cia, - msg, ap); + sim_engine_abort (hw_system (me), + STATE_HW (hw_system (me))->cpu, + STATE_HW (hw_system (me))->cia, + "%s: %s", name, msg); } void