https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85435

            Bug ID: 85435
           Summary: undefined behaviour in std::char_traits<signed
                    char>::move
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

#include <string>

int main()
{
  std::char_traits<signed char>::move(nullptr, nullptr, 0);
}

Compiled with UBsan shows:

/home/jwakely/gcc/8/include/c++/8.0.1/bits/char_traits.h:188:52: runtime error:
null pointer passed as argument 1, which is declared to never be null
/home/jwakely/gcc/8/include/c++/8.0.1/bits/char_traits.h:188:52: runtime error:
null pointer passed as argument 2, which is declared to never be null

The move member for the primary template needs this fix:

--- a/libstdc++-v3/include/bits/char_traits.h
+++ b/libstdc++-v3/include/bits/char_traits.h
@@ -185,6 +185,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     char_traits<_CharT>::
     move(char_type* __s1, const char_type* __s2, std::size_t __n)
     {
+      if (__n == 0)
+       return __s1;
       return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
                                                    __n * sizeof(char_type)));
     }

Reply via email to