Theldus / wsServer

wsServer - a tiny WebSocket server library written in C
https://theldus.github.io/wsServer
GNU General Public License v3.0
422 stars 80 forks source link

Creating variable "send" segfaults #59

Closed SollyBunny closed 2 years ago

SollyBunny commented 2 years ago

Using the example program if a variable is created with name "send" it will print "Waiting for incoming connections..." and once a client attempts to connect it will segfault with no other error

Theldus commented 2 years ago

Hi @SollyBunny, A variable name should not cause a SEGFAULT.

Could you provide me a minimal reproducible example where this occurs?

SollyBunny commented 2 years ago

I can no longer reproduce it, which is extremley confusing as before I could comment out and in a variable decleration (send) and it would either segfault or not.

Theldus commented 2 years ago

@SollyBunny you're actually right, but it's not wsServer's fault...

It turns out that when the static library libws.a is linked with echo.c, all symbols are resolved at that moment, and then the send(2) function (from libc) is mistakenly replaced by its variable, which is not even a function . This happens because the linker resolves undefined symbols (like send()) to the first one it finds, in this case, echo.c, instead of libc.

I managed to reproduce with the following code:

#include <ws.h>

int send;

void onopen(ws_cli_conn_t *client){}
void onclose(ws_cli_conn_t *client){}
void onmessage(ws_cli_conn_t *client,
const unsigned char *msg, uint64_t size, int type){}

int main(void)
{
    struct ws_events evs;
    evs.onopen    = &onopen;
    evs.onclose   = &onclose;
    evs.onmessage = &onmessage;
    ws_socket(&evs, 8080, 0, 1000);
    return (0);
}

But you can also reproduce this by doing:

foo.c:

int puts;

extern void do_something(void);

int main(void) {
    do_something();
    return (0);
}

bar.c:

#include <stdio.h>

void do_something(void) {
    puts("Hello, World!");
}

and then build and execute:

$ gcc foo.c bar.c -o foo
$ ./foo
Segmentation fault

So... not exactly a wsServer bug, just an unfortunate choice for your variable name, change it and it should work as expected.

That said, I'll keep the issue closed, but don't hesitate to create new issues if you wish =).