From: Cyril Hrubis <chrubis@suse.cz>
To: Zhao Gongyi <zhaogongyi@huawei.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2 1/2] lib/safe_macros: Add SAFE_STRTOF
Date: Thu, 1 Dec 2022 14:42:33 +0100 [thread overview]
Message-ID: <Y4ivSaZhuMSevMR2@yuki> (raw)
In-Reply-To: <20221201025141.71227-2-zhaogongyi@huawei.com>
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2022-12-01 13:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-01 2:51 [LTP] [PATCH v2 0/2] Add handling of abnormal input for parse_opts() Zhao Gongyi via ltp
2022-12-01 2:51 ` [LTP] [PATCH v2 1/2] lib/safe_macros: Add SAFE_STRTOF Zhao Gongyi via ltp
2022-12-01 13:42 ` Cyril Hrubis [this message]
2022-12-01 2:51 ` [LTP] [PATCH v2 2/2] lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF Zhao Gongyi via ltp
-- strict thread matches above, loose matches on Subject: below --
2022-12-02 2:34 [LTP] [PATCH v2 1/2] lib/safe_macros: Add SAFE_STRTOF zhaogongyi via ltp
2022-12-02 10:37 ` Cyril Hrubis
2022-12-02 2:54 zhaogongyi via ltp
2022-12-05 7:03 zhaogongyi via ltp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y4ivSaZhuMSevMR2@yuki \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
--cc=zhaogongyi@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox