Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Neon to return meaningful socket errors on Windows during socket.recv() calls. #445

Merged
merged 1 commit into from
Feb 13, 2024

Conversation

gitlarryf
Copy link
Contributor

Fix net.neon to return meaningful errors on recv()

src/socketx.h Outdated
@@ -3,6 +3,7 @@
#include <winsock.h>
typedef int socklen_t;
#pragma warning(disable: 4127) // incompatible with FD_SET()
#define socketerror WSAGetLastError()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use #define socketerror() so the use of this doesn't look like a variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been fixed.

src/socketx.h Outdated
@@ -15,5 +16,6 @@ typedef int socklen_t;
typedef int SOCKET;
const int INVALID_SOCKET = -1;
inline void closesocket(int x) { close(x); }
#define socketerror errno
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use #define socketerror() so the use of this doesn't look like a variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this has not been fixed.

lib/net.cpp Outdated
sizeof(msgbuf),
NULL);
#else
snprintf(msgbuf, sizeof(msgbuf), "%s", strerror(err));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid an unnecessary copy, this case can be simply return strerror(err). You should be aware of default constructors and how it is unnecessary to explicitly call std::string(...) for character buffers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

lib/net.cpp Outdated
{
static char msgbuf[256] = { '\0' };
#ifdef _WIN32
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of FormatMessage is not checked here, and it should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

lib/net.cpp Outdated
#else
snprintf(msgbuf, sizeof(msgbuf), "%s", strerror(err));
#endif
if (msgbuf[0] == '\0') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a redundant test, as I can see no case where the error message function succeeds but still returns an empty message. Instead, check for success of FormatMessage. (Note that strerror() has no error return value and is documented to always return a valid string.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed unnecessary check, and simplified the logic.

src/socketx.h Outdated
@@ -3,6 +3,7 @@
#include <winsock.h>
typedef int socklen_t;
#pragma warning(disable: 4127) // incompatible with FD_SET()
#define socketerror WSAGetLastError()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been fixed.

src/socketx.h Outdated
@@ -15,5 +16,6 @@ typedef int socklen_t;
typedef int SOCKET;
const int INVALID_SOCKET = -1;
inline void closesocket(int x) { close(x); }
#define socketerror errno
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this has not been fixed.

@gitlarryf gitlarryf force-pushed the win32-socket-code branch 2 times, most recently from 5fa1f81 to d03a469 Compare February 3, 2024 05:22
lib/net.cpp Outdated
@@ -15,6 +15,25 @@

// https://wiki.openssl.org/index.php/SSL/TLS_Client

static std::string socket_error(int err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to have something named socketerror and something else named socket_error. Please find names that are more different.

Copy link
Contributor Author

@gitlarryf gitlarryf Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to error_to_string(), and the macro to GetSocketError().

lib/net.cpp Outdated
static std::string error_to_string(int err)
{
#ifdef _WIN32
static char msgbuf[256] = { '\0' };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this declared static? It doesn't need to be static.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed static.

src/socketx.h Outdated
@@ -15,5 +16,6 @@ typedef int socklen_t;
typedef int SOCKET;
const int INVALID_SOCKET = -1;
inline void closesocket(int x) { close(x); }
#define GetSocketError() errno
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming convention is inconsistent. Nothing else in the socket library uses CamelCase. Choose something consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed name to socket_error()

lib/net.cpp Outdated
@@ -15,6 +15,25 @@

// https://wiki.openssl.org/index.php/SSL/TLS_Client

static std::string error_to_string(int err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is too generic - what kind of error? This is specifically related to sockets, so socket should be mentioned in the name somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed name to socket_error_message()

@ghewgill ghewgill merged commit a041415 into ghewgill:main Feb 13, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants