Skip to content

Commit c195cfe

Browse files
authored
Merge pull request #387 Ensure that TEXT column is UTF-8 encoded before using sqlite3_column_blob() from dougnazar
2 parents 0a365b9 + 77fc1f3 commit c195cfe

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/Column.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ std::string Column::getString() const
9292
{
9393
// Note: using sqlite3_column_blob and not sqlite3_column_text
9494
// - no need for sqlite3_column_text to add a \0 on the end, as we're getting the bytes length directly
95+
// however, we need to call sqlite3_column_bytes() to ensure correct format. It's a noop on a BLOB
96+
// or a TEXT value with the correct encoding (UTF-8). Otherwise it'll do a conversion to TEXT (UTF-8).
97+
(void)sqlite3_column_bytes(mStmtPtr.get(), mIndex);
9598
auto data = static_cast<const char *>(sqlite3_column_blob(mStmtPtr.get(), mIndex));
9699

97100
// SQLite docs: "The safest policy is to invoke… sqlite3_column_blob() followed by sqlite3_column_bytes()"

tests/Column_test.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919
#include <stdint.h>
2020

2121

22-
TEST(Column, basis)
22+
static void test_column_basis(bool utf16)
2323
{
2424
// Create a new database
2525
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
2626
EXPECT_EQ(SQLite::OK, db.getErrorCode());
2727
EXPECT_EQ(SQLite::OK, db.getExtendedErrorCode());
2828

29+
if (utf16)
30+
{
31+
EXPECT_EQ(0, db.exec("PRAGMA encoding = 'UTF-16';"));
32+
}
33+
2934
// Create a new table
3035
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL, binary BLOB, empty TEXT)"));
3136
EXPECT_TRUE(db.tableExists("test"));
@@ -82,6 +87,10 @@ TEST(Column, basis)
8287
EXPECT_EQ(1, id1);
8388
EXPECT_EQ(1, id2);
8489
EXPECT_EQ(1, id3);
90+
EXPECT_EQ(1, id4);
91+
EXPECT_EQ(1, id5);
92+
EXPECT_EQ(1, id6);
93+
EXPECT_EQ(1, id7);
8594
EXPECT_EQ(1U, uint1);
8695
EXPECT_EQ(1U, uint2);
8796
EXPECT_EQ(1U, uint3);
@@ -98,14 +107,17 @@ TEST(Column, basis)
98107
EXPECT_EQ(NULL, pempty);
99108
}
100109

110+
query.reset();
111+
query.executeStep();
112+
101113
// validates every variant of explicit getters
102114
{
103115
int64_t id = query.getColumn(0).getInt64();
104116
const unsigned int uint1 = query.getColumn(0).getUInt();
105117
const uint32_t uint2 = query.getColumn(0).getUInt();
118+
const std::string msg1 = query.getColumn(1).getString();
106119
const char* ptxt = query.getColumn(1).getText();
107-
const std::string msg1 = query.getColumn(1).getText();
108-
const std::string msg2 = query.getColumn(1).getString();
120+
const std::string msg2 = query.getColumn(1).getText();
109121
const int integer = query.getColumn(2).getInt();
110122
const double real = query.getColumn(3).getDouble();
111123
const void* pblob = query.getColumn(4).getBlob();
@@ -129,15 +141,15 @@ TEST(Column, basis)
129141
EXPECT_EQ(false, query.getColumn(0).isText());
130142
EXPECT_EQ(false, query.getColumn(0).isBlob());
131143
EXPECT_EQ(false, query.getColumn(0).isNull());
132-
EXPECT_STREQ("1", query.getColumn(0).getText()); // convert to string
144+
EXPECT_STREQ("1", query.getColumn(0).getText()); // convert to TEXT via text func
133145
EXPECT_EQ(1, query.getColumn(0).getBytes()); // size of the string "1" without the null terminator
134146
EXPECT_EQ(SQLite::TEXT, query.getColumn(1).getType());
135147
EXPECT_EQ(false, query.getColumn(1).isInteger());
136148
EXPECT_EQ(false, query.getColumn(1).isFloat());
137149
EXPECT_EQ(true, query.getColumn(1).isText());
138150
EXPECT_EQ(false, query.getColumn(1).isBlob());
139151
EXPECT_EQ(false, query.getColumn(1).isNull());
140-
EXPECT_STREQ("first", query.getColumn(1).getText()); // convert to string
152+
EXPECT_STREQ("first", query.getColumn(1).getString().c_str()); // convert to TEXT via string func
141153
EXPECT_EQ(5, query.getColumn(1).getBytes()); // size of the string "first"
142154
EXPECT_EQ(SQLite::INTEGER, query.getColumn(2).getType());
143155
EXPECT_EQ(true, query.getColumn(2).isInteger());
@@ -161,7 +173,6 @@ TEST(Column, basis)
161173
EXPECT_EQ(false, query.getColumn(4).isText());
162174
EXPECT_EQ(true, query.getColumn(4).isBlob());
163175
EXPECT_EQ(false, query.getColumn(4).isNull());
164-
EXPECT_STREQ("bl\0b", query.getColumn(4).getText()); // convert to string
165176
EXPECT_EQ(4, query.getColumn(4).getBytes()); // size of the blob "bl\0b" with the null char
166177
EXPECT_EQ(SQLite::Null, query.getColumn(5).getType());
167178
EXPECT_EQ(false, query.getColumn(5).isInteger());
@@ -172,6 +183,9 @@ TEST(Column, basis)
172183
EXPECT_STREQ("", query.getColumn(5).getText()); // convert to string
173184
EXPECT_EQ(0, query.getColumn(5).getBytes()); // size of the string "" without the null terminator
174185

186+
query.reset();
187+
query.executeStep();
188+
175189
// Use intermediate Column objects (this is not the recommended way to use the API)
176190
{
177191
const SQLite::Column id = query.getColumn(0);
@@ -185,6 +199,16 @@ TEST(Column, basis)
185199
}
186200
}
187201

202+
TEST(Column, basis)
203+
{
204+
test_column_basis(false);
205+
}
206+
207+
TEST(Column, basis16)
208+
{
209+
test_column_basis(true);
210+
}
211+
188212
TEST(Column, getName)
189213
{
190214
// Create a new database

0 commit comments

Comments
 (0)