From f21883785d80d596dbbea6be7e2b711a89ec5495 Mon Sep 17 00:00:00 2001 From: DarthArgus Date: Sun, 16 Oct 2016 05:43:39 +0000 Subject: [PATCH] auto-truncate too long strings before insertion into the db --- .../src/shared/core/DbBindableString.h | 51 +++++++++++++------ .../src/shared/core/DbBindableUnicode.h | 20 +++++--- 2 files changed, 49 insertions(+), 22 deletions(-) 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'; }