Skip to content

Commit 2e6765c

Browse files
committed
init: Sanity check LevelDB build/runtime versions
1 parent 2080d46 commit 2e6765c

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/dbwrapper.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66

77
#include <logging.h>
88
#include <random.h>
9+
#include <node/interface_ui.h>
910
#include <serialize.h>
1011
#include <span.h>
1112
#include <streams.h>
1213
#include <util/fs.h>
1314
#include <util/fs_helpers.h>
1415
#include <util/strencodings.h>
16+
#include <util/translation.h>
1517

1618
#include <algorithm>
1719
#include <cassert>
1820
#include <cstdarg>
1921
#include <cstdint>
2022
#include <cstdio>
23+
#include <leveldb/c.h>
2124
#include <leveldb/cache.h>
2225
#include <leveldb/db.h>
2326
#include <leveldb/env.h>
@@ -51,6 +54,22 @@ static void HandleError(const leveldb::Status& status)
5154
throw dbwrapper_error(errmsg);
5255
}
5356

57+
bool dbwrapper_SanityCheck()
58+
{
59+
unsigned long header_version = (leveldb::kMajorVersion << 16) | leveldb::kMinorVersion;
60+
unsigned long library_version = (leveldb_major_version() << 16) | leveldb_minor_version();
61+
62+
if (header_version != library_version) {
63+
InitError(Untranslated(strprintf("Compiled with LevelDB %d.%d, but linked with LevelDB %d.%d (incompatible).",
64+
leveldb::kMajorVersion, leveldb::kMinorVersion,
65+
leveldb_major_version(), leveldb_minor_version()
66+
)));
67+
return false;
68+
}
69+
70+
return true;
71+
}
72+
5473
class CBitcoinLevelDBLogger : public leveldb::Logger {
5574
public:
5675
// This code is adapted from posix_logger.h, which is why it is using vsprintf.

src/dbwrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <string>
2121
#include <vector>
2222

23+
bool dbwrapper_SanityCheck();
24+
2325
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
2426
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
2527

src/kernel/checks.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <kernel/checks.h>
66

7+
#include <dbwrapper.h>
78
#include <random.h>
89
#include <util/result.h>
910
#include <util/translation.h>
@@ -14,6 +15,10 @@ namespace kernel {
1415

1516
util::Result<void> SanityChecks(const Context&)
1617
{
18+
if (!dbwrapper_SanityCheck()) {
19+
return util::Error{Untranslated("Database sanity check failure. Aborting.")};
20+
}
21+
1722
if (!Random_SanityCheck()) {
1823
return util::Error{Untranslated("OS cryptographic RNG sanity check failure. Aborting.")};
1924
}

0 commit comments

Comments
 (0)