mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-08-24 22:21:43 +02:00
Fixed warning about strlen in strncpy
This commit is contained in:
parent
f42dccc5b9
commit
daf3fdeb8b
@ -53,6 +53,7 @@ typedef int32_t trexio_exit_code;
|
||||
|
||||
#include "trexio.h"
|
||||
#include "trexio_s.h"
|
||||
#include "trexio_private.h"
|
||||
#include "trexio_text.h"
|
||||
#include "trexio_hdf5.h"
|
||||
/*
|
||||
@ -90,8 +91,8 @@ typedef int32_t trexio_exit_code;
|
||||
object is the same as the size of the data type pointed by the pointer.
|
||||
|
||||
#+begin_src c :tangle trexio_private.h
|
||||
#define MALLOC(T) (T*) malloc (sizeof(T));
|
||||
#define CALLOC(N,T) (T*) calloc ((N),sizeof(T));
|
||||
#define MALLOC(T) (T*) malloc (sizeof(T))
|
||||
#define CALLOC(N,T) (T*) calloc ( (N) , sizeof(T) )
|
||||
#+end_src
|
||||
|
||||
When a pointer is freed, it should be set to ~NULL~.
|
||||
@ -101,6 +102,10 @@ typedef int32_t trexio_exit_code;
|
||||
#define FREE(X) { free(X) ; (X)=NULL; }
|
||||
#+end_src
|
||||
|
||||
The maximum string size for the filenames is 4096 characters.
|
||||
#+begin_src c :tangle trexio_private.h
|
||||
#define TREXIO_MAX_FILENAME_LENGTH 4096
|
||||
#+end_src
|
||||
* Front end
|
||||
|
||||
All calls to TREXIO are thread-safe.
|
||||
@ -414,6 +419,7 @@ trexio_open(const char* file_name, const char mode,
|
||||
|
||||
if (file_name == NULL) return NULL;
|
||||
if (file_name[0] == '\0') return NULL;
|
||||
/* Check overflow in file_name */
|
||||
|
||||
if (back_end < 0) return NULL;
|
||||
if (back_end >= TREXIO_INVALID_BACK_END) return NULL;
|
||||
@ -444,8 +450,14 @@ trexio_open(const char* file_name, const char mode,
|
||||
|
||||
/* Data for the parent type */
|
||||
|
||||
result->file_name = CALLOC(strlen(file_name)+1, char);
|
||||
strncpy(result->file_name, file_name, strlen(file_name)+1);
|
||||
result->file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||
strncpy(result->file_name, file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
if (result->file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
|
||||
free(result->file_name);
|
||||
free(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result->back_end = back_end;
|
||||
result->mode = mode;
|
||||
int irc = pthread_mutex_init ( &(result->thread_lock), NULL);
|
||||
@ -496,7 +508,7 @@ trexio_open(const char* file_name, const char mode,
|
||||
case TREXIO_JSON:
|
||||
rc = trexio_json_lock(result);
|
||||
break;
|
||||
*/
|
||||
,*/
|
||||
}
|
||||
|
||||
if (rc != TREXIO_SUCCESS) {
|
||||
@ -563,8 +575,8 @@ trexio_close (trexio_t* file)
|
||||
}
|
||||
|
||||
if (rc != TREXIO_SUCCESS) {
|
||||
free(file->file_name);
|
||||
free(file);
|
||||
FREE(file->file_name);
|
||||
FREE(file);
|
||||
return TREXIO_FAILURE;
|
||||
}
|
||||
|
||||
@ -590,8 +602,7 @@ trexio_close (trexio_t* file)
|
||||
|
||||
/* Terminate front end */
|
||||
|
||||
free(file->file_name);
|
||||
file->file_name = NULL;
|
||||
FREE(file->file_name);
|
||||
|
||||
int irc = pthread_mutex_destroy( &(file->thread_lock) );
|
||||
|
||||
|
@ -143,15 +143,19 @@ trexio_text_init (trexio_t* const file)
|
||||
/* Create the lock file in the directory */
|
||||
const char* lock_file_name = "/.lock";
|
||||
|
||||
size_t str_size = strlen(file->file_name) + strlen(lock_file_name) + 1;
|
||||
char* file_name = CALLOC(str_size, char);
|
||||
char* file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||
|
||||
if (file_name == NULL) {
|
||||
return TREXIO_ALLOCATION_FAILED;
|
||||
}
|
||||
|
||||
strncpy (file_name, file->file_name, str_size);
|
||||
strncat (file_name, lock_file_name, strlen(lock_file_name));
|
||||
strncpy (file_name, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name));
|
||||
|
||||
if (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
|
||||
FREE(file_name);
|
||||
return TREXIO_ALLOCATION_FAILED;
|
||||
}
|
||||
|
||||
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
||||
FREE(file_name);
|
||||
@ -222,7 +226,7 @@ trexio_text_unlock (trexio_t* const file)
|
||||
|
||||
}
|
||||
#+end_src
|
||||
|
||||
|
||||
** Deinitialize function (templated part)
|
||||
|
||||
#+begin_src c :tangle basic_text_group.c
|
||||
@ -270,16 +274,22 @@ trexio_text_read_$group$ (trexio_text_t* const file)
|
||||
|
||||
/* Build the file name */
|
||||
const char* $group$_file_name = "/$group$.txt";
|
||||
size_t str_size = strlen(file->parent.file_name) + strlen($group$_file_name) + 1;
|
||||
char * file_name = CALLOC(str_size, char);
|
||||
char * file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||
|
||||
if (file_name == NULL) {
|
||||
FREE($group$);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strncpy (file_name, file->parent.file_name, str_size);
|
||||
strncat (file_name, $group$_file_name, strlen($group$_file_name));
|
||||
strncpy (file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
strncat (file_name, $group$_file_name,
|
||||
TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name));
|
||||
|
||||
if (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
|
||||
FREE(file_name);
|
||||
FREE($group$);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* If the file exists, read it */
|
||||
FILE* f = fopen(file_name,"r");
|
||||
@ -708,7 +718,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
|
||||
if (file->rdm != NULL) return file->rdm;
|
||||
|
||||
/* Allocate the data structure */
|
||||
rdm_t* const rdm = MALLOC(rdm_t);
|
||||
rdm_t* rdm = MALLOC(rdm_t);
|
||||
assert (rdm != NULL);
|
||||
|
||||
rdm->one_e = NULL;
|
||||
@ -718,13 +728,19 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
|
||||
|
||||
/* Try to open the file. If the file does not exist, return */
|
||||
const char* rdm_file_name = "/rdm.txt";
|
||||
size_t str_size = strlen(file->parent.file_name) + strlen(rdm_file_name) + 1;
|
||||
char * file_name = CALLOC(str_size, char);
|
||||
char* file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||
|
||||
assert (file_name != NULL);
|
||||
strncpy (file_name, file->parent.file_name, str_size);
|
||||
strncat (file_name, rdm_file_name, strlen(rdm_file_name));
|
||||
strncpy (file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
|
||||
strncat (file_name, rdm_file_name,
|
||||
TREXIO_MAX_FILENAME_LENGTH-strlen(rdm_file_name));
|
||||
|
||||
if (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
|
||||
FREE(file_name);
|
||||
FREE(rdm);
|
||||
return NULL;
|
||||
}
|
||||
/* If the file exists, read it */
|
||||
FILE* f = fopen(file_name,"r");
|
||||
if (f != NULL) {
|
||||
@ -766,9 +782,17 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
|
||||
|
||||
rc = fscanf(f, "%1023s", buffer);
|
||||
assert (rc == 1);
|
||||
str_size = strlen(buffer);
|
||||
rdm->two_e_file_name = CALLOC(str_size,char);
|
||||
strncpy(rdm->two_e_file_name, buffer, str_size);
|
||||
rdm->two_e_file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH,char);
|
||||
strncpy(rdm->two_e_file_name, buffer, 1024);
|
||||
if (rdm->two_e_file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
|
||||
FREE(file_name);
|
||||
FREE(buffer);
|
||||
FREE(rdm->one_e);
|
||||
FREE(rdm->two_e_file_name);
|
||||
FREE(rdm);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FREE(buffer);
|
||||
fclose(f);
|
||||
|
Loading…
Reference in New Issue
Block a user