c - Seg fault with open command when trying to open very large file -
i'm taking networking class @ school , using c/gdb first time. our assignment make webserver communicates client browser. underway , can open files , send them client. goes great till open large file , seg fault. i'm not pro @ c/gdb i'm sorry if causing me ask silly questions , not able see solution myself when looked @ dumped core see seg fault comes here:
if (-1 == (openfd = open(path, o_rdonly)))
specifically tasked opening file , sending client browser. algorithm goes:
- open/error catch
- read file buffer/error catch
- send file
we tasked making sure server doesn't crash when sending large files. problem seems opening them. can send smaller files fine. file in question 29.5mb.
the whole algorithm is:
ssize_t send_file(int conn, char *path, int len, int blksize, char *mime) { int openfd; // file descriptor file open @ path int temp; // counter size of file send char buffer[len]; // buffer read file opening len big // open file if (-1 == (openfd = open(path, o_rdonly))) { send_head(conn, "", 400, strlen(error_400)); (void) send(conn, error_400, strlen(error_400), 0); logwrite(stdout, cant_open); return -1; } // read file if (-1 == read(openfd, buffer, len)) { send_head(conn, "", 400, strlen(error_400)); (void) send(conn, error_400, strlen(error_400), 0); logwrite(stdout, cant_open); return -1; } (void) close(openfd); // send buffer logwrite(stdout, suc_req); send_head(conn, mime, 200, len); send(conn, &buffer[0], len, 0); return len; }
i dunno if fact unix/c novice. sorry if is. =( you're appreciated.
rather using variable length array, perhaps try allocated memory using malloc
.
char *buffer = malloc (len); ... free (buffer);
i did simple tests on system, , when use variable length arrays of big size (like size you're having trouble with), segfault.
Comments
Post a Comment