Skip to content

Commit

Permalink
NoNewGlobals for ProxyProtocol::*::Magic (#1791)
Browse files Browse the repository at this point in the history
Detected by Coverity. CID 1554652: Initialization or destruction
ordering is unspecified (GLOBAL_INIT_ORDER).
  • Loading branch information
kinkie authored and squid-anubis committed Apr 26, 2024
1 parent 2a580c1 commit 8625eed
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/proxyp/Parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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");
Expand Down

0 comments on commit 8625eed

Please sign in to comment.