Skip to content

Commit

Permalink
Revert "Fixed strncpy"
Browse files Browse the repository at this point in the history
This reverts commit 3638513.
  • Loading branch information
scemama committed Dec 5, 2024
1 parent 3638513 commit 770b136
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/templates_front/templator_front.org
Original file line number Diff line number Diff line change
Expand Up @@ -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[<<MAX_STRING_LENGTH()>>])
{
strncpy(result, trexio_string_of_error(error), <<MAX_STRING_LENGTH()>>-1);
strncpy(result, trexio_string_of_error(error), <<MAX_STRING_LENGTH()>>);
result[<<MAX_STRING_LENGTH()>>-1] = '\0';
}
#+end_src
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions src/templates_hdf5/templator_hdf5.org
Original file line number Diff line number Diff line change
Expand Up @@ -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 ! */
Expand Down Expand Up @@ -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$};
Expand Down
48 changes: 24 additions & 24 deletions src/templates_text/templator_text.org
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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';

}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 770b136

Please sign in to comment.