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

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