[LTP] [PATCH v2 1/2] lib/safe_macros: Add SAFE_STRTOF

Cyril Hrubis chrubis@suse.cz
Fri Dec 2 11:37:32 CET 2022


Hi!
> According to man 3 strtof, it seems there are some cases like:
> 
> 1. strtof return the converted value as normal
> 2. strtof return 0 when no conversion is performed, and endptr == nptr:  'rval == 0'

We can ignore this case since we check that the string was consumed
later on. That's the:

+       if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
+               tst_brkm_(file, lineno, TBROK, cleanup_fn,
+                       "Invalid value: '%s'", str);
+               return 0;
+       }


> 3. strtof return HUGE_VAL or -HUGE_VAL when overflow:      '(rval == HUGE_VAL) || (rval == -HUGE_VAL)'
> 4. strtof retrun 0 and set errno to ERANGE when underflow:   'errno == ERANGE'

My manual says that the value is no larger than DBL_MIN, FLT_MIN, or
LDBL_MIN in this case. Either way I would avoid checking the returned
value in the case errno was set.

> For 2, it seems need to add checking of endptr when 'rval == 0' like:  (rval == 0 && !strcmp(endptr, nptr)

Again, no need to check the actual return value, we can just check that
the string was consumed, which we already do.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list