Blame SOURCES/gdb-rhbz1905996-fix-off-by-one-error-in-ada_fold_name.patch

a8223e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
a8223e
From: Kevin Buettner <kevinb@redhat.com>
a8223e
Date: Tue, 8 Dec 2020 14:07:45 -0700
a8223e
Subject: gdb-rhbz1905996-fix-off-by-one-error-in-ada_fold_name.patch
a8223e
a8223e
;; Fix off-by-one error in ada_fold_name.patch (RH BZ 1905996)
a8223e
;; Upstream patch proposal: https://sourceware.org/pipermail/gdb-patches/2020-December/173935.html
a8223e
;; =fedoratest
a8223e
a8223e
Fix off-by-one error in ada_fold_name
a8223e
a8223e
I'm seeing a libstdc++ assertion failure when running GDB's "maint selftest"
a8223e
command when GDB is configured with the following CFLAGS and CXXFLAGS as
a8223e
part of the configure line:
a8223e
a8223e
  CFLAGS='-D_GLIBCXX_DEBUG -g3 -O0' CXXFLAGS='-D_GLIBCXX_DEBUG -g3 -O0'
a8223e
a8223e
This is what I see when running the self tests:
a8223e
a8223e
(gdb) maint selftest
a8223e
Running selftest aarch64-analyze-prologue.
a8223e
Running selftest aarch64-process-record.
a8223e
Running selftest arm-record.
a8223e
Running selftest arm_analyze_prologue.
a8223e
Running selftest array_view.
a8223e
Running selftest child_path.
a8223e
Running selftest cli_utils.
a8223e
Running selftest command_structure_invariants.
a8223e
Running selftest copy_bitwise.
a8223e
Running selftest copy_integer_to_size.
a8223e
Running selftest cp_remove_params.
a8223e
Running selftest cp_symbol_name_matches.
a8223e
Running selftest dw2_expand_symtabs_matching.
a8223e
/usr/include/c++/11/string_view:211: constexpr const value_type& std::basic_string_view<_CharT, _Traits>::operator[](std::basic_string_view<_CharT, _Traits>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; std::basic_string_view<_CharT, _Traits>::const_reference = const char&; std::basic_string_view<_CharT, _Traits>::size_type = long unsigned int]: Assertion '__pos < this->_M_len' failed.
a8223e
Aborted (core dumped)
a8223e
a8223e
Here's a partial stack trace:
a8223e
a8223e
  #0  0x00007ffff6ef6262 in raise () from /lib64/libc.so.6
a8223e
  #1  0x00007ffff6edf8a4 in abort () from /lib64/libc.so.6
a8223e
  #2  0x00000000004249bf in std::__replacement_assert (
a8223e
      __file=0xef7480 "/usr/include/c++/11/string_view", __line=211,
a8223e
      __function=0xef7328 "constexpr const value_type& std::basic_string_view<_CharT, _Traits>::operator[](std::basic_string_view<_CharT, _Traits>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; std::ba"...,
a8223e
      __condition=0xef7311 "__pos < this->_M_len")
a8223e
      at /usr/include/c++/11/x86_64-redhat-linux/bits/c++config.h:2624
a8223e
  #3  0x0000000000451737 in std::basic_string_view<char, std::char_traits<char> >::operator[] (this=0x7fffffffc200, __pos=8)
a8223e
      at /usr/include/c++/11/string_view:211
a8223e
  #4  0x00000000004329f5 in ada_fold_name (name="function")
a8223e
      at /ironwood1/sourceware-git/rawhide-master/bld/../../worktree-master/gdb/ada-lang.c:988
a8223e
a8223e
And, looking at frame #4...
a8223e
a8223e
(top-gdb) up 4
a8223e
    at /ironwood1/sourceware-git/rawhide-master/bld/../../worktree-master/gdb/ada-lang.c:988
a8223e
988		fold_buffer[i] = tolower (name[i]);
a8223e
(top-gdb) p i
a8223e
$1 = 8
a8223e
(top-gdb) p name.size()
a8223e
$2 = 8
a8223e
a8223e
My patch adjusts the comparison to only copy name.size() characters
a8223e
from the string.  I've added a separate statement for NUL character
a8223e
termination of fold_buffer[].
a8223e
a8223e
gdb/ChangeLog:
a8223e
a8223e
	* ada-lang.c (ada_fold_name): Fix off-by-one error.
a8223e
a8223e
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
a8223e
--- a/gdb/ada-lang.c
a8223e
+++ b/gdb/ada-lang.c
a8223e
@@ -1006,8 +1006,9 @@ struct type *
a8223e
     {
a8223e
       int i;
a8223e
 
a8223e
-      for (i = 0; i <= len; i += 1)
a8223e
+      for (i = 0; i < len; i += 1)
a8223e
         fold_buffer[i] = tolower (name[i]);
a8223e
+      fold_buffer[i] = '\0';
a8223e
     }
a8223e
 
a8223e
   return fold_buffer;