I'm testing a segment of my program for underflow and overflow. The input is coming as a command line argument. The input that is coming the command line is the number of entries to process. The number zero here means to process every entry from the file.
Here is my code with only relevant information.
Code:
int main(int argc, char *argv[])
{
int numOfEntries, isValidEntries;
isValidEntries = inputValidation(argv[1], &numOfEntries);
if(!isValidEntries || numOfEntries < 0)
{
puts("\nThe number of entries that you entered is either too big or too small. ");
puts("Please execute the program again.");
}
return EXIT_SUCCESS;
}
int inputValidation(char *str, int *value)
{
char *endPtr;
if(str[0] == '\0')
{
return FALSE;
}
errno = FALSE;
*value = (int)strtol(str, &endPtr, BASE);
if((errno == ERANGE && ((*value) == INT_MAX || (*value) == INT_MIN)) || (errno != FALSE && (*value) == FALSE))
{
return FALSE;
}
else if(endPtr == str || *endPtr != '\0')
{
return FALSE;
}
return TRUE;
}
In essence, if we have an overflow or an underflow the input is invalid. Else if it is not an underflow but a negative number, The input is invalid. Is there anything you see here that I forgot to check or did improperly? Cheers :-)