Skip to content

Commit

Permalink
Fixed possible segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Feb 27, 2024
1 parent cebc00b commit eafa407
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions examples/C/src/lib/ServerUI.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @param initial_file Path to the initial HTML file to serve, relative to the source directory of
* the main reactor. Defaults to "page.html".
* @param hostport The host port number, which defults to 8080.
* @param hostport The host port number, which defaults to 8080.
*
* @author Edward A. Lee
*/
Expand All @@ -24,6 +24,8 @@ preamble {=

#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h> // Defines errno
#include <string.h> // Defines strerror()
#include <unistd.h> // Defines read(), close()
#include <string.h> // Defines strchr()
#include <libgen.h> // Defines dirname()
Expand Down Expand Up @@ -87,16 +89,21 @@ reactor ServerUI(initial_file: string = "page.html", hostport: uint16_t = 8080)
lf_print_error_and_exit("Error accepting connection.");
}

char buffer[1024] = {0};
read(browser_ui->client_socket, buffer, 1024);
char buffer[2048] = {0};
ssize_t bytes_read = read(browser_ui->client_socket, buffer, 2047); // Ensure null terminator.
if (bytes_read == 0) continue;
if (bytes_read < 0) {
lf_print_warning("Error %d reading socket: %s", errno, strerror(errno));
continue;
}

// The response depends on the path part of the request.
const char *start_of_path = strchr(buffer, ' ') + 1;
if (start_of_path != NULL && strncmp("/ ", start_of_path, 2) != 0) {
const char *end_of_path = strchr(start_of_path, ' ') + 1;
size_t length = end_of_path - start_of_path;
const char *start_of_path = strchr(buffer, ' ');
if (start_of_path != NULL && strncmp("/ ", start_of_path + 1, 2) != 0) {
const char *end_of_path = strchr(start_of_path + 1, ' ') + 1;
size_t length = end_of_path - start_of_path - 1;
char* path = (char*)malloc(length + 1);
strncpy(path, start_of_path, length);
strncpy(path, start_of_path + 1, length);
path[length] = '\0';
lf_schedule_value(browser_ui->req_action, 0, path, length + 1);
} else {
Expand Down

0 comments on commit eafa407

Please sign in to comment.