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

storage.backup_to(filename) crashes in backup_tests.cpp #540

Closed
juandent opened this issue Jul 3, 2020 · 27 comments
Closed

storage.backup_to(filename) crashes in backup_tests.cpp #540

juandent opened this issue Jul 3, 2020 · 27 comments
Labels

Comments

@juandent
Copy link
Contributor

juandent commented Jul 3, 2020

TEST_CASE("backup") in backup_tests.cpp calls retain (see below) with this == nullptr.

First it calls:

TEST_CASE("backup") {
    using namespace BackupTests;
    using Catch::Matchers::UnorderedEquals;
    const std::string usersTableName = "users";
    auto makeStorage = [&usersTableName](const std::string &filename) {
        return make_storage(
            filename,
            make_table(usersTableName, make_column("id", &User::id, primary_key()), make_column("name", &User::name)));
    };
    const std::string backupFilename = "backup.sqlite";
    SECTION("to") {
        ::remove(backupFilename.c_str());
        auto storage2 = makeStorage(backupFilename);
        auto storage = makeStorage("");
        storage.sync_schema();
        storage.replace(User{1, "Sharon"});
        storage.replace(User{2, "Maitre"});
        storage.replace(User{3, "Rita"});
        REQUIRE(!storage2.table_exists(usersTableName));
        SECTION("filename") {
            storage.backup_to(backupFilename);       // THIS CRASHES
        }
namespace sqlite_orm {

    namespace internal {

        struct connection_holder {

            connection_holder(std::string filename_) : filename(move(filename_)) {}

            void retain() {
                ++this->_retain_count;      // this == 000000000

What is happening?

Regards,
Juan

@juandent
Copy link
Contributor Author

juandent commented Jul 3, 2020

The cultrip is found in sqlite_orm.h method backup_t make_backup_to(const std::string &filename) of class sqlite_orm::internal::storage_base

                auto holder = std::make_unique<connection_holder>(filename);
>>>>>>>>.return {connection_ref{*holder}, "main", this->get_connection(), "main", move(holder)};

The correct solutions are:

  1.         auto conn_ref{ connection_ref{*holder} };
             return { conn_ref, "main", this->get_connection(), "main", move(holder) };
    
  2.         or change the order of the none-static data elements for the class struct backup_t 
    

This solves this issue and I bet some others!!

@fnc12
Copy link
Owner

fnc12 commented Jul 3, 2020

@juandent what branch? master or dev?

@juandent
Copy link
Contributor Author

juandent commented Jul 3, 2020

master

@juandent
Copy link
Contributor Author

juandent commented Jul 3, 2020

the problem is that move(holder) is done before connection_ref{*holder}, thus passing a nullptr to connection_ref

@juandent
Copy link
Contributor Author

juandent commented Jul 3, 2020

this happens because the order of the data members in the class backup_t moves holder first making it empty...

            std::unique_ptr<connection_holder> holder;
            connection_ref to;
            connection_ref from;
'''

@fnc12
Copy link
Owner

fnc12 commented Jul 3, 2020

@juandent ok thank you. I cannot reproduce it. But I think the reason is that the order of expression evaluation is not determined before C++17. Anyway I shall apply your fix and merge it soon

@fnc12
Copy link
Owner

fnc12 commented Jul 3, 2020

The fix is here #542

@fnc12 fnc12 added the bug label Jul 3, 2020
@fnc12
Copy link
Owner

fnc12 commented Jul 3, 2020

@juandent please help me. Some builds failed https://travis-ci.org/github/fnc12/sqlite_orm/jobs/704755026 and I cannot reproduce it with Xcode. Looks like exact error you showed here before. Also it turned out that backup_tests.cpp were not included into cmakelists file and this is why these tests failed - they were not supported actually (my bad). My PR uses the fix you suggested bug it calls SEGFAULT and I need to fix it. I shall appreciate it much

@juandent
Copy link
Contributor Author

juandent commented Jul 3, 2020

although the order of operations in an expression was (before C++ 17) unpredictable, the order of initialization of the non-static data members of a class/struct is and has always been deterministic: they are initialized in the exact order they appear in the class -- however not the order they appear in the constructors because they could have different orders. Thus backup_t should declare its non-static data members in the following order to avoid passing an empty holder:

         protected:
            sqlite3_backup *handle = nullptr;
            connection_ref to;
            connection_ref from;
            std::unique_ptr<connection_holder> holder;

Holder must be the last!!

Tell me if this helps you - it should!!

Regards,
Juan

@juandent
Copy link
Contributor Author

juandent commented Jul 3, 2020

internal::storage_base::make_backup_to should have this code:

            backup_t make_backup_to(const std::string &filename) {
                auto holder = std::make_unique<connection_holder>(filename);
                auto conn_ref{ connection_ref{*holder} };
                return { conn_ref, "main", this->get_connection(), "main", move(holder) };

@juandent
Copy link
Contributor Author

juandent commented Jul 3, 2020

sorry I was away from my computer - did this help any?

@fnc12
Copy link
Owner

fnc12 commented Jul 4, 2020

Thank you for your suggestions. I applied them a minute ago and now we need to wait until unit tests are finished on travis and appveyor. I can take up to 2 hours.

@juandent
Copy link
Contributor Author

juandent commented Jul 4, 2020

how are you doing ? what happened with the builds?

@fnc12
Copy link
Owner

fnc12 commented Jul 4, 2020

@fnc12
Copy link
Owner

fnc12 commented Jul 4, 2020

@juandent what OS and compiler do you use?

@juandent
Copy link
Contributor Author

juandent commented Jul 4, 2020 via email

@juandent
Copy link
Contributor Author

juandent commented Jul 4, 2020

why don't you install a VMWARE virtual system with Windows and install the (free ) Visual Studio 2019?

@fnc12
Copy link
Owner

fnc12 commented Jul 5, 2020

Good news: I am able to reproduce it with VS on Win10 (on virtual machine).

@fnc12
Copy link
Owner

fnc12 commented Jul 5, 2020

Ok I fixed windows but mac fails tests. SEGFAULT doesn't happen now.

@juandent
Copy link
Contributor Author

juandent commented Jul 5, 2020

which requiere is failing?

@fnc12
Copy link
Owner

fnc12 commented Jul 5, 2020

Ok I merge the PR cause the issue is not related to this PR

@fnc12
Copy link
Owner

fnc12 commented Jul 5, 2020

merged. Please check out dev branch

@fnc12
Copy link
Owner

fnc12 commented Jul 6, 2020

is the issue actual?

@juandent
Copy link
Contributor Author

juandent commented Jul 7, 2020

How do I download the dev branch? I am not too proficient with git yet...
and after downloading how do I run CMAKE on the download to get the binary builds (e.g. examples and tests)?

When I run CMAKE I get this error:

CMake Error at tests/CMakeLists.txt:21 (find_package):
By not providing "FindCatch2.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Catch2", but
CMake did not find one.

Could not find a package configuration file provided by "Catch2" with any
of the following names:

Catch2Config.cmake
catch2-config.cmake

Add the installation prefix of "Catch2" to CMAKE_PREFIX_PATH or set
"Catch2_DIR" to a directory containing one of the above files. If "Catch2"
provides a separate development package or SDK, be sure it has been
installed.

and I downloade Catch2 but it does not come with Catch2Config.cmake (instead it comes with Catch2Config.cmake.in)

So I am stuck.
Please help me so I can help you!!

Regards,
Juan

@fnc12
Copy link
Owner

fnc12 commented Jul 20, 2020

you need to install catch2. It is a bug #535

@fnc12
Copy link
Owner

fnc12 commented Jul 25, 2020

closing this issue cause it is fixed. @juandent is you have issues with git please contact me with e-mail. Thanks

@fnc12 fnc12 closed this as completed Jul 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants