1

I have a simple struct as follows:

typedef struct {
  char *raw_headers;
  char headers[128][512][512];
} HTTPrequest;

Now in the same file I have a function as follows:

void init_request(char *raw_headers) {
  HTTPrequest request;
  request.raw_headers = raw_headers;
}

This results in a Segmentation Fault when running the output binary.

I compile the file, as

$ gcc Server.c -o Builds/debug

And, I run the executable as,

$ ./Builds/debug

This is my original file as requested:

Parser.h

typedef struct {
  char *raw_headers;
  char headers[128][512][512];
} HTTPrequest;

void parser_init(char *raw_headers) {
  char *token, *key_value = NULL;
  token = strtok(raw_headers, "\r\n");

  int line_counter = 1
  HTTPrequest request;
  request.raw_headers = raw_headers;

  while (token) {
    char *line = token;

    if(line_counter != 1) {
    }

    token = strtok(NULL, "\r\n");
    line_counter++;
  }
}

Server.h

int socket_create() {
  return socket(AF_INET, SOCK_STREAM, 0);
}

void infinite_loop(int socketFD) {
  char buffer[1024];
  memset(buffer, 0, sizeof(buffer));

  printf("Starting infinite loop, don't worry, everything would be okay!\n");
  do {
    int connectionFD = accept(socketFD, (struct sockaddr*) NULL, NULL);
    /*Accept is a blocking call! The following code wont execute until, accept() returns.*/
    strcpy(buffer, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello!");
    write(connectionFD, buffer, sizeof(buffer));

    char request[2048];
    memset(&request, 0, sizeof(request));
    read(connectionFD, &request, sizeof(request));

    printf("Request received!\n");
    // Init the parser.
    parser_init(request);

    close(connectionFD);
  } while (true);
}

Server.c

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <stdbool.h>
#include "Parser.h"
#include "Server.h"

void main() {
  struct sockaddr_in listeningAddr;

  int socketFD = socket_create();

  memset(&listeningAddr, 0, sizeof(listeningAddr));

  listeningAddr.sin_family = AF_INET;
  listeningAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  listeningAddr.sin_port = htons(8070);

  bind(socketFD, (struct sockaddr*) &listeningAddr, sizeof(listeningAddr));

  if(listen(socketFD, 5) == -1) {
    printf("Woah there! I couldn't startup the server!");
  }

  infinite_loop(socketFD);
}

Also, the error message: Segmentation fault (core dumped)

7
  • 2
    >1k rep and not able to provide a minimal complete example? :/ Commented Sep 18, 2017 at 8:03
  • My bad, I forgot to write the argument while writing the question :P Commented Sep 18, 2017 at 8:04
  • 1
    Don't make up the code for the question as you go along. Create the minimal reproducible example first. Only then post. Commented Sep 18, 2017 at 8:05
  • @StoryTeller I actually had the file already ready, just wanted to write since only to put the relevant part in here :P Commented Sep 18, 2017 at 8:07
  • Please read the link StoryTeller provided. Your code is not complete or verifiable. Commented Sep 18, 2017 at 8:09

3 Answers 3

2

The headers member (32MiB) makes your struct too big to fit on most default system-provided stacks (8MiB on Linux).

Make it smaller, and trivial MCVEs such as:

typedef struct {
  char *raw_headers;
  char headers/*[128]*/[512][512]; }
HTTPrequest;

void init_request(char *raw_headers) {
  HTTPrequest request;
  request.raw_headers = raw_headers;
}

int main()
{
    init_request("hello, world");
}

will work, although, initializing an on-stack struct only to have it discarded by the function return is not very meaningful

(Initializer functions will usually take a pointer to the struct they're initializing and initialize the object through that pointer.)

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, makes sense!
1

Although your HTTPrequest is simple, it is over 32MB in size. You most probably encounter a stack overflow...

Comments

1

That's a typical case of Stack overflow!

The reason is that your struct HTTPrequest is too big, more than 32 MB. The 3D array has a size of 128 * 512 * 512 = 33554432 bytes, since it's of type char.

In any case, 3D arrays are barely used, and only in special cases. Reconsider your design and try to make that array a 2D instead of a 3D.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.