Fix GCC >= 10 -Wstringop-overflow warning when build with -O3

This commit is contained in:
rexy712 2022-06-18 10:33:23 -07:00
parent a2ba8b9eb6
commit 6937f1a885

View File

@ -20,10 +20,9 @@
#define REXY_STRING_BASE_TPP #define REXY_STRING_BASE_TPP
#include <utility> //forward, move, swap, etc #include <utility> //forward, move, swap, etc
#include <cstdlib> //memcpy
#include <cstring> //strlen, strcpy #include <cstring> //strlen, strcpy
#include "utility.hpp" //max #include "utility.hpp" //max, memcpy
#include "detail/string_appender.hpp" #include "detail/string_appender.hpp"
#include "algorithm.hpp" #include "algorithm.hpp"
#include "compat/to_address.hpp" #include "compat/to_address.hpp"
@ -88,7 +87,7 @@ namespace rexy{
this->set_islong_flag(true); this->set_islong_flag(true);
pointer raw = this->set_long_ptr(this->allocate(sizeof(value_type)*(cap+1))); pointer raw = this->set_long_ptr(this->allocate(sizeof(value_type)*(cap+1)));
if(data) if(data)
memcpy(raw, data, sizeof(value_type)*len); rexy::memcpy(raw, data, sizeof(value_type)*len);
raw[len] = 0; raw[len] = 0;
this->set_long_length(len); this->set_long_length(len);
this->set_long_capacity(cap); this->set_long_capacity(cap);
@ -96,7 +95,7 @@ namespace rexy{
this->set_islong_flag(false); this->set_islong_flag(false);
pointer raw = this->set_short_ptr(); pointer raw = this->set_short_ptr();
if(data) if(data)
memcpy(raw, data, sizeof(value_type)*len); rexy::memcpy(raw, data, sizeof(value_type)*len);
raw[len] = 0; raw[len] = 0;
this->set_short_length(len); this->set_short_length(len);
this->set_short_capacity(cap); this->set_short_capacity(cap);
@ -164,7 +163,7 @@ namespace rexy{
noexcept(is_nothrow_allocator_v<Alloc>): noexcept(is_nothrow_allocator_v<Alloc>):
detail::hasallocator<Alloc>(b) detail::hasallocator<Alloc>(b)
{ {
_copy_construct_string(b.get(), b.length(), b.capacity()); _copy_construct_string(b.get(), b.length(), b.length());
} }
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Alloc>::basic_string(basic_string&& s)noexcept: constexpr basic_string<Char,Alloc>::basic_string(basic_string&& s)noexcept:
@ -174,7 +173,7 @@ namespace rexy{
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const string_base<Char>& b) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const string_base<Char>& b)
noexcept(is_nothrow_allocator_v<Alloc>) noexcept(is_nothrow_allocator_v<Alloc>)
{ {
_copy_construct_string(b.get(), b.length(), b.capacity()); _copy_construct_string(b.get(), b.length(), b.length());
} }
//dtor //dtor
@ -192,12 +191,13 @@ namespace rexy{
noexcept(is_nothrow_allocator_v<Alloc>) noexcept(is_nothrow_allocator_v<Alloc>)
{ {
if(s.length() < this->capacity()){ if(s.length() < this->capacity()){
memcpy(this->get_pointer(), s.get_pointer(), sizeof(value_type)*(s.length()+1)); rexy::memcpy(this->get_pointer(), s.get_pointer(), sizeof(value_type)*(s.length()+1));
this->set_length(s.length()); this->set_length(s.length());
return *this; return *this;
}else{
basic_string tmp(s);
return (*this = std::move(tmp));
} }
basic_string tmp(s);
return (*this = std::move(tmp));
} }
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(basic_string&& s)noexcept{ constexpr basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(basic_string&& s)noexcept{
@ -270,7 +270,7 @@ namespace rexy{
pointer raw = this->get_pointer(); pointer raw = this->get_pointer();
if(mylen+len <= mycap){ if(mylen+len <= mycap){
memcpy(raw+mylen, data, sizeof(value_type)*len); rexy::memcpy(raw+mylen, data, sizeof(value_type)*len);
this->set_length(mylen+len); this->set_length(mylen+len);
raw[mylen+len] = 0; raw[mylen+len] = 0;
}else{ }else{
@ -336,7 +336,7 @@ namespace rexy{
size_type len = this->get_short_length(); size_type len = this->get_short_length();
pointer raw = this->get_short_ptr(); pointer raw = this->get_short_ptr();
pointer retval = this->allocate(sizeof(value_type)*len+1); pointer retval = this->allocate(sizeof(value_type)*len+1);
memcpy(retval, raw, sizeof(value_type)*len); rexy::memcpy(retval, raw, sizeof(value_type)*len);
retval[len] = 0; retval[len] = 0;
raw[0] = 0; raw[0] = 0;
this->set_short_length(0); this->set_short_length(0);
@ -361,7 +361,7 @@ namespace rexy{
if(len <= this->length()){ if(len <= this->length()){
this->set_length(len); this->set_length(len);
pointer raw = this->get_pointer(); pointer raw = this->get_pointer();
memcpy(raw, s, sizeof(value_type)*len); rexy::memcpy(raw, s, sizeof(value_type)*len);
raw[len] = 0; raw[len] = 0;
return *this; return *this;
} }