[LTP] [PATCH v2 1/2] lib/safe_macros: Add SAFE_STRTOF
Cyril Hrubis
chrubis@suse.cz
Thu Dec 1 14:42:33 CET 2022
Hi!
> +float safe_strtof(const char *file, const int lineno,
> + void (cleanup_fn) (void), char *str,
> + float min, float max)
> +{
> + float rval;
> + char *endptr;
> +
> + errno = 0;
> + rval = strtof(str, &endptr);
> +
> + if ((errno == ERANGE) || (rval == 0)
> + || (rval == HUGE_VAL) || (rval == -HUGE_VAL)) {
This does not look right, supposedly the ERANGE is only set on overflow,
and the rval is not guaranteed to be exact on underflow. Also rval == 0
is a valid result.
We do zero errno above so I would just do:
if (errno) {
tst_brkm_(...);
return rval;
}
> + tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
> + "strtof(%s) failed", str);
> + return rval;
> + }
> +
> + if (rval > max || rval < min) {
> + tst_brkm_(file, lineno, TBROK, cleanup_fn,
> + "strtof(%s): %f is out of range %f - %f",
> + str, rval, min, max);
> + return 0;
> + }
> +
> + if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
> + tst_brkm_(file, lineno, TBROK, cleanup_fn,
> + "Invalid value: '%s'", str);
> + return 0;
> + }
> +
> + return rval;
> +}
Otherwise it looks good.
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list