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.
This commit is contained in:
parent
b61f5c4cae
commit
8c30d271df
7 changed files with 23 additions and 20 deletions
4
bin.c
4
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;
|
||||
}
|
||||
|
|
2
bin.h
2
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 *);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# feuille version
|
||||
VERSION = 1.18.2
|
||||
VERSION = 1.19.0
|
||||
|
||||
# paths (customize them to fit your system)
|
||||
PREFIX = /usr/local
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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...");
|
||||
|
|
23
server.c
23
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 */
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
4
server.h
4
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
|
||||
|
|
Reference in a new issue