public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] sim: Suppress non-literal printf warning
Date: Tue, 11 Oct 2022 14:19:26 +0000 (GMT)	[thread overview]
Message-ID: <20221011141926.D162038582B7@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=96894c19ad2b91db76b9b606d48c56ad354b4801

commit 96894c19ad2b91db76b9b606d48c56ad354b4801
Author: Tsukasa OI <research_trasio@irq.a4lg.com>
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 <aburgess@redhat.com>
    Signed-off-by: Tsukasa OI <research_trasio@irq.a4lg.com>

Diff:
---
 sim/common/hw-device.h |  6 ++----
 sim/common/sim-hw.c    | 24 +++++++++++++++---------
 2 files changed, 17 insertions(+), 13 deletions(-)

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

                 reply	other threads:[~2022-10-11 14:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221011141926.D162038582B7@sourceware.org \
    --to=aburgess@sourceware.org \
    --cc=gdb-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).