Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect query result with use and into into vector, at least for sqlite3. #1130

Open
tamaskenez opened this issue Mar 7, 2024 · 2 comments

Comments

@tamaskenez
Copy link

At least with the sqlite3 backend, when a query is created both with use bindings and with into into a vector, the result is incorrect if the vector's size is 1.

In the test below, the SELECT statement returns one row. If the vector is resized to 2, the result is correct. If it's resized to 1, although it's just enough, it will be cleared.

#include <soci/soci.h>
#include <soci/sqlite3/soci-sqlite3.h>
#include <filesystem>
#include <cassert>
#include <vector>

int main2() {
    std::filesystem::remove("test.db");
    soci::session s(*soci::factory_sqlite3(), "test.db");
    s << "CREATE TABLE test ( key PRIMARY KEY )";
    s << "INSERT INTO test(key) VALUES('foo')";

    std::vector<std::string> v;
    for(auto N: {2u, 1u}) {
        v.resize(N);
        s << "SELECT key FROM test WHERE key = ?", soci::use(std::string("foo")), soci::into(v);
        assert(v.size() == 1 && v[0] == "foo"); // Fails if N == 1, v.size() will be 0
    }
    return 0;
}
@vadz
Copy link
Member

vadz commented Mar 7, 2024

One thing I can say without testing this code is that your use of temporary string with use seems to be incorrect, you should see "use after free" errors with ASAN.

@tamaskenez
Copy link
Author

@vadz thanks for looking into this, I fixed it, still hits the assertion:

#include <soci/soci.h>
#include <soci/sqlite3/soci-sqlite3.h>
#include <cassert>
#include <filesystem>
#include <vector>

int main() {
    std::filesystem::remove("test.db");
    soci::session s(*soci::factory_sqlite3(), "test.db");
    s << "CREATE TABLE test ( key PRIMARY KEY )";
    s << "INSERT INTO test(key) VALUES('foo')";

    std::vector<std::string> v;
    for (auto N : {2u, 1u}) {
        v.resize(N);
        const std::string foo("foo");
        s << "SELECT key FROM test WHERE key = ?", soci::use(foo), soci::into(v);
        assert(v.size() == 1 && v[0] == "foo");  // Fails if N == 1, v.size() will be 0
    }
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants