auto-truncate too long strings before insertion into the db

This commit is contained in:
DarthArgus
2016-10-16 05:43:39 +00:00
parent b1694841f2
commit f21883785d
2 changed files with 49 additions and 22 deletions
@@ -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+1;++i)
{
m_value[i]=buffer[i];
if (m_value[i]=='\0')
break;
size_t bufsize = strlen(buffer);
if (bufsize >= 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<int S>
void BindableString<S>::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<int S>
void BindableString<S>::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<int S>
@@ -192,12 +192,20 @@ namespace DB
template<int S>
void BindableUnicode<S>::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';
}