From 8f94e8b94315d17d9316146b5b3c5ae64184432f Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Fri, 6 Jan 2023 15:09:55 +0100 Subject: [PATCH] comm: make sure that our version check is reliable Rework the logic of the version check used in the database migration, and make sure that it is full functional to avoid confusion at release time. Changelog-Fixed: database: Correctly identity official release versions for database upgrade. Reported-by: @urza Signed-off-by: Vincenzo Palazzo --- common/test/Makefile | 7 +++++++ common/test/run-version.c | 16 ++++++++++++++++ common/version.c | 13 +++++++++++++ common/version.h | 5 +++++ wallet/db.c | 8 -------- 5 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 common/test/run-version.c diff --git a/common/test/Makefile b/common/test/Makefile index 3304f2f5f..2df9e1284 100644 --- a/common/test/Makefile +++ b/common/test/Makefile @@ -90,4 +90,11 @@ common/test/run-bolt12_merkle-json: \ common/base32.o \ common/wireaddr.o + +common/test/run-version: \ + common/amount.o \ + wire/fromwire.o \ + wire/towire.o + + check-units: $(COMMON_TEST_PROGRAMS:%=unittest/%) diff --git a/common/test/run-version.c b/common/test/run-version.c new file mode 100644 index 000000000..0e04b4cb0 --- /dev/null +++ b/common/test/run-version.c @@ -0,0 +1,16 @@ +#include "config.h" +#include "../version.c" +#include +#include +#include + +int main(int argc, char *argv[]) +{ + common_setup(argv[0]); + + assert(cmp_release_version("v22.11")); + assert(cmp_release_version("v22.11.1")); + assert(cmp_release_version("v22.11.1-6-gdf29990-modded") == false); + + common_shutdown(); +} diff --git a/common/version.c b/common/version.c index e64bc2595..4fc20b666 100644 --- a/common/version.c +++ b/common/version.c @@ -3,6 +3,7 @@ #include #include #include +#include /* Only common/version.c can safely include this. */ # include "version_gen.h" @@ -20,3 +21,15 @@ char *version_and_exit(const void *unused UNUSED) } exit(0); } + +static bool cmp_release_version(const char *version) { + if (version[0] != 'v') + return false; + return strspn(version+1, ".0123456789") == strlen(version+1); +} + +/* Released versions are of form v[year].[month]?(.patch)* */ +bool is_released_version(void) +{ + return cmp_release_version(version()); +} diff --git a/common/version.h b/common/version.h index b2db426df..90b7c824d 100644 --- a/common/version.h +++ b/common/version.h @@ -1,9 +1,14 @@ #ifndef LIGHTNING_COMMON_VERSION_H #define LIGHTNING_COMMON_VERSION_H #include "config.h" +#include char *version_and_exit(const void *unused); const char *version(void); +/* check if the current version is a release version. + * + * Released versions are of form v[year].[month]?(.patch)* */ +bool is_released_version(void); #define opt_register_version() \ opt_register_early_noarg("--version|-V", version_and_exit, NULL, \ diff --git a/wallet/db.c b/wallet/db.c index 5286d8cc1..4a45167f5 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -945,14 +945,6 @@ static struct migration dbmigrations[] = { /* FIXME: Remove payments local_offer_id column! */ }; -/* Released versions are of form v{num}[.{num}]* */ -static bool is_released_version(void) -{ - if (version()[0] != 'v') - return false; - return strcspn(version()+1, ".0123456789") == strlen(version()+1); -} - /** * db_migrate - Apply all remaining migrations from the current version */