From 5f0f989e98e9ca9f273d27d186fc495dfe429a3d Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Sun, 29 Mar 2026 15:17:17 +0200 Subject: [PATCH] feat: add cli_parse_ufloat for non-negative float parsing --- include/cli_parse_utils.h | 1 + src/Makefile.am | 3 ++- src/parse_utils/parse_ufloat.c | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/parse_utils/parse_ufloat.c diff --git a/include/cli_parse_utils.h b/include/cli_parse_utils.h index 1ea87ae..7cff7f9 100644 --- a/include/cli_parse_utils.h +++ b/include/cli_parse_utils.h @@ -6,5 +6,6 @@ int cli_parse_uint64(const char *s, uint64_t *out); int cli_parse_int64(const char *s, int64_t *out); int cli_parse_float(const char *s, float *out); +int cli_parse_ufloat(const char *s, float *out); #endif diff --git a/src/Makefile.am b/src/Makefile.am index 2e0e5a3..b7a97f3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,7 +5,8 @@ libcli_la_SOURCES = \ help.c \ parse_utils/parse_uint.c \ parse_utils/parse_int.c \ - parse_utils/parse_float.c + parse_utils/parse_float.c \ + parse_utils/parse_ufloat.c libcli_la_CPPFLAGS = -I$(top_srcdir)/include libcli_la_CFLAGS = -std=c99 -Wall -Wextra diff --git a/src/parse_utils/parse_ufloat.c b/src/parse_utils/parse_ufloat.c new file mode 100644 index 0000000..8f2d373 --- /dev/null +++ b/src/parse_utils/parse_ufloat.c @@ -0,0 +1,21 @@ +#include +#include +#include + +int +cli_parse_ufloat(const char *s, float *out) +{ + char *end; + + if (NULL == s || '\0' == *s) + return 1; + + errno = 0; + float v = strtof(s, &end); + + if (s == end || ERANGE == errno || '\0' != *end || 0 == isfinite(v) || v < 0.0f) + return 1; + + *out = v; + return 0; +}