From 770b136c5975aa2ad21cae1399578b3dd25ff883 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Thu, 5 Dec 2024 13:29:23 +0100 Subject: [PATCH] Revert "Fixed strncpy" This reverts commit 363851314be6712254846016a974baee8f14d028. --- src/templates_front/templator_front.org | 4 +-- src/templates_hdf5/templator_hdf5.org | 12 +++---- src/templates_text/templator_text.org | 48 ++++++++++++------------- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index e25233a..35c7f26 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -840,7 +840,7 @@ trexio_string_of_error (const trexio_exit_code error) void trexio_string_of_error_f (const trexio_exit_code error, char result[<>]) { - strncpy(result, trexio_string_of_error(error), <>-1); + strncpy(result, trexio_string_of_error(error), <>); result[<>-1] = '\0'; } #+end_src @@ -1291,7 +1291,7 @@ trexio_open(const char* file_name, const char mode, /* Data for the parent type */ - strncpy(result->file_name, file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy(result->file_name, file_name, TREXIO_MAX_FILENAME_LENGTH); result->file_name[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; if (result->file_name[TREXIO_MAX_FILENAME_LENGTH-2] != '\0') { if (rc_open != NULL) *rc_open = TREXIO_INVALID_ARG_1; diff --git a/src/templates_hdf5/templator_hdf5.org b/src/templates_hdf5/templator_hdf5.org index 2fe7318..bc41253 100644 --- a/src/templates_hdf5/templator_hdf5.org +++ b/src/templates_hdf5/templator_hdf5.org @@ -553,10 +553,8 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, char dset_index_name[256]; char dset_value_name[256]; /* Build the names of the datasets */ - strncpy(dset_index_name, $GROUP_DSET$_NAME "_indices", 255); - strncpy(dset_value_name, $GROUP_DSET$_NAME "_values", 255); - dset_index_name[255]='\0'; - dset_value_name[255]='\0'; + strncpy(dset_index_name, $GROUP_DSET$_NAME "_indices", 256); + strncpy(dset_value_name, $GROUP_DSET$_NAME "_values", 256); trexio_exit_code rc_write = TREXIO_FAILURE; /* NOTE: chunk size is set upon creation of the HDF5 dataset and cannot be changed ! */ @@ -618,10 +616,8 @@ trexio_hdf5_read_$group_dset$ (trexio_t* const file, char dset_index_name[256]; char dset_value_name[256]; /* Build the names of the datasets */ - strncpy(dset_index_name, $GROUP_DSET$_NAME "_indices", 255); - strncpy(dset_value_name, $GROUP_DSET$_NAME "_values", 255); - dset_index_name[255]='\0'; - dset_value_name[255]='\0'; + strncpy(dset_index_name, $GROUP_DSET$_NAME "_indices", 256); + strncpy(dset_value_name, $GROUP_DSET$_NAME "_values", 256); hsize_t offset_i[1] = {(hsize_t) offset_file * $group_dset_rank$}; hsize_t count_i[1] = {(hsize_t) size * $group_dset_rank$}; diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 7f1a873..a9cc96f 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -221,7 +221,7 @@ trexio_text_init (trexio_t* const file) char file_name[TREXIO_MAX_FILENAME_LENGTH]; - strncpy (file_name, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_name, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_name[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name)); @@ -241,7 +241,7 @@ trexio_text_init (trexio_t* const file) ,*/ char dirname[TREXIO_MAX_FILENAME_LENGTH] = "/tmp/trexio.XXXXXX"; if (mkdtemp(dirname) == NULL) return TREXIO_ERRNO; - strncpy (file_name, dirname, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_name, dirname, TREXIO_MAX_FILENAME_LENGTH); file_name[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name)); f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); @@ -369,7 +369,7 @@ trexio_text_read_$group$ (trexio_text_t* const file) /* Build the file name */ const char* $group$_file_name = "/$group$.txt"; - strncpy ($group$->file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy ($group$->file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH); $group$->file_name[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; strncat ($group$->file_name, $group$_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name)); @@ -488,7 +488,7 @@ trexio_text_read_$group$ (trexio_text_t* const file) } size_t tmp_$group_dset$_len = strlen(buffer); - strncpy(tmp_$group_dset$, buffer, 31); + strncpy(tmp_$group_dset$, buffer, 32); tmp_$group_dset$[31] = '\0'; tmp_$group_dset$ += tmp_$group_dset$_len + 1; } @@ -553,7 +553,7 @@ trexio_text_read_$group$ (trexio_text_t* const file) return NULL; } /* Safer string conversion to avoid buffer overflow in fscanf */ - strncpy($group$->$group_str$, buffer, $group$->len_$group_str$-1); + strncpy($group$->$group_str$, buffer, $group$->len_$group_str$); $group$->$group_str$[$group$->len_$group_str$-1] = '\0'; } @@ -598,7 +598,7 @@ trexio_text_has_$group$ (trexio_t* const file) const char* $group$_file_name = "/$group$.txt"; - strncpy ($group$_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy ($group$_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); $group$_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; strncat ($group$_full_path, $group$_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name)); @@ -1033,7 +1033,7 @@ trexio_text_read_$group_str$ (trexio_t* const file, char* const str, const uint3 $group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file); if ($group$ == NULL) return TREXIO_FAILURE; - strncpy(str, $group$->$group_str$, max_str_len-1); + strncpy(str, $group$->$group_str$, max_str_len); str[max_str_len-1] = '\0'; return TREXIO_SUCCESS; @@ -1142,22 +1142,22 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, CURRENTLY NO OFFSET IS USED WHEN WRITING ! ,*/ (void) offset_file; // Avoid unused variable error - + int64_t line_length = 0L; char format_str[256]; /* Determine the optimal type for storing indices depending on the size_max (usually mo_num or ao_num) */ if (size_max < UINT8_MAX) { line_length = $sparse_line_length_8$; // 41 for 4 indices - strncpy(format_str, $sparse_format_printf_8$, 255); + strncpy(format_str, $sparse_format_printf_8$, 256); format_str[255] = '\0'; } else if (size_max < UINT16_MAX) { line_length = $sparse_line_length_16$; // 49 for 4 indices - strncpy(format_str, $sparse_format_printf_16$, 255); + strncpy(format_str, $sparse_format_printf_16$, 256); format_str[255] = '\0'; } else { line_length = $sparse_line_length_32$; //69 for 4 indices - strncpy(format_str, $sparse_format_printf_32$, 255); + strncpy(format_str, $sparse_format_printf_32$, 256); format_str[255] = '\0'; } strncat(format_str, "\n", 2); @@ -1207,7 +1207,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH); /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, $group$_file_name, @@ -1247,7 +1247,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file, char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, $group_dset$_file_name, @@ -1323,7 +1323,7 @@ trexio_exit_code trexio_text_read_$group_dset$_size(trexio_t* const file, int64_ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, $group_dset$_file_name, @@ -1374,7 +1374,7 @@ trexio_exit_code trexio_text_has_$group_dset$(trexio_t* const file) char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, $group_dset$_file_name, @@ -1423,7 +1423,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file, /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name)); @@ -1486,7 +1486,7 @@ trexio_text_read_$group_dset$_size(trexio_t* const file, int64_t* const size_max /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name)); @@ -1540,7 +1540,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name)); @@ -1588,7 +1588,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH); /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, group_file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(group_file_name)); @@ -1617,7 +1617,7 @@ trexio_exit_code trexio_text_has_$group_dset$(trexio_t* const file) /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name)); @@ -1693,7 +1693,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file, char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, determinant_list_file_name, @@ -1779,7 +1779,7 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file, char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, determinant_list_file_name, @@ -1814,7 +1814,7 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file, memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH); /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, det_file_name, @@ -1843,7 +1843,7 @@ trexio_exit_code trexio_text_has_determinant_list(trexio_t* const file) char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH-1); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); file_full_path[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; /* Append name of the file with sparse data */ strncat (file_full_path, determinant_list_file_name,