Skip to content

Commit 4081deb

Browse files
committed
Attach Database didn't escape filepath of selected file
See issue #2002
1 parent 6ddd6e9 commit 4081deb

4 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/sql/ObjectIdentifier.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ std::string escapeIdentifier(const std::string& id)
5353
}
5454
}
5555

56+
std::string escapeString(const std::string& literal)
57+
{
58+
return '\'' + duplicate_char(literal, '\'') + '\'';
59+
}
60+
5661
bool ObjectIdentifier::fromSerialised(const std::string& serialised)
5762
{
5863
auto pos_comma = serialised.find(",");

src/sql/ObjectIdentifier.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ char getIdentifierQuoteChar();
2020
// Add quotes to an identifier
2121
std::string escapeIdentifier(const std::string& id);
2222

23+
// Add SQL quotes to a string literal and escape any single quote character
24+
std::string escapeString(const std::string& literal);
25+
2326
// Object identifier consisting of schema name and object name
2427
class ObjectIdentifier
2528
{

src/sqlitedb.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ QString escapeIdentifier(const QString& id)
4545
{
4646
return QString::fromStdString(escapeIdentifier(id.toStdString()));
4747
}
48+
QString escapeString(const QString& literal)
49+
{
50+
return QString::fromStdString(escapeString(literal.toStdString()));
51+
}
4852
}
4953

5054
// collation callbacks
@@ -324,7 +328,7 @@ bool DBBrowserDB::attach(const QString& filePath, QString attach_as)
324328
}
325329
}
326330

327-
if(!executeSQL(QString("ATTACH '%1' AS %2 %3").arg(filePath).arg(sqlb::escapeIdentifier(attach_as)).arg(key), false))
331+
if(!executeSQL(QString("ATTACH %1 AS %2 %3").arg(sqlb::escapeString(filePath)).arg(sqlb::escapeIdentifier(attach_as)).arg(key), false))
328332
{
329333
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
330334
return false;
@@ -334,7 +338,7 @@ bool DBBrowserDB::attach(const QString& filePath, QString attach_as)
334338
delete cipherSettings;
335339
#else
336340
// Attach database
337-
if(!executeSQL(QString("ATTACH '%1' AS %2").arg(filePath).arg(sqlb::escapeIdentifier(attach_as)), false))
341+
if(!executeSQL(QString("ATTACH %1 AS %2").arg(sqlb::escapeString(filePath)).arg(sqlb::escapeIdentifier(attach_as)), false))
338342
{
339343
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
340344
return false;

src/sqlitedb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ int collCompare(void* pArg, int sizeA, const void* sA, int sizeB, const void* sB
3333
namespace sqlb
3434
{
3535
QString escapeIdentifier(const QString& id);
36+
QString escapeString(const QString& literal);
3637
}
3738

3839
/// represents a single SQLite database. except when noted otherwise,

0 commit comments

Comments
 (0)