Skip to content

Commit

Permalink
Fix dangling view in addr/port parsing
Browse files Browse the repository at this point in the history
With this in the config:

    [bind]
    listen=144.76.164.202:1666

I was getting a failure during config parsing:

    Dec 04 16:00:21 fenrir lokinet[2480391]: [2024-12-04 20:00:21] [+0.000s] [config:critical|/home/testnet/lokinet/llarp/config/config.cpp:1232] Parsing input: 144.76.164.202:1666
    Dec 04 16:00:21 fenrir lokinet[2480391]: [2024-12-04 20:00:21] [+0.000s] [config:critical|/home/testnet/lokinet/llarp/config/config.cpp:1234] Parsed input =
    Dec 04 16:00:21 fenrir lokinet[2480391]: [1B blob data]
    Dec 04 16:00:21 fenrir lokinet[2480391]: :1666

which turned out to be that we had a string view viewing a temporary
std::string.  This fixes it.

Also removes a counterproductive `static` from inlined header methods
around addr parsing.  (Having a `static` free function forces a distinct
copy of the function to exist for every compilation unit that uses the
function *without* linkage of those functions, while plain `inline`
still compiles separate copies where used, but allows the linker to
deduplicate them when linking the final binary).
  • Loading branch information
jagerman committed Dec 5, 2024
1 parent e09df7d commit a3ae897
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 7 additions & 7 deletions llarp/address/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace llarp
namespace detail
{
// inline auto utilcat = log::Cat("addrutils");
inline static std::optional<std::string> parse_addr_string(std::string_view arg, std::string_view tld)
inline std::optional<std::string> parse_addr_string(std::string_view arg, std::string_view tld)
{
std::optional<std::string> ret = std::nullopt;

Expand All @@ -61,22 +61,22 @@ namespace llarp
return ret;
};

inline static std::pair<std::string, uint16_t> parse_addr(
std::string_view addr, std::optional<uint16_t> default_port)
inline std::pair<std::string, uint16_t> parse_addr(std::string_view addr, std::optional<uint16_t> default_port)
{
std::pair<std::string, uint16_t> result;
auto &[host, port] = result;

if (auto p = addr.find_last_not_of("0123456789");
p != std::string_view::npos && p + 2 <= addr.size() && addr[p] == ':')
{
if (!parse_int(addr.substr(p + 1), result.second))
if (!parse_int(addr.substr(p + 1), port))
throw std::invalid_argument{"Invalid address: could not parse port"};
addr.remove_suffix(addr.size() - p);
}
else if (default_port)
{
// log::critical(utilcat, "Setting default port for addr parse!");
result.second = *default_port;
port = *default_port;
}
else
{
Expand All @@ -96,7 +96,7 @@ namespace llarp
{
if (auto q = addr.find_first_not_of("0123456789abcdef:."); q != std::string_view::npos)
throw std::invalid_argument{"Invalid address: does not look like IPv4 or IPv6!"};
else if (!had_sq_brackets)
if (!had_sq_brackets)
throw std::invalid_argument{"Invalid address: IPv6 addresses require [...] square brackets"};
}

Expand All @@ -106,7 +106,7 @@ namespace llarp
// // addr = "::";
// }

result.first = addr;
host = addr;
return result;
}

Expand Down
5 changes: 3 additions & 2 deletions llarp/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,8 @@ namespace llarp

auto parse_addr_for_link = [net_ptr](const std::string& arg, bool& given_port_only) {
std::optional<oxen::quic::Address> maybe = std::nullopt;
std::string_view arg_v{arg}, host;
std::string_view arg_v{arg};
std::string host;
uint16_t p{};

if (auto pos = arg_v.find(':'); pos != arg_v.npos)
Expand All @@ -1242,7 +1243,7 @@ namespace llarp
maybe = net_ptr->get_best_public_address(true, p);
}
else
maybe = oxen::quic::Address{std::string{host}, p};
maybe = oxen::quic::Address{host, p};

if (maybe and maybe->is_loopback())
throw std::invalid_argument{"{} is a loopback address"_format(arg)};
Expand Down

0 comments on commit a3ae897

Please sign in to comment.