From 380d0f24426a7abd0630872c43cd68c2c7058b54 Mon Sep 17 00:00:00 2001 From: dam Date: Thu, 15 Dec 2022 17:27:22 +0000 Subject: Improved error return on read_input_to_int. --- main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 3d6ca66..918d3f0 100644 --- a/main.c +++ b/main.c @@ -1157,11 +1157,13 @@ bool read_input_to_int(int row, int style, const char *message, intmax_t *result read_input_to_string_buffer(row, input_pos_x, style, input_width); char *parser; + errno = 0; *result = strtoimax(string_buffer, &parser, 10); - // TODO Add comment about this comparison... is checking what? - ALSO ADD THIS BELOW - // If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid. - return parser != string_buffer; + bool success = (errno == 0 || errno == ERANGE) // No error OR value was clamped to limits (acceptable). + && parser != string_buffer; // If no digits are found, parser will return the address of the input string. + + return success; } // Retuns true if user presses enter, false otherwise. -- cgit v1.2.3