[RFC 1/4] Add "base" parameter to string_to_uint_ex

Sahil Siddiq icegambit91 at gmail.com
Mon Apr 29 03:31:44 UTC 2024


Add a "base" parameter to string_to_uint_ex instead of
hardcoding the base to 10.

* src/strace.c (is_uid_gid_pair): Pass base 10 as argument.
* src/string_to_uint.c (string_to_uint_ex): Introduce "base" parameter.
* src/string_to_uint.h: Likewise.
* src/util.c
  (parse_ts): Pass base 10 as argument.
  (set_tty_index): Likewise.
  (parse_fdinfo_pid): Likewise.

Signed-off-by: Sahil Siddiq <icegambit91 at gmail.com>
---
 src/strace.c         | 4 ++--
 src/string_to_uint.c | 4 ++--
 src/string_to_uint.h | 4 ++--
 src/util.c           | 6 +++---
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/strace.c b/src/strace.c
index 785ee0712..c8f76cada 100644
--- a/src/strace.c
+++ b/src/strace.c
@@ -1528,13 +1528,13 @@ is_uid_gid_pair(const char *username, uid_t *uid_ptr, gid_t *gid_ptr)
 		const unsigned long long max_uid =
 			zero_extend_signed_to_ull((uid_t) -1) - 1;
 		*uid_ptr = (uid_t) string_to_uint_ex(username, NULL,
-						     max_uid, ":");
+						     max_uid, ":", 10);
 	}
 	if (gid_ptr) {
 		const unsigned long long max_gid =
 			zero_extend_signed_to_ull((gid_t) -1) - 1;
 		*gid_ptr = (gid_t) string_to_uint_ex(colon + 1, NULL,
-						     max_gid, ":");
+						     max_gid, ":", 10);
 	}
 	return true;
 }
diff --git a/src/string_to_uint.c b/src/string_to_uint.c
index 6fbd8c168..e06ccac33 100644
--- a/src/string_to_uint.c
+++ b/src/string_to_uint.c
@@ -18,7 +18,7 @@
 long long
 string_to_uint_ex(const char *const str, char **const endptr,
 		  const unsigned long long max_val,
-		  const char *const accepted_ending)
+		  const char *const accepted_ending, int base)
 {
 	char *end;
 	long long val;
@@ -27,7 +27,7 @@ string_to_uint_ex(const char *const str, char **const endptr,
 		return -1;
 
 	errno = 0;
-	val = strtoll(str, &end, 10);
+	val = strtoll(str, &end, base);
 
 	if (str == end || val < 0 || (unsigned long long) val > max_val
 	    || (val == LLONG_MAX && errno == ERANGE))
diff --git a/src/string_to_uint.h b/src/string_to_uint.h
index c02130b51..0bc4d7a92 100644
--- a/src/string_to_uint.h
+++ b/src/string_to_uint.h
@@ -14,12 +14,12 @@
 
 extern long long
 string_to_uint_ex(const char *str, char **endptr,
-		  unsigned long long max_val, const char *accepted_ending);
+		  unsigned long long max_val, const char *accepted_ending, int base);
 
 static inline long long
 string_to_uint_upto(const char *const str, const unsigned long long max_val)
 {
-	return string_to_uint_ex(str, NULL, max_val, NULL);
+	return string_to_uint_ex(str, NULL, max_val, NULL, 10);
 }
 
 static inline int
diff --git a/src/util.c b/src/util.c
index efd390577..4140cb718 100644
--- a/src/util.c
+++ b/src/util.c
@@ -172,7 +172,7 @@ parse_ts(const char *s, struct timespec *t)
 		if (float_val < 0)
 			return -1;
 	} else {
-		int_val = string_to_uint_ex(s, &endptr, LLONG_MAX, "smun");
+		int_val = string_to_uint_ex(s, &endptr, LLONG_MAX, "smun", 10);
 
 		if (int_val < 0)
 			return -1;
@@ -695,7 +695,7 @@ set_tty_index(const char *value, void *data)
 {
 	struct finfo *finfo = data;
 
-	finfo->dev.tty_index = string_to_uint_ex(value, NULL, INT_MAX, "\n");
+	finfo->dev.tty_index = string_to_uint_ex(value, NULL, INT_MAX, "\n", 10);
 	return true;
 }
 
@@ -775,7 +775,7 @@ static bool
 parse_fdinfo_pid(const char *value, void *data)
 {
 	pid_t *pid = data;
-	*pid = string_to_uint_ex(value, NULL, INT_MAX, "\n");
+	*pid = string_to_uint_ex(value, NULL, INT_MAX, "\n", 10);
 	return true;
 }
 
-- 
2.44.0



More information about the Strace-devel mailing list