Add proper error checking for the string to number conversion

functions in the LinuxKPI.

MFC after:		1 week
Sponsored by:		Mellanox Technologies
This commit is contained in:
hselasky 2017-03-23 16:01:51 +00:00
parent a78ff3f4f3
commit 98637e5e2e

View File

@ -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))