From 535808c4d93e4637577aa17bf8413a41920dd2d8 Mon Sep 17 00:00:00 2001 From: Joanne Koong Date: Mon, 7 Oct 2024 13:37:20 -0700 Subject: Add libfuse util strtol wrapper Add a wrapper around strtol for more rigorous error checking and convert uses of atoi and strtol to use this instead. --- lib/util.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/util.c (limited to 'lib/util.c') diff --git a/lib/util.c b/lib/util.c new file mode 100644 index 0000000..a529d38 --- /dev/null +++ b/lib/util.c @@ -0,0 +1,27 @@ +#include +#include + +#include "util.h" + +int libfuse_strtol(const char *str, long *res) +{ + char *endptr; + int base = 10; + long val; + + errno = 0; + + if (!str) + return -EINVAL; + + val = strtol(str, &endptr, base); + + if (errno) + return -errno; + + if (endptr == str || *endptr != '\0') + return -EINVAL; + + *res = val; + return 0; +} -- cgit v1.2.3