Skip to content

Commit

Permalink
Additional socket checks for socket inet implementations (project-chi…
Browse files Browse the repository at this point in the history
…p#35674)

* Additional socket checks for socket inet implementations

* Self-review update

* One more extra check to make clang-tidy happy

* Switch to nolint

* Avoid large code deltas

* Restyle

* Restyled by clang-format

---------

Co-authored-by: Andrei Litvin <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored and yyzhong-g committed Dec 11, 2024
1 parent f0d8120 commit a156161
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/inet/TCPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ CHIP_ERROR TCPEndPointImplSockets::BindImpl(IPAddressType addrType, const IPAddr

if (res == CHIP_NO_ERROR)
{
// NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions): GetSocket calls ensure mSocket is valid
if (bind(mSocket, &sa.any, sockaddrsize) != 0)
{
res = CHIP_ERROR_POSIX(errno);
Expand Down Expand Up @@ -248,6 +249,7 @@ CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t
return INET_ERROR_WRONG_ADDRESS_TYPE;
}

// NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions): GetSocket calls ensure mSocket is valid
int conRes = connect(mSocket, &sa.any, sockaddrsize);

if (conRes == -1 && errno != EINPROGRESS)
Expand Down
5 changes: 5 additions & 0 deletions src/inet/UDPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ CHIP_ERROR IPv6Bind(int socket, const IPAddress & address, uint16_t port, Interf
sa.sin6_scope_id = static_cast<decltype(sa.sin6_scope_id)>(interfaceId);

CHIP_ERROR status = CHIP_NO_ERROR;

// NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions): Function called only with valid socket after GetSocket
if (bind(socket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
{
status = CHIP_ERROR_POSIX(errno);
Expand Down Expand Up @@ -139,6 +141,8 @@ CHIP_ERROR IPv4Bind(int socket, const IPAddress & address, uint16_t port)
sa.sin_addr = address.ToIPv4();

CHIP_ERROR status = CHIP_NO_ERROR;

// NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions): Function called only with valid socket after GetSocket
if (bind(socket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
{
status = CHIP_ERROR_POSIX(errno);
Expand Down Expand Up @@ -410,6 +414,7 @@ CHIP_ERROR UDPEndPointImplSockets::SendMsgImpl(const IPPacketInfo * aPktInfo, Sy
#endif // INET_CONFIG_UDP_SOCKET_PKTINFO

// Send IP packet.
// NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions): GetSocket calls ensure mSocket is valid
const ssize_t lenSent = sendmsg(mSocket, &msgHeader, 0);
if (lenSent == -1)
{
Expand Down

0 comments on commit a156161

Please sign in to comment.