aboutsummaryrefslogtreecommitdiffstats
path: root/include/osmocom
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2021-09-05 18:48:31 +0200
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2021-09-12 21:24:50 +0200
commit47773344fb20bb25b68d5e0b91d76ed781da140d (patch)
treec2427b124b740b78b366f9fe808c6c1733f5e2f7 /include/osmocom
parent11a58a1b34f8bba398f16358a863a0ab273c8307 (diff)
utils: add osmo_str_to_int() and osmo_str_to_int64()
Properly converting a string to an integer while validating against all possible errors is not trivial. It is a recurring theme in code review, and there are places in osmo code that do it wrong. End this by providing a simple API, if for nothing else then as an example of how to use strol() / strtoul() / strtoll() / strtoull() in an airtight way. A subsequent patch, adding stat items to the CTRL interface, uses this to properly validate indexes in CTRL variables and convert them to int. Related: SYS#5542 Change-Id: I4dac826aab00bc1780a5258b6b55d34ce7d50c60
Diffstat (limited to 'include/osmocom')
-rw-r--r--include/osmocom/core/utils.h3
1 files changed, 3 insertions, 0 deletions
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 1c603904..85a8cb31 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -283,6 +283,9 @@ int osmo_float_str_to_int(int64_t *val, const char *str, unsigned int precision)
int osmo_int_to_float_str_buf(char *buf, size_t buflen, int64_t val, unsigned int precision);
char *osmo_int_to_float_str_c(void *ctx, int64_t val, unsigned int precision);
+int osmo_str_to_int64(int64_t *result, const char *str, int base, int64_t min_val, int64_t max_val);
+int osmo_str_to_int(int *result, const char *str, int base, int min_val, int max_val);
+
/*! Translate a buffer function to a talloc context function.
* This is the full function body of a char *foo_name_c(void *ctx, val...) function, implemented by an
* int foo_name_buf(buf, buflen, val...) function: