From d814fe935c362b751439ad02e8bdb80aa913b1a9 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 31 Jul 2024 16:52:39 +0200 Subject: [PATCH] src: account for OpenSSL unexpected version PR-URL: https://github.com/nodejs/node/pull/54038 Reviewed-By: Richard Lau Reviewed-By: Joyee Cheung Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Franziska Hinkelmann Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- src/node_metadata.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/node_metadata.cc b/src/node_metadata.cc index 361b3596b4a65c..61bc8b204c92c7 100644 --- a/src/node_metadata.cc +++ b/src/node_metadata.cc @@ -43,14 +43,23 @@ Metadata metadata; #if HAVE_OPENSSL static constexpr size_t search(const char* s, char c, size_t n = 0) { - return *s == c ? n : search(s + 1, c, n + 1); + return *s == '\0' ? n : (*s == c ? n : search(s + 1, c, n + 1)); } static inline std::string GetOpenSSLVersion() { // sample openssl version string format // for reference: "OpenSSL 1.1.0i 14 Aug 2018" const char* version = OpenSSL_version(OPENSSL_VERSION); - const size_t start = search(version, ' ') + 1; + const size_t first_space = search(version, ' '); + + // When Node.js is linked to an alternative library implementing the + // OpenSSL API e.g. BoringSSL, the version string may not match the + // expected pattern. In this case just return “0.0.0” as placeholder. + if (version[first_space] == '\0') { + return "0.0.0"; + } + + const size_t start = first_space + 1; const size_t len = search(&version[start], ' '); return std::string(version, start, len); }