diff options
| author | Hans Petter Selasky <hselasky@FreeBSD.org> | 2017-03-23 16:01:51 +0000 |
|---|---|---|
| committer | Hans Petter Selasky <hselasky@FreeBSD.org> | 2017-03-23 16:01:51 +0000 |
| commit | d2f312e0e7590f31086f259a183eb339b42315e2 (patch) | |
| tree | f25d3b7de25fd3212e22b5078875cede53084ba1 | |
| parent | 9612674f64cd16c3fe60f72c54acb3a6791c1e5a (diff) | |
Add proper error checking for the string to number conversion
functions in the LinuxKPI.
MFC after: 1 week
Sponsored by: Mellanox Technologies
Notes
svn path=/head/; revision=315863
| -rw-r--r-- | sys/compat/linuxkpi/common/include/linux/kernel.h | 97 |
1 files changed, 92 insertions, 5 deletions
diff --git a/sys/compat/linuxkpi/common/include/linux/kernel.h b/sys/compat/linuxkpi/common/include/linux/kernel.h index 43a4dbd928b8..3b7a83f88f07 100644 --- a/sys/compat/linuxkpi/common/include/linux/kernel.h +++ b/sys/compat/linuxkpi/common/include/linux/kernel.h @@ -256,11 +256,98 @@ scnprintf(char *buf, size_t size, const char *fmt, ...) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#define simple_strtoul(...) strtoul(__VA_ARGS__) -#define simple_strtol(...) strtol(__VA_ARGS__) -#define kstrtol(a,b,c) ({*(c) = strtol(a,0,b); 0;}) -#define kstrtoint(a,b,c) ({*(c) = strtol(a,0,b); 0;}) -#define kstrtouint(a,b,c) ({*(c) = strtol(a,0,b); 0;}) +static inline unsigned long long +simple_strtoull(const char *cp, char **endp, unsigned int base) +{ + return (strtouq(cp, endp, base)); +} + +static inline long long +simple_strtoll(const char *cp, char **endp, unsigned int base) +{ + return (strtoq(cp, endp, base)); +} + +static inline unsigned long +simple_strtoul(const char *cp, char **endp, unsigned int base) +{ + return (strtoul(cp, endp, base)); +} + +static inline long +simple_strtol(const char *cp, char **endp, unsigned int base) +{ + return (strtol(cp, endp, base)); +} + +static inline int +kstrtoul(const char *cp, unsigned int base, unsigned long *res) +{ + char *end; + + *res = strtoul(cp, &end, base); + + if (*cp == 0 || *end != 0) + return (-EINVAL); + return (0); +} + +static inline int +kstrtol(const char *cp, unsigned int base, long *res) +{ + char *end; + + *res = strtol(cp, &end, base); + + if (*cp == 0 || *end != 0) + return (-EINVAL); + return (0); +} + +static inline int +kstrtoint(const char *cp, unsigned int base, int *res) +{ + char *end; + long temp; + + *res = temp = strtol(cp, &end, base); + + if (*cp == 0 || *end != 0) + return (-EINVAL); + if (temp != (int)temp) + return (-ERANGE); + return (0); +} + +static inline int +kstrtouint(const char *cp, unsigned int base, unsigned int *res) +{ + char *end; + unsigned long temp; + + *res = temp = strtoul(cp, &end, base); + + if (*cp == 0 || *end != 0) + return (-EINVAL); + if (temp != (unsigned int)temp) + return (-ERANGE); + return (0); +} + +static inline int +kstrtou32(const char *cp, unsigned int base, u32 *res) +{ + char *end; + unsigned long temp; + + *res = temp = strtoul(cp, &end, base); + + if (*cp == 0 || *end != 0) + return (-EINVAL); + if (temp != (u32)temp) + return (-ERANGE); + return (0); +} #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) |
