Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Gary Benson <gbenson@redhat.com>
To: gdb-patches@sourceware.org
Cc: Pedro Alves <palves@redhat.com>
Subject: [PATCH v2] Fix -Wstring-compare testcase build failure
Date: Fri, 26 Jun 2020 14:05:00 +0100	[thread overview]
Message-ID: <1593176700-16489-1-git-send-email-gbenson@redhat.com> (raw)
In-Reply-To: <db26442d-4830-0154-f2ab-4e51f70bd484@redhat.com>

Pedro Alves wrote:
> On 6/23/20 3:57 PM, Gary Benson via Gdb-patches wrote:
> > Hi all,
> > 
> > Clang fails to compile the file gdb/testsuite/gdb.cp/try_catch.cc
> > with the following error:
> >   warning: result of comparison against a string literal is
> >   unspecified (use strncmp instead) [-Wstring-compare]
> > 
> > This patch replaces the string literal with a pointer, to avoid
> > the error.
> > 
> > Is it ok to commit?
> 
> Hmm, no, I don't think so.
[snip]
> invalid_argument stores a copy of the string, so with or
> without your patch, that (obj.what() != "gdb.1") comparison
> is returning false.  Thus the "test &= false;" statement is
> executing.

Ah, I had not realized the string was copied, I think I thought
the compiler was complaining about something else.

> IOW, that Clang warning found a bug in the test program...
> 
> The fix should be to use strcmp instead.
> 
> And also, please add a market at the "return 0;" line,
> let the program run to it, and then add a gdb_test checking
> that "test" is still true at that point.

Ok.  How about this?

--
Clang fails to compile the file gdb/testsuite/gdb.cp/try_catch.cc
with the following error:
  warning: result of comparison against a string literal is
  unspecified (use strncmp instead) [-Wstring-compare]

This commit fixes the error, replacing the pointer comparison with
a call to strcmp.  This commit also adds a final check: the test
program is run to the final return statement, and the value of
"test" is checked to ensure it is still "true" at that point.

gdb/testsuite/ChangeLog:

	* gdb.cp/try_catch.cc: Include string.h.
	(main): Replace comparison against string literal with
	strcmp, avoiding build failure with -Wstring-compare.
	Add "marker test-complete".
	* gdb.cp/try_catch.exp: Run the test to the above marker,
	then verify that the value of "test" is still true.
---
 gdb/testsuite/ChangeLog            | 9 +++++++++
 gdb/testsuite/gdb.cp/try_catch.cc  | 5 +++--
 gdb/testsuite/gdb.cp/try_catch.exp | 3 +++
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/gdb/testsuite/gdb.cp/try_catch.cc b/gdb/testsuite/gdb.cp/try_catch.cc
index 4c4add2..5f454f4 100644
--- a/gdb/testsuite/gdb.cp/try_catch.cc
+++ b/gdb/testsuite/gdb.cp/try_catch.cc
@@ -18,6 +18,7 @@
 #include <exception>
 #include <stdexcept>
 #include <string>
+#include <string.h>
 
 enum region { oriental, egyptian, greek, etruscan, roman };
 
@@ -129,8 +130,8 @@ int main()
     }
   catch (exception& obj)
     {
-      if (obj.what() != "gdb.1")	// marker 3-catch
+      if (strcmp (obj.what(), "gdb.1") != 0)	// marker 3-catch
 	test &= false;
     }
-  return 0;
+  return 0;	// marker test-complete
 }
diff --git a/gdb/testsuite/gdb.cp/try_catch.exp b/gdb/testsuite/gdb.cp/try_catch.exp
index fb53294..e4da885 100644
--- a/gdb/testsuite/gdb.cp/try_catch.exp
+++ b/gdb/testsuite/gdb.cp/try_catch.exp
@@ -63,5 +63,8 @@ gdb_continue_to_breakpoint "marker 3-throw"
 gdb_breakpoint [gdb_get_line_number "marker 3-catch"]
 gdb_continue_to_breakpoint "marker 3-catch"
 
+gdb_breakpoint [gdb_get_line_number "marker test-complete"]
+gdb_test "p test" "= true"
+
 gdb_exit
 return 0
-- 
1.8.3.1



  reply	other threads:[~2020-06-26 13:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-23 14:57 [PATCH] " Gary Benson
2020-06-23 15:57 ` Pedro Alves
2020-06-26 13:05   ` Gary Benson [this message]
2020-06-26 13:19     ` [PATCH v2] " Pedro Alves
2020-06-26 13:54       ` Gary Benson

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=1593176700-16489-1-git-send-email-gbenson@redhat.com \
    --to=gbenson@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /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