Skip to content

Commit

Permalink
Coverity. Make global variables a singleton. (#11908)
Browse files Browse the repository at this point in the history
This is an attempt to fix some ordering creation issues for some global variables.
The whole idea is to get swoc::bwf::Global_Names initialized first.
  • Loading branch information
brbzull0 authored Jan 15, 2025
1 parent e1f49f6 commit 5ded0e4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
10 changes: 5 additions & 5 deletions lib/swoc/include/swoc/bwf_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ template <typename T> class ContextNames : public NameMap<BufferWriter &(BufferW
* This nameset is used if no other is provided. Therefore bindings added to this nameset will be
* available in the default formatting use.
*/
extern ExternalNames Global_Names;
extern ExternalNames& Global_Names();

// --------------- Implementation --------------------
/// --- Spec ---
Expand Down Expand Up @@ -919,25 +919,25 @@ BufferWriter::print_nfv(Binding &&names, Extractor &&ex, bwf::ArgPack const &arg
template <typename... Args>
BufferWriter &
BufferWriter::print(const TextView &fmt, Args &&...args) {
return this->print_nfv(bwf::Global_Names.bind(), bwf::Format::bind(fmt), bwf::ArgTuple{std::forward_as_tuple(args...)});
return this->print_nfv(bwf::Global_Names().bind(), bwf::Format::bind(fmt), bwf::ArgTuple{std::forward_as_tuple(args...)});
}

template <typename... Args>
BufferWriter &
BufferWriter::print(bwf::Format const &fmt, Args &&...args) {
return this->print_nfv(bwf::Global_Names.bind(), fmt.bind(), bwf::ArgTuple{std::forward_as_tuple(args...)});
return this->print_nfv(bwf::Global_Names().bind(), fmt.bind(), bwf::ArgTuple{std::forward_as_tuple(args...)});
}

template <typename... Args>
BufferWriter &
BufferWriter::print_v(TextView const &fmt, std::tuple<Args...> const &args) {
return this->print_nfv(bwf::Global_Names.bind(), bwf::Format::bind(fmt), bwf::ArgTuple{args});
return this->print_nfv(bwf::Global_Names().bind(), bwf::Format::bind(fmt), bwf::ArgTuple{args});
}

template <typename... Args>
BufferWriter &
BufferWriter::print_v(const bwf::Format &fmt, const std::tuple<Args...> &args) {
return this->print_nfv(bwf::Global_Names.bind(), fmt.bind(), bwf::ArgTuple{args});
return this->print_nfv(bwf::Global_Names().bind(), fmt.bind(), bwf::ArgTuple{args});
}

template <typename Binding, typename Extractor>
Expand Down
6 changes: 5 additions & 1 deletion lib/swoc/src/bw_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
using namespace std::literals;
using namespace swoc::literals;

swoc::bwf::ExternalNames swoc::bwf::Global_Names;

using swoc::svto_radix;

namespace swoc { inline namespace SWOC_VERSION_NS {

namespace bwf {
ExternalNames& Global_Names() {
static swoc::bwf::ExternalNames Global_Names;
return Global_Names;
}

const Spec Spec::DEFAULT;

Expand Down
8 changes: 4 additions & 4 deletions lib/swoc/unit_tests/ex_bw_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ struct Context {

void
EX_BWF_Format_Init() {
swoc::bwf::Global_Names.assign("timestamp", &BWF_Timestamp);
swoc::bwf::Global_Names.assign("now", &BWF_Now);
swoc::bwf::Global_Names.assign("version", &BWF_Version);
swoc::bwf::Global_Names.assign("dave", &BWF_EvilDave);
swoc::bwf::Global_Names().assign("timestamp", &BWF_Timestamp);
swoc::bwf::Global_Names().assign("now", &BWF_Now);
swoc::bwf::Global_Names().assign("version", &BWF_Version);
swoc::bwf::Global_Names().assign("dave", &BWF_EvilDave);
}

// Work with external / global names.
Expand Down
4 changes: 2 additions & 2 deletions src/proxy/http/HttpProxyServerMain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ bool et_udp_threads_ready = false;

// File / process scope initializations
static bool HTTP_SERVER_INITIALIZED __attribute__((unused)) = []() -> bool {
swoc::bwf::Global_Names.assign("ts-thread", [](swoc::BufferWriter &w, swoc::bwf::Spec const &spec) -> swoc::BufferWriter & {
swoc::bwf::Global_Names().assign("ts-thread", [](swoc::BufferWriter &w, swoc::bwf::Spec const &spec) -> swoc::BufferWriter & {
return bwformat(w, spec, this_thread());
});
swoc::bwf::Global_Names.assign("ts-ethread", [](swoc::BufferWriter &w, swoc::bwf::Spec const &spec) -> swoc::BufferWriter & {
swoc::bwf::Global_Names().assign("ts-ethread", [](swoc::BufferWriter &w, swoc::bwf::Spec const &spec) -> swoc::BufferWriter & {
return bwformat(w, spec, this_ethread());
});
return true;
Expand Down

0 comments on commit 5ded0e4

Please sign in to comment.