Ask coding questions

← Back to all posts
Seg fault :(
h
realTronsi (901)

On line 50 in server.c I have

fd_set readfds;

which is causing a segmentation fault. fd_set macro is imported from <sys/time.h>.

--

Makefile:11: recipe for target 'all' failed
make: *** [all] Segmentation fault (core dumped)
exit status 2
@xxpertHacker no bully pls
Answered by xxpertHacker (768) [earned 5 cycles]
View Answer
Comments
hotnewtop
xxpertHacker (768)

I'm getting a realloc abortion, but I don't see it directly called anywhere... oh, nvm, line 35 server.c, realloc is failing.

You can't realloc a pointer that didn't even come from alloc!

And even then, you're not always able to realloc a pointer from alloc anyways!

(Also, doesn't realloc invalidate old pointers?)

Also, I told you that you were on your own, I can't help you out here.

Isn't server.clients garbage memory right now?

realTronsi (901)

@xxpertHacker thought line 50 was the problem since removing it stopped the seg faults.

But just realized I wrote realloc instead of calloc :|

xxpertHacker (768)

@realTronsi calloc makes so much more sense there, I thought you were just writing nonsense.

realTronsi (901)

@xxpertHacker wow great putting it in a struct leads to another seg fault :|

typedef struct Server {
	int PORT;

	int* clients;
	size_t client_t;
	int MAX_CLIENTS;

	int master_sd;
	
	fd_set readfds;
	struct sockaddr_in address;
} Server_;
xxpertHacker (768)

@realTronsi yay, go make another question so I could easily get 5 more useless internet points, lol

realTronsi (901)

@xxpertHacker just answer it in 5 different comments :D

xxpertHacker (768)

@realTronsi Then I'll want 25 useless internet points.

realTronsi (901)

@xxpertHacker that makes no sense, just answer :(

realTronsi (901)

@xxpertHacker okay answer word by word so you get #word free internet points

xxpertHacker (768)

@realTronsi did you mean to call calloc(server.client_t?

Go add this too:

CC = clang
CFLAGS = -Wall -pedantic
realTronsi (901)

@xxpertHacker wdym i'm not callocing the client_t

xxpertHacker (768)

@realTronsi Yes, that's the point.

This is bad code:

int* clients;
clients = calloc(clients, *clients);

It literally makes no sense, and it's a type error.
And it's undefined behavior.

Look at GCC's pedantic warning:

server/server.c: In function ‘create_server’:
server/server.c:35:26: warning: passing argument 1 of ‘calloc’ makes integer from pointer without a cast [-Wint-conversion]
  server.clients = calloc(server.clients, (server.client_t)*sizeof(*server.clients));
                          ^~~~~~

Simply put, that is a type error.

Next,

In file included from server/../include/server.h:5:0,
                 from server/server.c:1:
/usr/include/stdlib.h:541:14: note: expected ‘size_t {aka long unsigned int}’ but argument is of type ‘int *’
 extern void *calloc (size_t __nmemb, size_t __size)
              ^~~~~~

You're passing the address of a pointer, which is currently initialized by random memory, as a size_t.

For all you know, you're doing calloc(0x3434238935, ...).

(btw, use a code formatter)

realTronsi (901)

wait wtf lmao yeah I see

server.clients = calloc(1, (server.client_t)*sizeof(*server.clients));

idek why I put the address there, when I wrote this 2 days ago I must've been thinking of realloc and thought I calloced b4

xxpertHacker (768)

@realTronsi omg, finally you see why I saw that code as nonsense.

Also, this is why I dislike weakly-typed languages, noobs run into errors too often, and implicit casts even screw me over.

realTronsi (901)

@xxpertHacker

noobs run into errors too often

._.

idk what happened for this one but I'm not normally this autistic

xxpertHacker (768)

Why did you think line 50 was the problem?

programmeruser (521)

Well, running with GDB gives me this:

[WARNING] ~ SIFI: Master socket initalization failed

Program received signal SIGSEGV, Segmentation fault.
__GI___libc_realloc (bytes=4, oldmem=0x3) at malloc.c:3165
3165    malloc.c: No such file or directory.
programmeruser (521)

@realTronsi and here's the backtrace:

#0  __GI___libc_realloc (bytes=4, oldmem=0x3) at malloc.c:3165
#1  realloc_hook_ini (ptr=0x3, sz=4, caller=<optimized out>) at hooks.c:41
#2  0x000056441c0c9972 in create_server (PORT=80) at server/server.c:35
#3  0x000056441c0c97fb in main () at examples/main.c:9