]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxtables: Introduce xtables_strtoul_base()
authorPhil Sutter <phil@nwl.cc>
Wed, 22 Nov 2023 19:41:34 +0000 (20:41 +0100)
committerPhil Sutter <phil@nwl.cc>
Thu, 23 Nov 2023 17:01:21 +0000 (18:01 +0100)
Semantically identical to xtables_strtoul() but accepts the base as
parameter so callers may force it irrespective of number prefix. The old
xtables_strtoul() becomes a shallow wrapper.

Signed-off-by: Phil Sutter <phil@nwl.cc>
include/xtables.h
libxtables/xtables.c

index 087a1d600f9ae433760de5f9cff0955ec16f1f37..1a9e08bb131abf0a4d1358881a6a4e74e312e405 100644 (file)
@@ -486,6 +486,8 @@ extern void xtables_register_matches(struct xtables_match *, unsigned int);
 extern void xtables_register_target(struct xtables_target *me);
 extern void xtables_register_targets(struct xtables_target *, unsigned int);
 
+extern bool xtables_strtoul_base(const char *, char **, uintmax_t *,
+       uintmax_t, uintmax_t, unsigned int);
 extern bool xtables_strtoul(const char *, char **, uintmax_t *,
        uintmax_t, uintmax_t);
 extern bool xtables_strtoui(const char *, char **, unsigned int *,
index ba9ceaeb3da4186e97ac8a16531a618b401f3232..b4339e8d312750351ef8e3cc5a94ccd3231a9007 100644 (file)
@@ -580,23 +580,23 @@ int xtables_load_ko(const char *modprobe, bool quiet)
 }
 
 /**
- * xtables_strtou{i,l} - string to number conversion
+ * xtables_strtoul_base - string to number conversion
  * @s: input string
  * @end:       like strtoul's "end" pointer
  * @value:     pointer for result
  * @min:       minimum accepted value
  * @max:       maximum accepted value
+ * @base:      assumed base of value
  *
  * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
  * "15a" is rejected.
  * In either case, the value obtained is compared for min-max compliance.
- * Base is always 0, i.e. autodetect depending on @s.
  *
  * Returns true/false whether number was accepted. On failure, *value has
  * undefined contents.
  */
-bool xtables_strtoul(const char *s, char **end, uintmax_t *value,
-                     uintmax_t min, uintmax_t max)
+bool xtables_strtoul_base(const char *s, char **end, uintmax_t *value,
+                         uintmax_t min, uintmax_t max, unsigned int base)
 {
        uintmax_t v;
        const char *p;
@@ -608,7 +608,7 @@ bool xtables_strtoul(const char *s, char **end, uintmax_t *value,
                ;
        if (*p == '-')
                return false;
-       v = strtoumax(s, &my_end, 0);
+       v = strtoumax(s, &my_end, base);
        if (my_end == s)
                return false;
        if (end != NULL)
@@ -625,6 +625,12 @@ bool xtables_strtoul(const char *s, char **end, uintmax_t *value,
        return false;
 }
 
+bool xtables_strtoul(const char *s, char **end, uintmax_t *value,
+                    uintmax_t min, uintmax_t max)
+{
+       return xtables_strtoul_base(s, end, value, min, max, 0);
+}
+
 bool xtables_strtoui(const char *s, char **end, unsigned int *value,
                      unsigned int min, unsigned int max)
 {