Skip to content

Commit

Permalink
Make ParseIpAddress more robust
Browse files Browse the repository at this point in the history
- Let it handle null input by returning nullopt
- Convert between SPI_Firmware::IpAddress and in_addr
  • Loading branch information
JesseTG committed Sep 9, 2023
1 parent 64b187d commit 1224028
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/libretro/config/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,17 @@ std::optional<melonds::FirmwareLanguage> melonds::config::ParseLanguage(const ch
}

optional<SPI_Firmware::IpAddress> melonds::config::ParseIpAddress(const char* value) noexcept {
SPI_Firmware::IpAddress address;
if (string_is_empty(value))
return nullopt;

in_addr address;
if (inet_pton(AF_INET, value, &address) == 1) {
return address;
// Both in_addr and ip represent an IPv4 address,
// but they may have different alignment requirements.
// Better safe than sorry.
SPI_Firmware::IpAddress ip;
memcpy(&ip, &address, sizeof(address));
return ip;
}

return nullopt;
Expand Down

0 comments on commit 1224028

Please sign in to comment.