diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-02-06 11:02:41 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-02-06 11:05:07 -0500 |
commit | a77a366b87fb04a878392284241fe72850b4dd88 (patch) | |
tree | 193a9a42c2627278583f92233381a2688debbc9b /src/common/compress_zstd.c | |
parent | f98cb5d3552666ede73137d998162094d5a31a1a (diff) | |
download | tor-a77a366b87fb04a878392284241fe72850b4dd88.tar.gz tor-a77a366b87fb04a878392284241fe72850b4dd88.zip |
Warn on zstd header/library version mismatch
If we're going to potentially degrade performance in this case, we
may as well tell people so.
Diffstat (limited to 'src/common/compress_zstd.c')
-rw-r--r-- | src/common/compress_zstd.c | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/src/common/compress_zstd.c b/src/common/compress_zstd.c index 02469ced9e..96906efb9b 100644 --- a/src/common/compress_zstd.c +++ b/src/common/compress_zstd.c @@ -58,21 +58,29 @@ tor_zstd_method_supported(void) #endif } +/** Format a zstd version number as a string in <b>buf</b>. */ +static void +tor_zstd_format_version(char *buf, size_t buflen, unsigned version_number) +{ + tor_snprintf(buf, buflen, + "%u.%u.%u", + version_number / 10000 % 100, + version_number / 100 % 100, + version_number % 100); +} + +#define VERSION_STR_MAX_LEN 16 /* more than enough space for 99.99.99 */ + /** Return a string representation of the version of the currently running * version of libzstd. Returns NULL if Zstandard is unsupported. */ const char * tor_zstd_get_version_str(void) { #ifdef HAVE_ZSTD - static char version_str[16]; - size_t version_number; + static char version_str[VERSION_STR_MAX_LEN]; - version_number = ZSTD_versionNumber(); - tor_snprintf(version_str, sizeof(version_str), - "%d.%d.%d", - (int) version_number / 10000 % 100, - (int) version_number / 100 % 100, - (int) version_number % 100); + tor_zstd_format_version(version_str, sizeof(version_str), + ZSTD_versionNumber()); return version_str; #else /* !(defined(HAVE_ZSTD)) */ @@ -487,6 +495,27 @@ tor_zstd_init(void) atomic_counter_init(&total_zstd_allocation); } +/** Warn if the header and library versions don't match. */ +void +tor_zstd_warn_if_version_mismatched(void) +{ +#ifdef HAVE_ZSTD + if (! tor_zstd_can_use_static_apis()) { + char header_version[VERSION_STR_MAX_LEN]; + char runtime_version[VERSION_STR_MAX_LEN]; + tor_zstd_format_version(header_version, sizeof(header_version), + ZSTD_VERSION_NUMBER); + tor_zstd_format_version(runtime_version, sizeof(runtime_version), + ZSTD_versionNumber()); + + log_warn(LD_GENERAL, + "Tor was compiled with zstd %s, but is running with zstd %s. " + "For safety, we'll avoid using advanced zstd functionality.", + header_version, runtime_version); + } +#endif +} + #ifdef TOR_UNIT_TESTS /** Testing only: disable usage of static-only APIs, so we can make sure that * we still work without them. */ @@ -496,3 +525,4 @@ tor_zstd_set_static_apis_disabled_for_testing(int disabled) static_apis_disable_for_testing = disabled; } #endif + |