diff --git a/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableString.h b/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableString.h index 2548990e..7a1d2e04 100755 --- a/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableString.h +++ b/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableString.h @@ -249,33 +249,52 @@ namespace DB return; } - // the following is strncpy, but keeps a count of the number of characters - unsigned int i; - for (i=0;i= S) { + WARNING(true, ("Attmpted to insert %s which is too long. Truncating.", buffer)); + indicator = S; + memcpy(m_value, buffer, indicator-1); + } else { + indicator = bufsize; + memcpy(m_value, buffer, indicator); } - FATAL((i==S+1) && (m_value[S]!='\0'),("Attempt to save string \"%s\" to the database. It is too long for the column.",buffer)); - indicator=i; // set indicator to actual length of string -// m_value[S]='\0'; // guarantee nullptr terminator -- uncomment if you remove the above FATAL + + m_value[indicator] = '\0'; } template void BindableString::setValue(const Unicode::String &buffer) { - FATAL(buffer.size()>S,("Attempt to save a Unicode::String \"%s\"that is too long to the database.", Unicode::wideToNarrow(buffer).c_str())); - strncpy(m_value, Unicode::wideToNarrow(buffer).c_str(), S+1); - indicator=buffer.size(); + size_t bufsize = buffer.size(); + + if (bufsize >= S) { + WARNING(true, ("Attmpted to insert %s which is too long. Truncating.", buffer.c_str())); + indicator = S; + memcpy(m_value, Unicode::wideToNarrow(buffer).c_str(), indicator-1); + } else { + indicator = bufsize; + memcpy(m_value, Unicode::wideToNarrow(buffer).c_str(), indicator); + } + + m_value[indicator] = '\0'; } template void BindableString::setValue(const std::string &buffer) { - FATAL(buffer.length()>S,("Attempt to save a std::string \"%s\"that is too long to the database.", buffer.c_str())); - strncpy(m_value, buffer.c_str(), S+1); - indicator=buffer.length(); + size_t bufsize = buffer.size(); + + if (bufsize >= S) { + WARNING(true, ("Attmpted to insert %s which is too long. Truncating.", buffer.c_str())); + indicator = S; + memcpy(m_value, buffer.c_str(), indicator-1); + } else { + indicator = bufsize; + memcpy(m_value, buffer.c_str(), indicator); + } + + m_value[indicator] = '\0'; } template diff --git a/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableUnicode.h b/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableUnicode.h index 544ead4c..fba31be0 100755 --- a/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableUnicode.h +++ b/engine/shared/library/sharedDatabaseInterface/src/shared/core/DbBindableUnicode.h @@ -192,12 +192,20 @@ namespace DB template void BindableUnicode::setValue(const Unicode::String &buffer) { - FATAL(buffer.size()>S,("Attempt to save a Unicode::String \"%s\"that is too long to the database.", Unicode::wideToNarrow(buffer).c_str())); - - std::string str; - str = Unicode::wideToUTF8(buffer, str); - indicator = (str.size()>S ? S : str.size()); - memcpy(m_value, str.c_str(),indicator); + std::string str; + str = Unicode::wideToUTF8(buffer, str); + + size_t bufsize = str.size(); + + if (bufsize >= S) { + WARNING(true, ("Attmpted to insert %s which is too long. Truncating.", buffer.c_str())); + indicator = S; + memcpy(m_value, str.c_str(), indicator-1); + } else { + indicator = bufsize; + memcpy(m_value, str.c_str(), indicator); + } + m_value[indicator] = '\0'; }