Skip to contentSkip to conversation

lib/safe_macros: Add SAFE_STRTOF

PATCH zhaogongyi via ltp 1 participant 1 message Dec 5, 2022

lore
Share & tools
lore
#0 lore raw

Hi Cyril,

Quoted 17 lines

Hi!

Quoted 4 lines

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:

(quoted diff) +5 -0
+       if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
+               tst_brkm_(file, lineno, TBROK, cleanup_fn,
+                       "Invalid value: '%s'", str);
+               return 0;
+       }
Quoted 8 lines
Quoted 1 line

3. strtof return HUGE_VAL or -HUGE_VAL when overflow: '(rval ==

HUGE_VAL) || (rval == -HUGE_VAL)'

Quoted 1 line

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.

I usually look for information here: https://pubs.opengroup.org/onlinepubs/9699919799/

Quoted 7 lines
Quoted 3 lines

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.

Thanks! I have resubmit the patch as your suggestion.

Regards,
Gongyi

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp