Well, that really excites me. I have the following function that just reads the input and returns a string
unsigned char* readFromIn() { unsigned char* text = malloc(1024); if (fgets(text, 1024, stdin) != NULL) { <--This is what causing segmentation fault int textLen = strlen(text); if (textLen > 0 && text[textLen - 1] == '\n') text[textLen - 1] = '\0';
The fact is that this function is not called anywhere and just for confirmation, I changed the name of the function to something crazy like 9rawiohawr90awrhiokawrioawr and put the printf statement at the top of the function.
I'm really not sure why an unconfirmed function can cause a segmentation error.
I am using gcc 4.6.3 on ubuntu.
Edit: I know the string
if (fgets(text, 1024, stdin) != NULL) {
is violating the code because, as soon as I comment on this conditional, segmentation errors do not occur.
I know that the NOT function is called because I do not see the output from the debug printf statement that I put.
Edit2: I tried changing the type from unsigned char to char. Another segmentation error. I will try to get gdb output.
Edit3: backdrace gdb created the following
#0 0xb7fa5ac2 in _IO_2_1_stdin_ () from /lib/i386-linux-gnu/libc.so.6
The execution of the frame 0,1,2 does not display anything interesting in particular.
Edit4: I tried all the sentences in the comment, but to no avail, I still get the same segmentation error.
So, I installed a new copy of Ubuntu on the virtual OS and recompiled my code. However, the same problem occurs. It seems to me that the problem is that there is some kind of ambiguity in my code or in the library itself. I created a minimal example demonstrating the problem:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <libwebsockets.h> unsigned char* readFromIn() { unsigned char* text = malloc(1024); if (fgets(text, 1024, stdin) != NULL) { <--SEGMENTATION FAULT HERE int textLen = strlen(text); if (textLen > 0 && text[textLen - 1] == '\n') text[textLen - 1] = '\0'; return text; } else { free(text); return NULL; } } int callback_http(struct libwebsocket_context *context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) { return 0; } static struct libwebsocket_protocols protocols[] = { /* first protocol must always be HTTP handler */ { "http-only", // name callback_http, // callback 0 // per_session_data_size } }; int main(void) { printf("Initializing Web Server\n"); // server url will be http://localhost:8081 int port = 8081; const char *interface = NULL; struct libwebsocket_context *context; // we're not using ssl const char *cert_path = NULL; const char *key_path = NULL; // no special options int opts = 0; struct lws_context_creation_info info; memset(&info, 0, sizeof info); info.port = port; info.iface = interface; info.protocols = protocols; info.extensions = libwebsocket_get_internal_extensions(); info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; info.gid = -1; info.uid = -1; info.options = opts; context = libwebsocket_create_context(&info); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return 0; } printf("starting server...\n"); while (1) { libwebsocket_service(context, 50); } printf("Shutting server down...\n"); libwebsocket_context_destroy(context); return 0; }
And this is how I compiled my code
gcc -g testbug.c -o test -lwebsockets
Here is the library that I use
http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/tag/?id=v1.23-chrome32-firefox24
You will see that I do not call the readFromIn () function, but a segmentation error occurs as soon as you try to run the executable.
I restarted gdb and this time backtrace and frames tell me a bit more information.
(gdb) run Starting program: /home/l46kok/Desktop/websocketserver/test Initializing Web Server [1384002761:2270] NOTICE: Initial logging level 7 [1384002761:2270] NOTICE: Library version: 1.3 unknown-build-hash [1384002761:2271] NOTICE: Started with daemon pid 0 [1384002761:2271] NOTICE: static allocation: 4448 + (12 x 1024 fds) = 16736 bytes [1384002761:2271] NOTICE: canonical_hostname = ubuntu [1384002761:2271] NOTICE: Compiled with OpenSSL support [1384002761:2271] NOTICE: Using non-SSL mode [1384002761:2271] NOTICE: per-conn mem: 124 + 1360 headers + protocol rx buf [1384002761:2294] NOTICE: Listening on port 8081 Program received signal SIGSEGV, Segmentation fault. 0xb7fb1ac0 in _IO_2_1_stdin_ () from /lib/i386-linux-gnu/libc.so.6 (gdb) backtrace
So yes .. I think I gave all the information at hand. But I'm really not sure what the problem is. The program causes a segmentation error on line 483, but the problem disappeared when I comment on the calling function that is not being called.