Fixed GH #1155: Select from MySQL table with longtext column causes SIGSEGV (#1164)

This commit is contained in:
jnytra 2017-07-06 00:44:21 +02:00 committed by Aleksandar Fabijanic
parent 15c076b3ea
commit f8bacb47b5
6 changed files with 96 additions and 2 deletions

View File

@ -322,6 +322,7 @@ public:
private:
bool realExtractFixed(std::size_t pos, enum_field_types type, void* buffer, bool isUnsigned = false);
bool realExtractFixedBlob(std::size_t pos, enum_field_types type, void* buffer, size_t len);
// Prevent VC8 warning "operator= could not be generated"
Extractor& operator=(const Extractor&);

View File

@ -132,6 +132,16 @@ bool Extractor::extract(std::size_t pos, std::string& val)
MetaColumn::ColumnDataType columnType = _metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type();
if (columnType != Poco::Data::MetaColumn::FDT_STRING && columnType != Poco::Data::MetaColumn::FDT_BLOB)
throw MySQLException("Extractor: not a string");
if (columnType == Poco::Data::MetaColumn::FDT_BLOB && _metadata.length(pos) > 0 && _metadata.rawData(pos) == NULL)
{
std::vector<char> buffer(_metadata.length(pos), 0);
bool ret = realExtractFixedBlob(pos, _metadata.row()[pos].buffer_type, buffer.data(), buffer.size());
if (ret)
val.assign(buffer.data(), buffer.size());
return ret;
}
val.assign(reinterpret_cast<const char*>(_metadata.rawData(pos)), _metadata.length(pos));
return true;
@ -146,8 +156,19 @@ bool Extractor::extract(std::size_t pos, Poco::Data::BLOB& val)
if (_metadata.isNull(static_cast<Poco::UInt32>(pos)))
return false;
if (_metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type() != Poco::Data::MetaColumn::FDT_BLOB)
MetaColumn::ColumnDataType columnType = _metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type();
if (columnType != Poco::Data::MetaColumn::FDT_BLOB)
throw MySQLException("Extractor: not a blob");
if (columnType == Poco::Data::MetaColumn::FDT_BLOB && _metadata.length(pos) > 0 && _metadata.rawData(pos) == NULL)
{
std::vector<unsigned char> buffer(_metadata.length(pos), 0);
bool ret = realExtractFixedBlob(pos, _metadata.row()[pos].buffer_type, buffer.data(), buffer.size());
if (ret)
val.assignRaw(buffer.data(), buffer.size());
return ret;
}
val.assignRaw(_metadata.rawData(pos), _metadata.length(pos));
return true;
@ -162,9 +183,20 @@ bool Extractor::extract(std::size_t pos, Poco::Data::CLOB& val)
if (_metadata.isNull(static_cast<Poco::UInt32>(pos)))
return false;
if (_metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type() != Poco::Data::MetaColumn::FDT_BLOB)
MetaColumn::ColumnDataType columnType = _metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type();
if (columnType != Poco::Data::MetaColumn::FDT_BLOB)
throw MySQLException("Extractor: not a blob");
if (columnType == Poco::Data::MetaColumn::FDT_BLOB && _metadata.length(pos) > 0 && _metadata.rawData(pos) == NULL)
{
std::vector<char> buffer(_metadata.length(pos), 0);
bool ret = realExtractFixedBlob(pos, _metadata.row()[pos].buffer_type, buffer.data(), buffer.size());
if (ret)
val.assignRaw(buffer.data(), buffer.size());
return ret;
}
val.assignRaw(reinterpret_cast<const char*>(_metadata.rawData(pos)), _metadata.length(pos));
return true;
}
@ -253,6 +285,21 @@ bool Extractor::realExtractFixed(std::size_t pos, enum_field_types type, void* b
return isNull == 0;
}
bool Extractor::realExtractFixedBlob(std::size_t pos, enum_field_types type, void* buffer, size_t len)
{
MYSQL_BIND bind = {0};
my_bool isNull = 0;
bind.is_null = &isNull;
bind.buffer_type = type;
bind.buffer = buffer;
bind.buffer_length = len;
if (!_stmt.fetchColumn(pos, &bind))
return false;
return isNull == 0;
}
//////////////
// Not implemented

View File

@ -497,6 +497,14 @@ void MySQLTest::testBLOBStmt()
}
void MySQLTest::testLongText()
{
if (!_pSession) fail ("Test not available.");
recreatePersonLongTextTable();
_pExecutor->longText();
}
void MySQLTest::testUnsignedInts()
{
if (!_pSession) fail ("Test not available.");
@ -763,6 +771,13 @@ void MySQLTest::recreatePersonBLOBTable()
catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonBLOBTable()"); }
}
void MySQLTest::recreatePersonLongTextTable()
{
dropTable("Person");
try { *_pSession << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Info LONGTEXT)", now; }
catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreatePersonBLOBTable()"); }
catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonBLOBTable()"); }
}
void MySQLTest::recreatePersonDateTimeTable()
{
@ -957,6 +972,7 @@ CppUnit::Test* MySQLTest::suite()
CppUnit_addTest(pSuite, MySQLTest, testDateTime);
//CppUnit_addTest(pSuite, MySQLTest, testBLOB);
CppUnit_addTest(pSuite, MySQLTest, testBLOBStmt);
CppUnit_addTest(pSuite, MySQLTest, testLongText);
CppUnit_addTest(pSuite, MySQLTest, testUnsignedInts);
CppUnit_addTest(pSuite, MySQLTest, testFloat);
CppUnit_addTest(pSuite, MySQLTest, testDouble);

View File

@ -81,6 +81,7 @@ public:
void testDateTime();
void testBLOB();
void testBLOBStmt();
void testLongText();
void testUnsignedInts();
void testFloat();
@ -121,6 +122,7 @@ private:
void dropTable(const std::string& tableName);
void recreatePersonTable();
void recreatePersonBLOBTable();
void recreatePersonLongTextTable();
void recreatePersonDateTimeTable();
void recreatePersonDateTable();
void recreatePersonTimeTable();

View File

@ -1429,6 +1429,33 @@ void SQLExecutor::blobStmt()
poco_assert (res == blob);
}
void SQLExecutor::longText()
{
std::string funct = "longText()";
std::string lastName("lastname");
std::string firstName("firstname");
std::string address("Address");
std::string info("0123456789");
Poco::Data::CLOB img("0123456789", 10);
int count = 0;
try { *_pSession << "INSERT INTO Person VALUES (?,?,?,?)", use(lastName), use(firstName), use(address), use(info), now; }
catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail (funct); }
try { *_pSession << "SELECT COUNT(*) FROM Person", into(count), now; }
catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail (funct); }
assert (count == 1);
std::string res;
poco_assert (res.size() == 0);
Statement stmt = (*_pSession << "SELECT Info FROM Person", into(res));
try { stmt.execute(); }
catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail (funct); }
catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail (funct); }
poco_assert (res == info);
}
void SQLExecutor::tuples()
{

View File

@ -82,6 +82,7 @@ public:
void blob(unsigned int bigSize = ~0);
void blobStmt();
void longText();
void dateTime();
void date();
void time();