libc: Add Kconfig option to disable hex-string to float parsing

Introduce CONFIG_LIBC_DISABLE_HEXSTR_TO_FLOAT to remove support for
parsing hexadecimal floating-point constants (C99 %a) in strtod(),
strtof(), strtold(), and sscanf("%a"). This feature is rarely used in
embedded systems and its removal saves significant flash space. Decimal
float parsing remains unaffected.

Signed-off-by: Peter van der Perk <peter.vanderperk@nxp.com>
This commit is contained in:
Peter van der Perk
2026-01-07 10:50:39 +01:00
committed by Xiang Xiao
parent 6b5d2fa3b1
commit 0fed06190b
2 changed files with 18 additions and 2 deletions
+10
View File
@@ -46,4 +46,14 @@ config LIBC_MAX_EXITFUNS
Configure the amount of exit functions for atexit/on_exit. The ANSI
default is 32, but most likely we don't need as many.
config LIBC_DISABLE_HEXSTR_TO_FLOAT
bool "Disable hex-string to float/double conversions"
default DEFAULT_SMALL
---help---
Disables parsing of hexadecimal floating-point constants (C99 %a),
e.g., "0x1.fp3", in strtod(), strtof(), strtold(), and sscanf("%a").
This saves flash space since hex-float parsing adds significant code
and is rarely needed in embedded systems. Decimal float parsing remains
available.
endmenu # stdlib Options
+8 -2
View File
@@ -432,7 +432,7 @@ static long_double decfloat(FAR char *ptr, FAR char **endptr)
* A long_double number about ptr
*
****************************************************************************/
#ifndef CONFIG_LIBC_DISABLE_HEXSTR_TO_FLOAT
static long_double hexfloat(FAR char *ptr,
FAR char **endptr, int bits, int emin)
{
@@ -618,6 +618,7 @@ static long_double hexfloat(FAR char *ptr,
return scalbnx(y, 2., e2);
}
#endif
/****************************************************************************
* Name: strtox
@@ -644,6 +645,7 @@ static long_double strtox(FAR const char *str, FAR char **endptr, int flag)
long_double y = 0;
int i = 0;
#ifndef CONFIG_LIBC_DISABLE_HEXSTR_TO_FLOAT
int bits;
int emin;
@@ -664,6 +666,7 @@ static long_double strtox(FAR const char *str, FAR char **endptr, int flag)
default:
return 0;
}
#endif
/* Skip leading whitespace */
@@ -712,12 +715,15 @@ static long_double strtox(FAR const char *str, FAR char **endptr, int flag)
/* Process optional 0x prefix */
s -= i;
#ifndef CONFIG_LIBC_DISABLE_HEXSTR_TO_FLOAT
if (*s == '0' && (*(s + 1) | 32) == 'x')
{
s += 2;
y = hexfloat(s, endptr, bits, emin);
}
else if (isdigit(*s) || (*s == '.' && isdigit(*(s + 1))))
else
#endif
if (isdigit(*s) || (*s == '.' && isdigit(*(s + 1))))
{
y = decfloat(s, endptr);
}