c3lang / c3c

Compiler for the C3 language

Home Page:https://c3-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Windows's "Socket.read" causes crash

velikoss opened this issue · comments

When net library use Socket.read function on Windows, it causes the application to crash. This problem is not present on Linux nor macOS.

Steps to Reproduce

  1. Clone c3-simple-http.
  2. Start the server on Windows.
  3. Send a GET request to the server.

Expected Behavior

The server should handle the GET request and respond appropriately.

Actual Behavior

The server crashes with a stack buffer overrun or stack overflow error.

Error Messages and Logs

  1. STATUS_STACK_BUFFER_OVERRUN
  2. STATUS_STACK_OVERFLOW
  3. The application stops working when a new connection is established.

Some Patches done to prevent this issue

  1. The accept function in tcp::accept fails silently due to incorrect parameter initialization.

    • Adding socket.ai_addrlen = socket.ai_addr_storage.len; before os::accept line fixes the issue
  2. Patches in libc

extern fn isz recv(int sockFd, void* buf, usz nbyte, int flags) @if(!env::WIN32);
extern fn int recv(Win32_SOCKET s, char* buf, int len, int flags) @if(env::WIN32);
  1. Patched Socket.read
fn usz! Socket.read(&self, char[] bytes) @dynamic @if(env::WIN32)
 {
     int n = libc::recv((Win32_SOCKET)self.sock, bytes.ptr, bytes.len, 0);
     if (n < 0) return NetError.READ_FAILED?;
     return (usz)n;
 }

Please try the latest master, it has these patches and a little more.

Guess issue is fixed by now.