From: Alejandro Colomar Date: Sun, 10 Dec 2023 23:51:05 +0000 (+0100) Subject: lib/atoi/strtou_noneg.[ch]: Add strtou[l]l_noneg() X-Git-Tag: 4.15.0-rc1~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a2646f67649ad8f8c25159d4c2ff26565d2874a;p=thirdparty%2Fshadow.git lib/atoi/strtou_noneg.[ch]: Add strtou[l]l_noneg() These functions reject negative numbers, instead of silently converting them into unsigned, which strtou[l]l(3) do. Reviewed-by: Iker Pedrosa Cc: "Serge E. Hallyn" Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 308099fc2..0254977bc 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -30,6 +30,8 @@ libshadow_la_SOURCES = \ agetpass.h \ alloc.c \ alloc.h \ + atoi/strtou_noneg.c \ + atoi/strtou_noneg.h \ attr.h \ audit_help.c \ basename.c \ diff --git a/lib/atoi/strtou_noneg.c b/lib/atoi/strtou_noneg.c new file mode 100644 index 000000000..14602a9b1 --- /dev/null +++ b/lib/atoi/strtou_noneg.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2023, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/strtou_noneg.h" + + +extern inline unsigned long strtoul_noneg(const char *s, + char **restrict endp, int base); +extern inline unsigned long long strtoull_noneg(const char *s, + char **restrict endp, int base); diff --git a/lib/atoi/strtou_noneg.h b/lib/atoi/strtou_noneg.h new file mode 100644 index 000000000..5670dc9ba --- /dev/null +++ b/lib/atoi/strtou_noneg.h @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2023, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_STRTOU_NONEG_H_ +#define SHADOW_INCLUDE_LIB_ATOI_STRTOU_NONEG_H_ + + +#include + +#include +#include + +#include "attr.h" + + +ATTR_STRING(1) ATTR_ACCESS(write_only, 2) +inline unsigned long strtoul_noneg(const char *s, + char **restrict endp, int base); +ATTR_STRING(1) ATTR_ACCESS(write_only, 2) +inline unsigned long long strtoull_noneg(const char *s, + char **restrict endp, int base); + + +inline unsigned long +strtoul_noneg(const char *s, char **restrict endp, int base) +{ + if (strtol(s, endp, base) < 0) { + errno = ERANGE; + return 0; + } + return strtoul(s, endp, base); +} + + +inline unsigned long long +strtoull_noneg(const char *s, char **restrict endp, int base) +{ + if (strtol(s, endp, base) < 0) { + errno = ERANGE; + return 0; + } + return strtoull(s, endp, base); +} + + +#endif // include guard