Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH][gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name
@ 2020-07-05 21:40 Tom de Vries
  2020-07-06  2:27 ` Tom Tromey
  2020-07-06  2:47 ` Simon Marchi
  0 siblings, 2 replies; 4+ messages in thread
From: Tom de Vries @ 2020-07-05 21:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Hi,

When building gdb with CFLAGS=-std=gnu17 and CXXFLAGS=-std=gnu++17 and running
test-case gdb.tui/new-layout.exp, we run into:
...
UNRESOLVED: gdb.tui/new-layout.exp: left window box after shrink (ll corner)
FAIL: gdb.tui/new-layout.exp: right window box after shrink (ll corner)
...

In a minimal form, we run into an abort when issuing a winheight command:
...
$ gdb -tui -ex "winheight src - 5"
   <tui stuff>
Aborted (core dumped)
$
...
with this backtrace at the abort:
...
\#0  0x0000000000438db0 in std::char_traits<char>::length (__s=0x0)
     at /usr/include/c++/9/bits/char_traits.h:335
\#1  0x000000000043b72e in std::basic_string_view<char, \
   std::char_traits<char> >::basic_string_view (this=0x7fffffffd4f0, \
   __str=0x0) at /usr/include/c++/9/string_view:124
\#2  0x000000000094971b in tui_partial_win_by_name (name="src")
     at src/gdb/tui/tui-win.c:663
...
due to a NULL comparison which constructs a string_view object from NULL:
...
   657  /* Answer the window represented by name.  */
   658  static struct tui_win_info *
   659  tui_partial_win_by_name (gdb::string_view name)
   660  {
   661    struct tui_win_info *best = nullptr;
   662
   663    if (name != NULL)
...

In gdbsupport/gdb_string_view.h, we either use:
- gdb's copy of libstdc++-v3/include/experimental/string_view, or
- the standard implementation of string_view, when built with C++17 or later
  (which in gcc's case comes from libstdc++-v3/include/std/string_view)

In the first case, there's support for constructing a string_view from a NULL
pointer:
...
      /*constexpr*/ basic_string_view(const _CharT* __str)
      : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
        _M_str{__str}
      { }
...
but in the second case, there's not:
...
      __attribute__((__nonnull__)) constexpr
      basic_string_view(const _CharT* __str) noexcept
      : _M_len{traits_type::length(__str)},
        _M_str{__str}
      { }
...

Fix this by removing the NULL comparison altogether.

Build on x86_64-linux with CFLAGS=-std=gnu17 and CXXFLAGS=-std=gnu++17, and
tested.

Any comments?

Thanks,
- Tom

[gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name

gdb/ChangeLog:

2020-07-05  Tom de Vries  <tdevries@suse.de>

	PR tui/26205
	* tui/tui-win.c (tui_partial_win_by_name): Don't test for NULL name.

---
 gdb/tui/tui-win.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/gdb/tui/tui-win.c b/gdb/tui/tui-win.c
index 336571f158..f906b0dc4f 100644
--- a/gdb/tui/tui-win.c
+++ b/gdb/tui/tui-win.c
@@ -660,21 +660,18 @@ tui_partial_win_by_name (gdb::string_view name)
 {
   struct tui_win_info *best = nullptr;
 
-  if (name != NULL)
+  for (tui_win_info *item : all_tui_windows ())
     {
-      for (tui_win_info *item : all_tui_windows ())
-	{
-	  const char *cur_name = item->name ();
+      const char *cur_name = item->name ();
 
-	  if (name == cur_name)
-	    return item;
-	  if (startswith (cur_name, name))
-	    {
-	      if (best != nullptr)
-		error (_("Window name \"%*s\" is ambiguous"),
-		       (int) name.size (), name.data ());
-	      best = item;
-	    }
+      if (name == cur_name)
+	return item;
+      if (startswith (cur_name, name))
+	{
+	  if (best != nullptr)
+	    error (_("Window name \"%*s\" is ambiguous"),
+		   (int) name.size (), name.data ());
+	  best = item;
 	}
     }
 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name
  2020-07-05 21:40 [PATCH][gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name Tom de Vries
@ 2020-07-06  2:27 ` Tom Tromey
  2020-07-06  2:47 ` Simon Marchi
  1 sibling, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2020-07-06  2:27 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches, Tom Tromey

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> 2020-07-05  Tom de Vries  <tdevries@suse.de>

Tom> 	PR tui/26205
Tom> 	* tui/tui-win.c (tui_partial_win_by_name): Don't test for NULL name.

Thanks, this is ok.

Tom


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name
  2020-07-05 21:40 [PATCH][gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name Tom de Vries
  2020-07-06  2:27 ` Tom Tromey
@ 2020-07-06  2:47 ` Simon Marchi
  2020-07-06  9:19   ` Tom de Vries
  1 sibling, 1 reply; 4+ messages in thread
From: Simon Marchi @ 2020-07-06  2:47 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches; +Cc: Tom Tromey

On 2020-07-05 5:40 p.m., Tom de Vries wrote:
> Hi,
> 
> When building gdb with CFLAGS=-std=gnu17 and CXXFLAGS=-std=gnu++17 and running
> test-case gdb.tui/new-layout.exp, we run into:
> ...
> UNRESOLVED: gdb.tui/new-layout.exp: left window box after shrink (ll corner)
> FAIL: gdb.tui/new-layout.exp: right window box after shrink (ll corner)
> ...
> 
> In a minimal form, we run into an abort when issuing a winheight command:
> ...
> $ gdb -tui -ex "winheight src - 5"
>    <tui stuff>
> Aborted (core dumped)
> $
> ...
> with this backtrace at the abort:
> ...
> \#0  0x0000000000438db0 in std::char_traits<char>::length (__s=0x0)
>      at /usr/include/c++/9/bits/char_traits.h:335
> \#1  0x000000000043b72e in std::basic_string_view<char, \
>    std::char_traits<char> >::basic_string_view (this=0x7fffffffd4f0, \
>    __str=0x0) at /usr/include/c++/9/string_view:124
> \#2  0x000000000094971b in tui_partial_win_by_name (name="src")
>      at src/gdb/tui/tui-win.c:663
> ...
> due to a NULL comparison which constructs a string_view object from NULL:
> ...
>    657  /* Answer the window represented by name.  */
>    658  static struct tui_win_info *
>    659  tui_partial_win_by_name (gdb::string_view name)
>    660  {
>    661    struct tui_win_info *best = nullptr;
>    662
>    663    if (name != NULL)
> ...
> 
> In gdbsupport/gdb_string_view.h, we either use:
> - gdb's copy of libstdc++-v3/include/experimental/string_view, or
> - the standard implementation of string_view, when built with C++17 or later
>   (which in gcc's case comes from libstdc++-v3/include/std/string_view)
> 
> In the first case, there's support for constructing a string_view from a NULL
> pointer:
> ...
>       /*constexpr*/ basic_string_view(const _CharT* __str)
>       : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
>         _M_str{__str}
>       { }
> ...
> but in the second case, there's not:
> ...
>       __attribute__((__nonnull__)) constexpr
>       basic_string_view(const _CharT* __str) noexcept
>       : _M_len{traits_type::length(__str)},
>         _M_str{__str}
>       { }
> ...
> 
> Fix this by removing the NULL comparison altogether.
> 
> Build on x86_64-linux with CFLAGS=-std=gnu17 and CXXFLAGS=-std=gnu++17, and
> tested.
> 
> Any comments?

This is the second string_view problem in a short amount of time, the other being:

  https://sourceware.org/bugzilla/show_bug.cgi?id=26183

When importing the string_view support, I ended up copying experimental/string_view
(which is a version from before it was a standard) from libstdc++, rather than
std/string_view.  In this message:

  https://sourceware.org/legacy-ml/gdb-patches/2018-04/msg00111.html

I said it was because "it is easier to adapt to the c++ standard we follow", but I
don't remember the specific reason.  I think we should change our string_view to
be an adapted copy of std/string_view instead, to minimize the chances of differences
with the standard version.  I started to do it the other day (but did not finish
adapting the tests), and didn't see any blocker.

I still think your patch is good (it would be necessary in any case).

Simon


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name
  2020-07-06  2:47 ` Simon Marchi
@ 2020-07-06  9:19   ` Tom de Vries
  0 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2020-07-06  9:19 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Tom Tromey

On 7/6/20 4:47 AM, Simon Marchi wrote:
> On 2020-07-05 5:40 p.m., Tom de Vries wrote:
>> Hi,
>>
>> When building gdb with CFLAGS=-std=gnu17 and CXXFLAGS=-std=gnu++17 and running
>> test-case gdb.tui/new-layout.exp, we run into:
>> ...
>> UNRESOLVED: gdb.tui/new-layout.exp: left window box after shrink (ll corner)
>> FAIL: gdb.tui/new-layout.exp: right window box after shrink (ll corner)
>> ...
>>
>> In a minimal form, we run into an abort when issuing a winheight command:
>> ...
>> $ gdb -tui -ex "winheight src - 5"
>>    <tui stuff>
>> Aborted (core dumped)
>> $
>> ...
>> with this backtrace at the abort:
>> ...
>> \#0  0x0000000000438db0 in std::char_traits<char>::length (__s=0x0)
>>      at /usr/include/c++/9/bits/char_traits.h:335
>> \#1  0x000000000043b72e in std::basic_string_view<char, \
>>    std::char_traits<char> >::basic_string_view (this=0x7fffffffd4f0, \
>>    __str=0x0) at /usr/include/c++/9/string_view:124
>> \#2  0x000000000094971b in tui_partial_win_by_name (name="src")
>>      at src/gdb/tui/tui-win.c:663
>> ...
>> due to a NULL comparison which constructs a string_view object from NULL:
>> ...
>>    657  /* Answer the window represented by name.  */
>>    658  static struct tui_win_info *
>>    659  tui_partial_win_by_name (gdb::string_view name)
>>    660  {
>>    661    struct tui_win_info *best = nullptr;
>>    662
>>    663    if (name != NULL)
>> ...
>>
>> In gdbsupport/gdb_string_view.h, we either use:
>> - gdb's copy of libstdc++-v3/include/experimental/string_view, or
>> - the standard implementation of string_view, when built with C++17 or later
>>   (which in gcc's case comes from libstdc++-v3/include/std/string_view)
>>
>> In the first case, there's support for constructing a string_view from a NULL
>> pointer:
>> ...
>>       /*constexpr*/ basic_string_view(const _CharT* __str)
>>       : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
>>         _M_str{__str}
>>       { }
>> ...
>> but in the second case, there's not:
>> ...
>>       __attribute__((__nonnull__)) constexpr
>>       basic_string_view(const _CharT* __str) noexcept
>>       : _M_len{traits_type::length(__str)},
>>         _M_str{__str}
>>       { }
>> ...
>>
>> Fix this by removing the NULL comparison altogether.
>>
>> Build on x86_64-linux with CFLAGS=-std=gnu17 and CXXFLAGS=-std=gnu++17, and
>> tested.
>>
>> Any comments?
> 
> This is the second string_view problem in a short amount of time, the other being:
> 
>   https://sourceware.org/bugzilla/show_bug.cgi?id=26183
> 
> When importing the string_view support, I ended up copying experimental/string_view
> (which is a version from before it was a standard) from libstdc++, rather than
> std/string_view.  In this message:
> 
>   https://sourceware.org/legacy-ml/gdb-patches/2018-04/msg00111.html
> 
> I said it was because "it is easier to adapt to the c++ standard we follow", but I
> don't remember the specific reason.

Sounds like a good rationale to me.

> I think we should change our string_view to
> be an adapted copy of std/string_view instead, to minimize the chances of differences
> with the standard version.  I started to do it the other day (but did not finish
> adapting the tests), and didn't see any blocker.
> 

I suppose it's a trade-off, but I agree ideally we use std/string_view.

> I still think your patch is good (it would be necessary in any case).

Ack, committed, and thanks for the explanation.

- Tom


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-07-06  9:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-05 21:40 [PATCH][gdb/tui,c++17] Fix NULL string_view in tui_partial_win_by_name Tom de Vries
2020-07-06  2:27 ` Tom Tromey
2020-07-06  2:47 ` Simon Marchi
2020-07-06  9:19   ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox