commit 585c547ed01372b3885acb6625c983bf13c53281
parent 5a609a104dd4bf42bdd4ed5e816757c9469ce580
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date: Wed, 9 Dec 2015 23:47:07 +0100
Using 'strtonum' to parse and validate bits parameter value
Diffstat:
1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -133,6 +133,10 @@ int main(int argc, char *argv[])
char *outputFile;
+ const char *errstr;
+
+ int32_t int_bits;
+
while ((getoptFlag = getopt(argc, argv, "b:c:ef:him:o:rsv")) != -1) {
switch(getoptFlag) {
case 'b':
@@ -219,8 +223,16 @@ int main(int argc, char *argv[])
}
// default to 8 if bits option is not specified
- if (!bits) {
- bits = "8";
+ if (bits) {
+ // convert numeric command line flags to integer values
+ int_bits = strtonum(bits, 8, 9, &errstr);
+
+ if (errstr) {
+ printf("\nInvalid value for bits.\n\n");
+ return EXIT_FAILURE;
+ }
+ } else {
+ int_bits = 8;
}
// default to empty string if mode option is not specified
@@ -228,14 +240,6 @@ int main(int argc, char *argv[])
mode = "";
}
- // convert numeric command line flags to integer values
- int32_t int_bits = atoi(bits);
-
- // now set bits to 8 if not already value 8 or 9
- if (int_bits != 8 && int_bits != 9) {
- int_bits = 8;
- }
-
// default to 160 if columns option is not specified
if (!columns) {
columns = "160";