From 0fde979b21a01983e4ce1b3d10be8c291ffe64dd Mon Sep 17 00:00:00 2001 From: Rubidium Date: Tue, 11 Mar 2025 23:33:32 +0100 Subject: [PATCH] Codefix: check for errors in the function getting the socket error --- src/network/core/os_abstraction.cpp | 5 +++-- src/network/core/os_abstraction.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/network/core/os_abstraction.cpp b/src/network/core/os_abstraction.cpp index 41d5471b72..238f368f6e 100644 --- a/src/network/core/os_abstraction.cpp +++ b/src/network/core/os_abstraction.cpp @@ -27,8 +27,9 @@ /** * Construct the network error with the given error code. * @param error The error code. + * @param message The error message. Leave empty to determine this automatically based on the error number. */ -NetworkError::NetworkError(int error) : error(error) +NetworkError::NetworkError(int error, const std::string &message) : error(error), message(message) { } @@ -185,7 +186,7 @@ NetworkError GetSocketError(SOCKET d) { int err; socklen_t len = sizeof(err); - getsockopt(d, SOL_SOCKET, SO_ERROR, (char *)&err, &len); + if (getsockopt(d, SOL_SOCKET, SO_ERROR, (char *)&err, &len) != 0) return NetworkError(-1, "Could not get error for socket"); return NetworkError(err); } diff --git a/src/network/core/os_abstraction.h b/src/network/core/os_abstraction.h index cc35c01436..86c37db105 100644 --- a/src/network/core/os_abstraction.h +++ b/src/network/core/os_abstraction.h @@ -23,7 +23,7 @@ private: int error; ///< The underlying error number from errno or WSAGetLastError. mutable std::string message; ///< The string representation of the error (set on first call to #AsString). public: - NetworkError(int error); + NetworkError(int error, const std::string &message = {}); bool HasError() const; bool WouldBlock() const;