@@ -141,7 +141,7 @@ static int create_directory(char *output_dir, char *slug);
141141 * @arg data Buffer with data received from the user
142142 * @arg path Path at which file containing data from the buffer will be created
143143 */
144- static int save_to_file (uint8_t * data , char * output_dir , char * slug );
144+ static int save_to_file (const Fiche_Settings * s , uint8_t * data , char * slug );
145145
146146
147147// Logging-related
@@ -637,7 +637,7 @@ static void *handle_connection(void *args) {
637637
638638
639639 // Save to file failed, we have to finish here
640- if ( save_to_file (buffer , c -> settings -> output_dir_path , slug ) != 0 ) {
640+ if ( save_to_file (c -> settings , buffer , slug ) != 0 ) {
641641 print_error ("Couldn't save a file!" );
642642 print_separator ();
643643
@@ -739,19 +739,19 @@ static int create_directory(char *output_dir, char *slug) {
739739}
740740
741741
742- static int save_to_file (uint8_t * data , char * output_dir , char * slug ) {
742+ static int save_to_file (const Fiche_Settings * s , uint8_t * data , char * slug ) {
743743 char * file_name = "index.txt" ;
744744
745745 // Additional 2 bytes are for 2 slashes
746- size_t len = strlen (output_dir ) + strlen (slug ) + strlen (file_name ) + 3 ;
746+ size_t len = strlen (s -> output_dir_path ) + strlen (slug ) + strlen (file_name ) + 3 ;
747747
748748 // Generate a path
749749 char * path = malloc (len );
750750 if (!path ) {
751751 return -1 ;
752752 }
753753
754- snprintf (path , len , "%s%s%s%s%s" , output_dir , "/" , slug , "/" , file_name );
754+ snprintf (path , len , "%s%s%s%s%s" , s -> output_dir_path , "/" , slug , "/" , file_name );
755755
756756 // Attempt file saving
757757 FILE * f = fopen (path , "w" );
@@ -760,6 +760,14 @@ static int save_to_file(uint8_t *data, char *output_dir, char *slug) {
760760 return -1 ;
761761 }
762762
763+ // Null-terminate buffer if not null terminated already
764+ for (int i = 0 ; i < s -> buffer_len ; i ++ ) {
765+ if (data [i ] == 0 ) {
766+ break ;
767+ }
768+ data [s -> buffer_len - 1 ] = 0 ;
769+ }
770+
763771 if ( fprintf (f , "%s" , data ) < 0 ) {
764772 fclose (f );
765773 free (path );
0 commit comments