On Fri, 10 Mar 2017 16:24:32 +0000 "Legacy, Allain" <allain.leg...@windriver.com> wrote:
> The robustness of the strtoul() could be improved with something like the > following to catch non-integer characters following the port number. > > char *end = NULL; > tmp = strtoull(val, &end, 0); > if ((val[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0)) Extra () no necessary here. Also errno is not set unless the return value is ULLONG_MAX. It will be last value. Something like: tmp = strtoull(val, &end, 0); if (!*val || !*end || (tmp == ULLONG_MAX && errno)) ... If endptr is not NULL, strtoul() stores the address of the first invalid character in *endptr. If there were no digits at all, str‐ toul() 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 VALUE The strtoul() function returns either the result of the conversion or, if there was a leading minus sign, the negation of the result of the conversion represented as an unsigned value, unless the original (non‐ negated) value would overflow; in the latter case, strtoul() returns ULONG_MAX and sets errno to ERANGE. Precisely the same holds for str‐ toull() (with ULLONG_MAX instead of ULONG_MAX).