From 8625eed9103031a5ab48f261ce7c50ac534a6e18 Mon Sep 17 00:00:00 2001 From: Francesco Chemolli <5175948+kinkie@users.noreply.github.com> Date: Fri, 26 Apr 2024 13:14:19 +0000 Subject: [PATCH] NoNewGlobals for ProxyProtocol::*::Magic (#1791) Detected by Coverity. CID 1554652: Initialization or destruction ordering is unspecified (GLOBAL_INIT_ORDER). --- src/proxyp/Parser.cc | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/proxyp/Parser.cc b/src/proxyp/Parser.cc index 0e9b2c94ea..4ba40f206b 100644 --- a/src/proxyp/Parser.cc +++ b/src/proxyp/Parser.cc @@ -30,7 +30,12 @@ namespace ProxyProtocol { namespace One { /// magic octet prefix for PROXY protocol version 1 -static const SBuf Magic("PROXY", 5); +static const auto & +Magic() +{ + static const auto magic = new SBuf("PROXY", 5); + return *magic; +} /// extracts PROXY protocol v1 header from the given buffer static Parsed Parse(const SBuf &buf); @@ -41,7 +46,12 @@ static void ParseAddresses(Parser::Tokenizer &tok, Header::Pointer &header); namespace Two { /// magic octet prefix for PROXY protocol version 2 -static const SBuf Magic("\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12); +static const auto & +Magic() +{ + static const auto magic = new SBuf("\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12); + return *magic; +} /// extracts PROXY protocol v2 header from the given buffer static Parsed Parse(const SBuf &buf); @@ -115,7 +125,7 @@ ProxyProtocol::One::Parse(const SBuf &buf) Parser::Tokenizer tok(buf); static const SBuf::size_type maxHeaderLength = 107; // including CRLF - static const auto maxInteriorLength = maxHeaderLength - Magic.length() - 2; + static const auto maxInteriorLength = maxHeaderLength - Magic().length() - 2; static const auto interiorChars = CharacterSet::CR.complement().rename("non-CR"); SBuf interior; @@ -244,8 +254,8 @@ ProxyProtocol::Parse(const SBuf &buf) Parser::Tokenizer magicTok(buf); const auto parser = - magicTok.skip(Two::Magic) ? &Two::Parse : - magicTok.skip(One::Magic) ? &One::Parse : + magicTok.skip(Two::Magic()) ? &Two::Parse : + magicTok.skip(One::Magic()) ? &One::Parse : nullptr; if (parser) { @@ -254,7 +264,7 @@ ProxyProtocol::Parse(const SBuf &buf) } // detect and terminate other protocols - if (buf.length() >= Two::Magic.length()) { + if (buf.length() >= Two::Magic().length()) { // PROXY/1.0 magic is shorter, so we know that // the input does not start with any PROXY magic throw TexcHere("PROXY protocol error: invalid magic"); -- 2.47.2