improve error handling to make coverity scan happy

This commit is contained in:
Axel Kohlmeyer
2023-12-02 08:53:05 -05:00
parent 4035291a48
commit cace83e30d
2 changed files with 16 additions and 5 deletions

View File

@ -127,12 +127,15 @@ void ReadCarFile(void)
/* First pass through file -- Count molecules */
while(fgets(line,MAX_LINE_LENGTH,CarF) != NULL )
if( strncmp(line,"end",3) == 0 )
while (fgets(line,MAX_LINE_LENGTH,CarF) != NULL )
if (strncmp(line,"end",3) == 0 )
no_molecules++;
/* Allocate space to keep track of the number of atoms within a molecule */
if (no_molecules < 1) {
fprintf(stderr, "No molecules in system");
exit(32);
}
no_atoms = (int *) calloc(no_molecules,sizeof(int));
if ( no_atoms == NULL ) {
printf("Could not allocate memory for no_atoms\n");

View File

@ -12,6 +12,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#if defined(_WIN32)
#define strdup(x) _strdup(x)
@ -91,10 +92,14 @@ void SearchAndFill(struct FrcFieldItem *item)
}
file_pos = ftell(FrcF);
if (file_pos < 0) {
fprintf(stderr, "Could not obtain file stream position: ", strerror(errno));
exit(2);
}
/* Count the number of lines until next item is found */
while( strncmp(fgets(line,MAX_LINE_LENGTH,FrcF), "#", 1) != 0 )
while (strncmp(fgets(line,MAX_LINE_LENGTH,FrcF), "#", 1) != 0 )
ctr++;
/* Allocate the memory using calloc */
@ -110,7 +115,10 @@ void SearchAndFill(struct FrcFieldItem *item)
/* Read lines until keyword is found */
fseek(FrcF,file_pos,SEEK_SET);
if (fseek(FrcF,file_pos,SEEK_SET) < 0) {
fprintf(stderr, "Resetting file stream failed: ", strerror(errno));
exit(2);
}
strcpy(line,"empty");
/* Read lines until data starts (when !--- is found) */