简体   繁体   中英

Initializing a struct results in Segmentation Fault

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)

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.)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM