SQLite Forum

free(): corrupted unsorted chunks
Login

free(): corrupted unsorted chunks

(1) By Gavin Henry (ghenry) on 2021-12-07 09:46:40 [link] [source]

Morning all,

Got a weird bug in my project that shows itself on the second run of make check:

free(): corrupted unsorted chunks

If I don't do this, it's fine on each run:

assert_int_equal(sqlite3_close(db), SQLITE_OK);

Project https://github.com/SentryPeer/SentryPeer

Line I have to comment out - https://github.com/SentryPeer/SentryPeer/blob/main/tests/unit_tests/test_database.c#L153

In each test I open my sqlite db, finalize and close. In my last test I remove the db. Seems I'm closing the db in an inconsistent state?

This doesn't fail though:

assert_int_equal(sqlite3_close(db), SQLITE_OK);

Any ideas? I know it's not sqlite3, but my usage :-)

Lastly, if I run without make check and just run:

./tests/unit_tests/runner

I get no free() issues. Even running via gdb tests/unit_tests/runner, I get no issues with the . Just need a second pair of each on my tests.

Thanks, Gavin.

(2) By Gunter Hick (gunter_hick) on 2021-12-07 12:25:05 in reply to 1 [source]

Have you tried running it under valgrind?

(3) By Gavin Henry (ghenry) on 2021-12-07 12:41:06 in reply to 2 [link] [source]

I don't have any crashes running like so:

tests/unit_tests/runner

What's the best way to do make check via valgrind?

I also have libasan support added. It finds leaks in other places, but not this.

(4) By Gunter Hick (gunter_hick) on 2021-12-07 13:22:04 in reply to 3 [link] [source]

valgrind plus its configuration options are prepended to the command line used to run the image to be checked. I'm afraid you will have to modify the makefile recipe for the failing subtarget of target "check" accordingly.

This type of error typically occurs when
- a write operation overruns the allocated space
- a write operation occurs through a dangling pointer, i.e. after the allocated space has been freed
- a write operation occurs through an uninitialized pointer (that resides on the heap and contains whatever garbage was left behind)
- a write operation occurs on an array element outside the allocated space (i.e. an invalid/uninitialized index is used)

Typically, the location where the error is detected is miles away from where the corruption occurred.

(5) By Gavin Henry (ghenry) on 2021-12-07 13:46:51 in reply to 4 [link] [source]

I think I'll run valgrind on the runner binary and clean it up. See if this goes away.

(6) By Gavin Henry (ghenry) on 2021-12-07 13:56:13 in reply to 4 [link] [source]

So I've commented out all over tests and am just running these two:

cmocka_unit_test(test_open_insert_close_sqlite_db),
cmocka_unit_test(test_open_select_close_sqlite_db),

no issues with make check at all. Must be something left over from other tests. I'll add a teardown.

Thanks for reading!

(7) By Gavin Henry (ghenry) on 2021-12-07 14:10:02 in reply to 4 [link] [source]

Awww. It's gone now. Can't reproduce. Argh. All tests re-enabled and re-run loads of times.

(8) By Gavin Henry (ghenry) on 2021-12-07 14:10:37 in reply to 4 [link] [source]

Ah, that's because I'm not removing the test db in the second test anymore. I commented that out. Interesting.

ERROR: files left in build directory after distclean:
./test_sentrypeer.db