Fix -Wstring-compare testcase build failure

Message ID 1592924228-13346-1-git-send-email-gbenson@redhat.com
State New
Headers show
Series
  • Fix -Wstring-compare testcase build failure
Related show

Commit Message

Jose E. Marchesi via Gdb-patches June 23, 2020, 2:57 p.m.
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?

Cheers,
Gary

--
gdb/testsuite/ChangeLog:

	* gdb.cp/try_catch.cc: Replace string literal with a
	pointer	to avoid build failure with -Wstring-compare.
---
 gdb/testsuite/ChangeLog           | 5 +++++
 gdb/testsuite/gdb.cp/try_catch.cc | 5 +++--
 2 files changed, 8 insertions(+), 2 deletions(-)

-- 
1.8.3.1

Comments

Jose E. Marchesi via Gdb-patches June 23, 2020, 3:57 p.m. | #1
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.

>  

>    // 3 use standard library

>    using namespace std;

> +  const char *throwme = "gdb.1";

>    try

>      {

>        if (j < 100)

> -	throw invalid_argument("gdb.1"); // marker 3-throw

> +	throw invalid_argument(throwme); // marker 3-throw

>      }

>    catch (exception& obj)

>      {

> -      if (obj.what() != "gdb.1")	// marker 3-catch

> +      if (obj.what() != throwme)	// marker 3-catch

>  	test &= false;

>      }

>    return 0;

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.

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.

Thanks,
Pedro Alves

Patch

diff --git a/gdb/testsuite/gdb.cp/try_catch.cc b/gdb/testsuite/gdb.cp/try_catch.cc
index 4c4add2..b0c01ba 100644
--- a/gdb/testsuite/gdb.cp/try_catch.cc
+++ b/gdb/testsuite/gdb.cp/try_catch.cc
@@ -122,14 +122,15 @@  int main()
 
   // 3 use standard library
   using namespace std;
+  const char *throwme = "gdb.1";
   try
     {
       if (j < 100)
-	throw invalid_argument("gdb.1"); // marker 3-throw
+	throw invalid_argument(throwme); // marker 3-throw
     }
   catch (exception& obj)
     {
-      if (obj.what() != "gdb.1")	// marker 3-catch
+      if (obj.what() != throwme)	// marker 3-catch
 	test &= false;
     }
   return 0;