From 6266976e7a88046ebd2afc41fac19aa6e6b491cc Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 24 Jul 2023 17:48:34 -0600 Subject: [PATCH] LibTLS: Move singleton for DefaultRootCACertificates out of line This follows the pattern of every other singleton in the system. Also, remove use of AK::Singleton in place of a function-scope static. There are only three uses of that class outside of the Kernel, and all the remaining uses are suspect. We need it in the Kernel because we want to avoid global destructors to prevent nasty surprises about expected lifetimes of objects. In Userland, we have normal thread-safe statics available. 7d11edbe1 attempted to standardize the pattern, but it seems like more uses of awkward singleton creation have crept in or were missed back then. As a bonus, this fixes a linker error on macOS with -g -O0 for Lagom WebContent. --- Userland/Libraries/LibTLS/Certificate.h | 5 +---- Userland/Libraries/LibTLS/TLSv12.cpp | 7 ++++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibTLS/Certificate.h b/Userland/Libraries/LibTLS/Certificate.h index ae2408e81d..cfd931e8c9 100644 --- a/Userland/Libraries/LibTLS/Certificate.h +++ b/Userland/Libraries/LibTLS/Certificate.h @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -295,11 +294,9 @@ public: static ErrorOr> parse_pem_root_certificate_authorities(ByteBuffer&); static ErrorOr> load_certificates(); - static DefaultRootCACertificates& the() { return s_the; } + static DefaultRootCACertificates& the(); private: - static Singleton s_the; - Vector m_ca_certificates; }; diff --git a/Userland/Libraries/LibTLS/TLSv12.cpp b/Userland/Libraries/LibTLS/TLSv12.cpp index 68ede57755..5d7a94666d 100644 --- a/Userland/Libraries/LibTLS/TLSv12.cpp +++ b/Userland/Libraries/LibTLS/TLSv12.cpp @@ -489,7 +489,6 @@ Vector TLSv12::parse_pem_certificate(ReadonlyBytes certificate_pem_ return { move(certificate) }; } -Singleton DefaultRootCACertificates::s_the; DefaultRootCACertificates::DefaultRootCACertificates() { auto load_result = load_certificates(); @@ -501,6 +500,12 @@ DefaultRootCACertificates::DefaultRootCACertificates() m_ca_certificates = load_result.release_value(); } +DefaultRootCACertificates& DefaultRootCACertificates::the() +{ + static DefaultRootCACertificates s_the; + return s_the; +} + ErrorOr> DefaultRootCACertificates::load_certificates() { auto cacert_file = TRY(Core::File::open("/etc/cacert.pem"sv, Core::File::OpenMode::Read));