Skip to content

Commit

Permalink
Reorganize errno checks (again)
Browse files Browse the repository at this point in the history
Check for errors as outlined by cert-err34-c and the compliant examples (e.g., https://wiki.sei.cmu.edu/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number)
* new wrapper functions for each strto*() that report errors via `LogInfo`
* remove all errno = 0 (other than when calling a function that is documented to set it on error and we check errno immediately afterwards)
* don't check errno if error is identifiable otherwise: i.e., report error via LogError() instead of check_errno(): OpenFile(), MkDir()
* remove now obsolete check_errno()
  • Loading branch information
dschlaep committed Jul 26, 2024
1 parent b832a02 commit 813c0b9
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 174 deletions.
12 changes: 10 additions & 2 deletions include/filefuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ void LogError(LOG_INFO *LogInfo, const int mode, const char *fmt, ...);

void sw_message(const char *msg);

void check_errno(
const char *valMsg1, char *valMsg2, const char *endPtr, LOG_INFO *LogInfo
unsigned long int sw_strtoul(
const char *str, const char *errMsg, LOG_INFO *LogInfo
);

long int sw_strtol(const char *str, const char *errMsg, LOG_INFO *LogInfo);

int sw_strtoi(const char *str, const char *errMsg, LOG_INFO *LogInfo);

double sw_strtod(const char *str, const char *errMsg, LOG_INFO *LogInfo);

float sw_strtof(const char *str, const char *errMsg, LOG_INFO *LogInfo);

int key_to_id(const char *key, const char **possibleKeys, int numPossKeys);

void set_hasKey(
Expand Down
19 changes: 7 additions & 12 deletions src/SW_Carbon.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ in SW_VegProd.c and SW_Flow_lib.c.
#include "include/SW_VegProd.h" // for BIO_INDEX, WUE_INDEX
#include <math.h> // for pow
#include <stdio.h> // for sscanf, FILE
#include <stdlib.h> // for strtol, strtod
#include <string.h> // for strcmp, memset

/* =================================================== */
Expand Down Expand Up @@ -94,7 +93,7 @@ void SW_CBN_read(
double ppm = 1.;
int existing_years[MAX_NYEAR] = {0};
short fileWasEmpty = 1;
char *MyFileName, inbuf[MAX_FILENAMESIZE], *endPtr;
char *MyFileName, inbuf[MAX_FILENAMESIZE];

MyFileName = InFiles[eCarbon];
f = OpenFile(MyFileName, "r", LogInfo);
Expand Down Expand Up @@ -134,8 +133,7 @@ void SW_CBN_read(
return; /* Exit prematurely due to error */
}

year = (int) strtol(yearStr, &endPtr, 10);
check_errno(MyFileName, yearStr, endPtr, LogInfo);
year = sw_strtoi(yearStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand All @@ -155,14 +153,13 @@ void SW_CBN_read(
return; /* Exit prematurely due to error */
}

year = (int) strtol(yearStr, &endPtr, 10);
check_errno(MyFileName, yearStr, endPtr, LogInfo);
year = sw_strtoi(yearStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}

(void
) year; /* Silence clang-tidy clang-analyzer-deadcode.DeadStores */
/* Silence clang-tidy clang-analyzer-deadcode.DeadStores */
(void) year;

continue; // Skip to the ppm values
}
Expand All @@ -186,14 +183,12 @@ void SW_CBN_read(
return; /* Exit prematurely due to error */
}

year = (int) strtol(yearStr, &endPtr, 10);
check_errno(MyFileName, yearStr, endPtr, LogInfo);
year = sw_strtoi(yearStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}

ppm = strtod(ppmStr, &endPtr);
check_errno(MyFileName, ppmStr, endPtr, LogInfo);
ppm = sw_strtod(ppmStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down
7 changes: 3 additions & 4 deletions src/SW_Domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {

FILE *f;
int y, keyID;
char inbuf[LARGE_VALUE], *MyFileName, *endPtr;
char inbuf[LARGE_VALUE], *MyFileName;
char key[15], value[LARGE_VALUE]; // 15 - Max key size
int intRes = 0, scanRes;
double doubleRes = 0.;
Expand Down Expand Up @@ -241,12 +241,11 @@ void SW_DOM_read(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
doDoubleConv = (Bool) (keyID >= 9 && keyID <= 12);

if (doDoubleConv) {
doubleRes = strtod(value, &endPtr);
doubleRes = sw_strtod(value, MyFileName, LogInfo);
} else {
intRes = (int) strtol(value, &endPtr, 10);
intRes = sw_strtoi(value, MyFileName, LogInfo);
}

check_errno(MyFileName, value, endPtr, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down
2 changes: 0 additions & 2 deletions src/SW_Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "include/SW_Output.h" // for SW_OUT_close_files, SW_OUT_creat...
#include "include/SW_Weather.h" // for SW_WTH_finalize_all_weather
#include "include/Times.h" // for SW_WT_ReportTime, SW_WT_StartTime
#include <errno.h> // for errno
#include <stdio.h> // for NULL, stdout


Expand Down Expand Up @@ -54,7 +53,6 @@ int main(int argc, char **argv) {
Bool EchoInits = swFALSE;

unsigned long userSUID;
errno = 0;

// Start overall wall time
SW_WT_StartTime(&SW_WallTime);
Expand Down
12 changes: 5 additions & 7 deletions src/SW_Main_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ void sw_init_args(
* -q=quiet, don't print "Check logfile"
* at end of program.
*/
char str[1024], *endPtr;
char str[1024];
const char *errMsg = "command-line";

/* valid options */
char const *opts[] = {"-d", "-f", "-e", "-q", "-v", "-h", "-s", "-t", "-r"};
Expand Down Expand Up @@ -254,8 +255,7 @@ void sw_init_args(
break;

case 6: /* -s */
*userSUID = (unsigned long) strtoll(str, &endPtr, 10);
check_errno(NULL, str, endPtr, LogInfo);
*userSUID = sw_strtoul(str, errMsg, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand All @@ -264,8 +264,7 @@ void sw_init_args(
* (currently, unsigned long) */
/* Expect that conversion of string to double results in the
* same value as conversion of userSUID to double */
doubleUserSUID = strtod(str, &endPtr);
check_errno(NULL, str, endPtr, LogInfo);
doubleUserSUID = sw_strtod(str, errMsg, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand All @@ -284,8 +283,7 @@ void sw_init_args(
break;

case 7: /* -t */
*wallTimeLimit = strtod(str, &endPtr);
check_errno(NULL, str, endPtr, LogInfo);
*wallTimeLimit = sw_strtod(str, errMsg, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down
18 changes: 8 additions & 10 deletions src/SW_Markov.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ Bool SW_MKV_read_prob(
int lineno = 0, day, x, index;
RealF wet, dry, avg, std;
RealF *floatVals[4] = {&wet, &dry, &avg, &std};
char inbuf[MAX_FILENAMESIZE], *endPtr;
char inbuf[MAX_FILENAMESIZE];
char dayStr[4] = {'\0'}, inFloatStrs[4][20] = {{'\0'}};

/* note that Files.read() must be called prior to this. */
Expand All @@ -554,15 +554,14 @@ Bool SW_MKV_read_prob(
inFloatStrs[3]
);

day = (int) strtol(dayStr, &endPtr, 10);
check_errno(MyFileName, dayStr, endPtr, LogInfo);
day = sw_strtoi(dayStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return swFALSE; // Exit function prematurely due to error
}

for (index = 0; index < numFloatInStrings; index++) {
*(floatVals[index]) = strtof(inFloatStrs[index], &endPtr);
check_errno(MyFileName, inFloatStrs[index], endPtr, LogInfo);
*(floatVals[index]) =
sw_strtof(inFloatStrs[index], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return swFALSE; // Exit function prematurely due to error
}
Expand Down Expand Up @@ -673,7 +672,7 @@ Bool SW_MKV_read_cov(char *InFiles[], SW_MARKOV *SW_Markov, LOG_INFO *LogInfo) {
RealF *floatVals[] = {
&t1, &t2, &t3, &t4, &t5, &t6, &cfxw, &cfxd, &cfnw, &cfnd
};
char weekStr[3] = {'\0'}, inFloatStrs[10][20] = {{'\0'}}, *endPtr;
char weekStr[3] = {'\0'}, inFloatStrs[10][20] = {{'\0'}};
const int numFloatVals = 10;

char *MyFileName = InFiles[eMarkovCov];
Expand Down Expand Up @@ -717,15 +716,14 @@ Bool SW_MKV_read_cov(char *InFiles[], SW_MARKOV *SW_Markov, LOG_INFO *LogInfo) {
return swFALSE; // Exit function prematurely due to error
}

week = (int) strtol(weekStr, &endPtr, 10);
check_errno(MyFileName, weekStr, endPtr, LogInfo);
week = sw_strtoi(weekStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return swFALSE; // Exit function prematurely due to error
}

for (index = 0; index < numFloatVals; index++) {
*(floatVals[index]) = strtof(inFloatStrs[index], &endPtr);
check_errno(MyFileName, inFloatStrs[index], endPtr, LogInfo);
*(floatVals[index]) =
sw_strtof(inFloatStrs[index], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return swFALSE; // Exit function prematurely due to error
}
Expand Down
6 changes: 2 additions & 4 deletions src/SW_Model.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#include "include/SW_Files.h" // for eModel
#include "include/Times.h" // for Time_get_lastdoy_y, Time_init_model
#include <stdio.h> // for FILE
#include <stdlib.h> // for strtod
#include <string.h> // for memcpy


Expand Down Expand Up @@ -115,7 +114,7 @@ void SW_MDL_read(SW_MODEL *SW_Model, char *InFiles[], LOG_INFO *LogInfo) {
*/
FILE *f;
int lineno;
char *MyFileName, inbuf[MAX_FILENAMESIZE], *endPtr;
char *MyFileName, inbuf[MAX_FILENAMESIZE];
double value;

MyFileName = InFiles[eModel];
Expand All @@ -132,8 +131,7 @@ void SW_MDL_read(SW_MODEL *SW_Model, char *InFiles[], LOG_INFO *LogInfo) {
lineno = 0;
while (GetALine(f, inbuf, MAX_FILENAMESIZE)) {
if (lineno <= 4) {
value = strtod(inbuf, &endPtr);
check_errno(MyFileName, inbuf, endPtr, LogInfo);
value = sw_strtod(inbuf, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down
8 changes: 3 additions & 5 deletions src/SW_Output.c
Original file line number Diff line number Diff line change
Expand Up @@ -2802,7 +2802,7 @@ void SW_OUT_read(
char msg[200];
/* space for uppercase conversion */
char upkey[50], upsum[4];
char inbuf[MAX_FILENAMESIZE], *endPtr;
char inbuf[MAX_FILENAMESIZE];
int first, last = -1; /* first doy for output */

char *MyFileName = InFiles[eOutput];
Expand Down Expand Up @@ -2916,15 +2916,13 @@ void SW_OUT_read(
outfile[0] = '\0';
#endif

first = (int) strtol(firstStr, &endPtr, 10);
check_errno(MyFileName, firstStr, endPtr, LogInfo);
first = sw_strtoi(firstStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}

if (Str_CompareI(lastStr, (char *) "END") != 0) {
last = (int) strtol(lastStr, &endPtr, 10);
check_errno(MyFileName, lastStr, endPtr, LogInfo);
last = sw_strtoi(lastStr, MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down
33 changes: 15 additions & 18 deletions src/SW_Site.c
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ void SW_SIT_read(
#endif
LyrIndex r;
Bool too_many_regions = swFALSE;
char inbuf[MAX_FILENAMESIZE], *endPtr;
char inbuf[MAX_FILENAMESIZE];
int intRes = 0;
double doubleRes = 0.;
float floatRes = 0.;
Expand All @@ -1341,7 +1341,7 @@ void SW_SIT_read(

strLine = (Bool) (lineno == 35 || lineno == 37 || lineno == 38);

if (!strLine) {
if (!strLine && lineno <= 38) {
/* Check to see if the line number contains a double or integer
* value */
doFloatConv = (Bool) (lineno >= 14 && lineno <= 21);
Expand All @@ -1350,15 +1350,14 @@ void SW_SIT_read(
(Bool) (!doFloatConv && ((lineno >= 0 && lineno <= 2) ||
(lineno >= 5 && lineno <= 31)));

if (doFloatConv) {
floatRes = strtof(inbuf, &endPtr);
} else if (doDoubleConv) {
doubleRes = strtod(inbuf, &endPtr);
if (doDoubleConv) {
doubleRes = sw_strtod(inbuf, MyFileName, LogInfo);
} else if (doFloatConv) {
floatRes = sw_strtof(inbuf, MyFileName, LogInfo);
} else {
intRes = (int) strtol(inbuf, &endPtr, 10);
intRes = sw_strtoi(inbuf, MyFileName, LogInfo);
}

check_errno(MyFileName, inbuf, endPtr, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down Expand Up @@ -1528,14 +1527,12 @@ void SW_SIT_read(
x = sscanf(inbuf, "%9s %9s", rgnStr[0], rgnStr[1]);

if (x == 2) {
region = (int) strtol(rgnStr[0], &endPtr, 10);
check_errno(MyFileName, rgnStr[0], endPtr, LogInfo);
region = sw_strtoi(rgnStr[0], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}

rgnlow = (int) strtol(rgnStr[1], &endPtr, 10);
check_errno(MyFileName, rgnStr[1], endPtr, LogInfo);
rgnlow = sw_strtoi(rgnStr[1], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down Expand Up @@ -1631,7 +1628,7 @@ void SW_LYR_read(SW_SITE *SW_Site, char *InFiles[], LOG_INFO *LogInfo) {
int x, k, index;
RealF dmin = 0.0, dmax, evco, trco_veg[NVEGTYPES], psand, pclay,
soildensity, imperm, soiltemp, f_gravel;
char inbuf[MAX_FILENAMESIZE], *endPtr;
char inbuf[MAX_FILENAMESIZE];
char inFloatStrs[12][20] = {{'\0'}};
float *inFloatVals[] = {
&dmax,
Expand Down Expand Up @@ -1693,8 +1690,8 @@ void SW_LYR_read(SW_SITE *SW_Site, char *InFiles[], LOG_INFO *LogInfo) {

/* Convert float strings to floats */
for (index = 0; index < numFloatInStrings; index++) {
*(inFloatVals[index]) = strtof(inFloatStrs[index], &endPtr);
check_errno(MyFileName, inFloatStrs[index], endPtr, LogInfo);
*(inFloatVals[index]) =
sw_strtof(inFloatStrs[index], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down Expand Up @@ -1960,7 +1957,7 @@ void SW_SWRC_read(SW_SITE *SW_Site, char *InFiles[], LOG_INFO *LogInfo) {
LyrIndex lyrno = 0, k;
int x, index;
RealF tmp_swrcp[SWRC_PARAM_NMAX];
char inbuf[MAX_FILENAMESIZE], *endPtr;
char inbuf[MAX_FILENAMESIZE];
char swrcpFloatStrs[6][20] = {{'\0'}};
const int numFloatInStrings = 6;

Expand Down Expand Up @@ -2003,8 +2000,8 @@ void SW_SWRC_read(SW_SITE *SW_Site, char *InFiles[], LOG_INFO *LogInfo) {

/* Convert float strings to floats */
for (index = 0; index < numFloatInStrings; index++) {
tmp_swrcp[index] = strtof(swrcpFloatStrs[index], &endPtr);
check_errno(MyFileName, swrcpFloatStrs[index], endPtr, LogInfo);
tmp_swrcp[index] =
sw_strtof(swrcpFloatStrs[index], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down
6 changes: 2 additions & 4 deletions src/SW_Sky.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include "include/SW_Files.h" // for eSky
#include "include/Times.h" // for isleapyear, interpolate_monthlyV...
#include <stdio.h> // for sscanf, FILE
#include <stdlib.h> // for strtod

/* =================================================== */
/* Global Function Definitions */
Expand Down Expand Up @@ -71,7 +70,7 @@ void SW_SKY_read(char *InFiles[], SW_SKY *SW_Sky, LOG_INFO *LogInfo) {
FILE *f;
int lineno = 0, x = 0, k, index;
RealD tmp[MAX_MONTHS];
char *MyFileName, inbuf[MAX_FILENAMESIZE], *endPtr;
char *MyFileName, inbuf[MAX_FILENAMESIZE];
char tmpStrs[MAX_MONTHS][20] = {{'\0'}};

MyFileName = InFiles[eSky];
Expand Down Expand Up @@ -111,8 +110,7 @@ void SW_SKY_read(char *InFiles[], SW_SKY *SW_Sky, LOG_INFO *LogInfo) {
}

for (index = 0; index < MAX_MONTHS; index++) {
tmp[index] = strtod(tmpStrs[index], &endPtr);
check_errno(MyFileName, tmpStrs[index], endPtr, LogInfo);
tmp[index] = sw_strtod(tmpStrs[index], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
}
Expand Down
Loading

0 comments on commit 813c0b9

Please sign in to comment.