Skip to content

Commit 2e69a81

Browse files
committed
Fix SRombauts#189 unit test "Column.basis" failing on Visual Studio 2013
The implicit cast to std::string() would fallback to const char* with MSVC 2010-2013 (witch does not work with the NULL char in the middle) Without it, trying to access a binary blob with implicit cast to string ends up converting it to a C-style char*, damaging the data by truncating it to the first null character!
1 parent ca45c67 commit 2e69a81

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

appveyor.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ environment:
2323
- arch: Win32
2424
- arch: Win64
2525

26-
matrix:
27-
allow_failures:
28-
- image: Visual Studio 2013
29-
3026
init:
3127
- echo %APPVEYOR_BUILD_WORKER_IMAGE% - %configuration% - %arch%
3228
- if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2017" (set vs=Visual Studio 15 2017)

include/SQLiteCpp/Column.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,16 @@ class Column
224224
return getBlob();
225225
}
226226

227-
#if !(defined(_MSC_VER) && _MSC_VER < 1900)
227+
#if !defined(_MSC_VER) || _MSC_VER >= 1900
228228
// NOTE : the following is required by GCC and Clang to cast a Column result in a std::string
229229
// (error: conversion from ‘SQLite::Column’ to non-scalar type ‘std::string {aka std::basic_string<char>}’)
230+
// and also required for Microsoft Visual Studio 2015 and newer
230231
// but is not working under Microsoft Visual Studio 2010, 2012 and 2013
231232
// (error C2440: 'initializing' : cannot convert from 'SQLite::Column' to 'std::basic_string<_Elem,_Traits,_Ax>'
232233
// [...] constructor overload resolution was ambiguous)
234+
// WARNING: without it, trying to access a binary blob with implicit cast to string
235+
// ends up converting it to a C-style char*, damaging the data by truncating it to the first null character!
236+
// (see https://github.com/SRombauts/SQLiteCpp/issues/189 Visual Studio 2013: unit test "Column.basis" failing)
233237
/**
234238
* @brief Inline cast operator to std::string
235239
*

tests/Column_test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ TEST(Column, basis) {
6868
const int integer = query.getColumn(2); // operator int()
6969
const double real = query.getColumn(3); // operator double()
7070
const void* pblob = query.getColumn(4); // operator void*()
71-
const std::string sblob = query.getColumn(4); // operator std::string() (or const char* with MSVC)
71+
#if !defined(_MSC_VER) || _MSC_VER >= 1900
72+
// This implicit cast should use operator std::string()
73+
// but would fallback to const char* with MSVC 2010-2013 (witch does not work with the NULL char in the middle)
74+
const std::string sblob = query.getColumn(4); // operator std::string()
75+
#endif
7276
const void* pempty = query.getColumn(5); // operator void*()
7377
EXPECT_EQ(1, id1);
7478
EXPECT_EQ(1, id2);
@@ -81,8 +85,10 @@ TEST(Column, basis) {
8185
EXPECT_EQ(-123, integer);
8286
EXPECT_EQ(0.123, real);
8387
EXPECT_EQ(0, memcmp("bl\0b", pblob, size));
88+
#if !defined(_MSC_VER) || _MSC_VER >= 1900
8489
EXPECT_EQ((size_t)size, sblob.size());
8590
EXPECT_EQ(0, memcmp("bl\0b", &sblob[0], size));
91+
#endif
8692
EXPECT_EQ(NULL, pempty);
8793
}
8894

0 commit comments

Comments
 (0)