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.129.124]) by sourceware.org (Postfix) with ESMTPS id 13C833857364 for ; Wed, 5 Oct 2022 11:45:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 13C833857364 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-519-flc-bWbvNA2yVY_prmpYIw-1; Wed, 05 Oct 2022 07:45:57 -0400 X-MC-Unique: flc-bWbvNA2yVY_prmpYIw-1 Received: by mail-wm1-f72.google.com with SMTP id b7-20020a05600c4e0700b003bde2d860d1so192172wmq.8 for ; Wed, 05 Oct 2022 04:45:57 -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=Y/tBWm59no4LiuYyWV94fKxlaLGTxZLx9h8GmRS3jDw=; b=1AhLSzb4reZQeBjsr7xYjcGSl/fvsfM596iJM0D2k6r2D/6G8unNtK9wMUFqlfBsc3 U9ISSNG5jrZGB6mYhCHP6wTZvsvmCRp5B6pmokRD/YESrIRPLxFt1/BKBuxMN2CiRnyJ toZDiDG30UdkT96nw60Iot+kZX3rT6uz0q53x2pm+6gaakwep0Xqc+RZBtZzWeebdxhk NUCF/PFVPE+pHWwbKppQRBlnXMsDObH+SlRC+GSmjXWoGaJcfabUt8An7lkfSx6poQCr PT1cMFAsdmQkIMggk1LA4Fw4inUv4LuInh2axI2qtF4X6LPeNjWZydk8UyUpZFMXclmq hdmg== X-Gm-Message-State: ACrzQf142xzsMC157g9IUwkClu2xHJSEGv2VZMUtzZA4icpxzSBQkyTs fVqQumN5EE7w3hBIwT8JJUScw703W1SSmAeTLrulOU/mOMAWkBTw34Lw3IZp6wh3Kg4fy9pCRQF TIR/+QgttorsK/vAyhPzNkA== X-Received: by 2002:a05:6000:168c:b0:226:f4c2:d6db with SMTP id y12-20020a056000168c00b00226f4c2d6dbmr18931949wrd.659.1664970356458; Wed, 05 Oct 2022 04:45:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7K/ZEBnfKwZEwjpJqwG2P9aT3WJ8/eAtO3TrKB1oTTKhRQKnjoXz5QsXdPuvCDRImwC0AtVw== X-Received: by 2002:a05:6000:168c:b0:226:f4c2:d6db with SMTP id y12-20020a056000168c00b00226f4c2d6dbmr18931938wrd.659.1664970356214; Wed, 05 Oct 2022 04:45:56 -0700 (PDT) Received: from localhost (52.72.115.87.dyn.plus.net. [87.115.72.52]) by smtp.gmail.com with ESMTPSA id o11-20020a05600c510b00b003a83ca67f73sm1790915wms.3.2022.10.05.04.45.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 04:45:55 -0700 (PDT) From: Andrew Burgess To: Tsukasa OI , Tsukasa OI , Mike Frysinger Cc: gdb-patches@sourceware.org Subject: Re: [PATCH 4/4] sim: Suppress non-literal printf warning In-Reply-To: <35eaee9855aa3882a1ecc9de9d62c0f8b2fe0e7c.1663073826.git.research_trasio@irq.a4lg.com> References: <35eaee9855aa3882a1ecc9de9d62c0f8b2fe0e7c.1663073826.git.research_trasio@irq.a4lg.com> Date: Wed, 05 Oct 2022 12:45:55 +0100 Message-ID: <87h70ifq8c.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.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, 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: Wed, 05 Oct 2022 11:46:00 -0000 Tsukasa OI writes: > Clang generates a warning if the format parameter of a printf-like function > is not a literal. However, on hw_vabort, it's unavoidable to use non- > literal as a format string (unless we make huge redesign). > > We have "include/diagnostics.h" to suppress certain warnings only when > necessary. Because DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL can suppress > warnings when the format parameter of a printf-like function is not a > literal, this commit adds this (only where necessary) to suppress this > error with "-Werror", the default configuration. > > sim/ChangeLog: > > * common/sim-hw.c (hw_vabort): Suppress non-literal printf warning > by using DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL. > --- > sim/common/sim-hw.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/sim/common/sim-hw.c b/sim/common/sim-hw.c > index cece5638bc9..36f355d2262 100644 > --- a/sim/common/sim-hw.c > +++ b/sim/common/sim-hw.c > @@ -425,10 +425,13 @@ hw_vabort (struct hw *me, > strcat (msg, ": "); > strcat (msg, fmt); > /* report the problem */ > + DIAGNOSTIC_PUSH > + DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL > sim_engine_vabort (hw_system (me), > STATE_HW (hw_system (me))->cpu, > STATE_HW (hw_system (me))->cia, > msg, ap); > + DIAGNOSTIC_POP Rather than disabling diagnostics, I'd like to propose the patch below which expands FMT and AP within sim-hw.c, then passes the expanded string through to sim_engine_abort. What do you think of this? My motivation is to avoid disabling diagnostics as much as possible. As far as I can tell the host_callback_struct::evprintf_filtered callback is just the standard printf API, so using vsnprintf should expand everything correctly. Thanks, Andrew --- 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