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

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