mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-12-23 04:43:57 +01: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.h"
|
||||||
#include "trexio_s.h"
|
#include "trexio_s.h"
|
||||||
|
#include "trexio_private.h"
|
||||||
#include "trexio_text.h"
|
#include "trexio_text.h"
|
||||||
#include "trexio_hdf5.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.
|
object is the same as the size of the data type pointed by the pointer.
|
||||||
|
|
||||||
#+begin_src c :tangle trexio_private.h
|
#+begin_src c :tangle trexio_private.h
|
||||||
#define MALLOC(T) (T*) malloc (sizeof(T));
|
#define MALLOC(T) (T*) malloc (sizeof(T))
|
||||||
#define CALLOC(N,T) (T*) calloc ((N),sizeof(T));
|
#define CALLOC(N,T) (T*) calloc ( (N) , sizeof(T) )
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
When a pointer is freed, it should be set to ~NULL~.
|
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; }
|
#define FREE(X) { free(X) ; (X)=NULL; }
|
||||||
#+end_src
|
#+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
|
* Front end
|
||||||
|
|
||||||
All calls to TREXIO are thread-safe.
|
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 == NULL) return NULL;
|
||||||
if (file_name[0] == '\0') return NULL;
|
if (file_name[0] == '\0') return NULL;
|
||||||
|
/* Check overflow in file_name */
|
||||||
|
|
||||||
if (back_end < 0) return NULL;
|
if (back_end < 0) return NULL;
|
||||||
if (back_end >= TREXIO_INVALID_BACK_END) 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 */
|
/* Data for the parent type */
|
||||||
|
|
||||||
result->file_name = CALLOC(strlen(file_name)+1, char);
|
result->file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||||
strncpy(result->file_name, file_name, strlen(file_name)+1);
|
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->back_end = back_end;
|
||||||
result->mode = mode;
|
result->mode = mode;
|
||||||
int irc = pthread_mutex_init ( &(result->thread_lock), NULL);
|
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:
|
case TREXIO_JSON:
|
||||||
rc = trexio_json_lock(result);
|
rc = trexio_json_lock(result);
|
||||||
break;
|
break;
|
||||||
*/
|
,*/
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc != TREXIO_SUCCESS) {
|
if (rc != TREXIO_SUCCESS) {
|
||||||
@ -563,8 +575,8 @@ trexio_close (trexio_t* file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rc != TREXIO_SUCCESS) {
|
if (rc != TREXIO_SUCCESS) {
|
||||||
free(file->file_name);
|
FREE(file->file_name);
|
||||||
free(file);
|
FREE(file);
|
||||||
return TREXIO_FAILURE;
|
return TREXIO_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -590,8 +602,7 @@ trexio_close (trexio_t* file)
|
|||||||
|
|
||||||
/* Terminate front end */
|
/* Terminate front end */
|
||||||
|
|
||||||
free(file->file_name);
|
FREE(file->file_name);
|
||||||
file->file_name = NULL;
|
|
||||||
|
|
||||||
int irc = pthread_mutex_destroy( &(file->thread_lock) );
|
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 */
|
/* Create the lock file in the directory */
|
||||||
const char* lock_file_name = "/.lock";
|
const char* lock_file_name = "/.lock";
|
||||||
|
|
||||||
size_t str_size = strlen(file->file_name) + strlen(lock_file_name) + 1;
|
char* file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||||
char* file_name = CALLOC(str_size, char);
|
|
||||||
|
|
||||||
if (file_name == NULL) {
|
if (file_name == NULL) {
|
||||||
return TREXIO_ALLOCATION_FAILED;
|
return TREXIO_ALLOCATION_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy (file_name, file->file_name, str_size);
|
strncpy (file_name, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||||
strncat (file_name, lock_file_name, strlen(lock_file_name));
|
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);
|
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
||||||
FREE(file_name);
|
FREE(file_name);
|
||||||
@ -222,7 +226,7 @@ trexio_text_unlock (trexio_t* const file)
|
|||||||
|
|
||||||
}
|
}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** Deinitialize function (templated part)
|
** Deinitialize function (templated part)
|
||||||
|
|
||||||
#+begin_src c :tangle basic_text_group.c
|
#+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 */
|
/* Build the file name */
|
||||||
const char* $group$_file_name = "/$group$.txt";
|
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(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||||
char * file_name = CALLOC(str_size, char);
|
|
||||||
|
|
||||||
if (file_name == NULL) {
|
if (file_name == NULL) {
|
||||||
FREE($group$);
|
FREE($group$);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy (file_name, file->parent.file_name, str_size);
|
strncpy (file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||||
strncat (file_name, $group$_file_name, strlen($group$_file_name));
|
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 */
|
/* If the file exists, read it */
|
||||||
FILE* f = fopen(file_name,"r");
|
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;
|
if (file->rdm != NULL) return file->rdm;
|
||||||
|
|
||||||
/* Allocate the data structure */
|
/* Allocate the data structure */
|
||||||
rdm_t* const rdm = MALLOC(rdm_t);
|
rdm_t* rdm = MALLOC(rdm_t);
|
||||||
assert (rdm != NULL);
|
assert (rdm != NULL);
|
||||||
|
|
||||||
rdm->one_e = 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 */
|
/* Try to open the file. If the file does not exist, return */
|
||||||
const char* rdm_file_name = "/rdm.txt";
|
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(TREXIO_MAX_FILENAME_LENGTH, char);
|
||||||
char * file_name = CALLOC(str_size, char);
|
|
||||||
|
|
||||||
assert (file_name != NULL);
|
assert (file_name != NULL);
|
||||||
strncpy (file_name, file->parent.file_name, str_size);
|
strncpy (file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||||
strncat (file_name, rdm_file_name, strlen(rdm_file_name));
|
|
||||||
|
|
||||||
|
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 */
|
/* If the file exists, read it */
|
||||||
FILE* f = fopen(file_name,"r");
|
FILE* f = fopen(file_name,"r");
|
||||||
if (f != NULL) {
|
if (f != NULL) {
|
||||||
@ -766,9 +782,17 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
|
|||||||
|
|
||||||
rc = fscanf(f, "%1023s", buffer);
|
rc = fscanf(f, "%1023s", buffer);
|
||||||
assert (rc == 1);
|
assert (rc == 1);
|
||||||
str_size = strlen(buffer);
|
rdm->two_e_file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH,char);
|
||||||
rdm->two_e_file_name = CALLOC(str_size,char);
|
strncpy(rdm->two_e_file_name, buffer, 1024);
|
||||||
strncpy(rdm->two_e_file_name, buffer, str_size);
|
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);
|
FREE(buffer);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
Loading…
Reference in New Issue
Block a user