From 8c30d271df21ac4a917b8db82f4b6a42b6ab2b8e Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Tue, 29 Nov 2022 13:57:11 +0100 Subject: [PATCH] feat: use paste size instead of NULL byte in order to write the paste Previously, the pastes were written to disk until feuille encountered a NULL byte. This is now fixed. + version bump. --- bin.c | 4 ++-- bin.h | 2 +- config.mk | 2 +- feuille.1 | 2 +- feuille.c | 6 ++++-- server.c | 23 ++++++++++++----------- server.h | 4 ++-- 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/bin.c b/bin.c index 3de01e6..d744b8b 100644 --- a/bin.c +++ b/bin.c @@ -83,7 +83,7 @@ int paste_exists(char *id) * id: the ID of the paste. * -> 0 if done, -1 if not. */ -int write_paste(char *paste, char *id) +int write_paste(char *paste, unsigned long paste_size, char *id) { /* open the file with write access */ FILE *file; @@ -91,7 +91,7 @@ int write_paste(char *paste, char *id) return -1; /* write the content to file */ - if (fputs(paste, file) == -1) { + if (fwrite(paste, paste_size, sizeof(char), file) == -1) { fclose(file); return -1; } diff --git a/bin.h b/bin.h index 2947615..fddb7f0 100644 --- a/bin.h +++ b/bin.h @@ -17,7 +17,7 @@ #include "feuille.h" int paste_exists(char *); -int write_paste(char *, char *); +int write_paste(char *, unsigned long, char *); char *generate_id(int); char *create_url(char *); diff --git a/config.mk b/config.mk index 1631637..9bf583c 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # feuille version -VERSION = 1.18.2 +VERSION = 1.19.0 # paths (customize them to fit your system) PREFIX = /usr/local diff --git a/feuille.1 b/feuille.1 index de70c4d..6127a62 100644 --- a/feuille.1 +++ b/feuille.1 @@ -14,7 +14,7 @@ . ftr VB CB . ftr VBI CBI .\} -.TH "feuille" "1" "November 2022" "feuille 1.18.2" "" +.TH "feuille" "1" "November 2022" "feuille 1.19.0" "" .hy .SH NAME .PP diff --git a/feuille.c b/feuille.c index 6277b79..7e2a587 100644 --- a/feuille.c +++ b/feuille.c @@ -95,6 +95,8 @@ void accept_loop(int server) while ((connection = accept_connection(server))) { verbose(1, "--- new incoming connection. connection ID: %d:%d ---", pid, time(0)); + unsigned long paste_size = 0; + char *paste = NULL; char *id = NULL; char *url = NULL; @@ -102,7 +104,7 @@ void accept_loop(int server) /* read paste from connection */ verbose(1, "reading paste from incoming connection..."); - if ((paste = read_paste(connection)) != NULL) { + if ((paste_size = read_paste(connection, &paste)) != 0) { /* generate random ID */ verbose(1, "done."); verbose(2, "generating a random ID..."); @@ -112,7 +114,7 @@ void accept_loop(int server) verbose(2, "done."); verbose(1, "writing paste `%s' to disk...", id); - if (write_paste(paste, id) == 0) { + if (write_paste(paste, paste_size, id) == 0) { /* create URL */ verbose(1, "done."); verbose(2, "making the right URL..."); diff --git a/server.c b/server.c index ff560d7..ac11c81 100644 --- a/server.c +++ b/server.c @@ -175,15 +175,16 @@ void close_connection(int connection) * connection: the socket associated with the connection. * -> the string containing all data sent, or NULL if an error occured. Needs to be freed. */ -char *read_paste(int connection) +unsigned long read_paste(int connection, char **output) { + unsigned long buffer_size = settings.buffer_size; unsigned long total_size = 0; /* allocate buffer to store the data */ char *buffer; - if ((buffer = malloc((buffer_size + 2) * sizeof(char))) == NULL) - return NULL; + if ((buffer = malloc((buffer_size + 1) * sizeof(char))) == NULL) + return 0; /* read all data until EOF is received, or max file size is reached, or the socket timeouts... */ /* each time, the data is appended to the buffer, once it's been reallocated a larger size */ @@ -197,7 +198,7 @@ char *read_paste(int connection) /* yup, free the buffer and return an error */ free(buffer); errno = EFBIG; - return NULL; + return 0; } /* have we reached the end of the buffer? */ @@ -207,9 +208,9 @@ char *read_paste(int connection) /* reallocate the buffer with a larger size */ void *tmp; - if ((tmp = realloc(buffer, (buffer_size + 2) * sizeof(char))) == NULL) { + if ((tmp = realloc(buffer, (buffer_size + 1) * sizeof(char))) == NULL) { free(buffer); - return NULL; + return 0; } buffer = tmp; @@ -223,14 +224,14 @@ char *read_paste(int connection) errno = ENOENT; free(buffer); - return NULL; + return 0; } - /* end the buffer with a newline and a null byte */ - buffer[total_size] = '\n'; - buffer[total_size + 1] = 0; + /* end the buffer with a newline */ + buffer[total_size] = '\n'; - return buffer; + *output = buffer; + return total_size + 1; /* newline */ } /** diff --git a/server.h b/server.h index b5aa896..d72b18a 100644 --- a/server.h +++ b/server.h @@ -21,7 +21,7 @@ int initialize_server(); int accept_connection(int); void close_connection(int); -char *read_paste(int); -int send_response(int, char *); +unsigned long read_paste(int, char **); +int send_response(int, char *); #endif