svn commit: r315863 - head/sys/compat/linuxkpi/common/include/linux

Hans Petter Selasky hselasky at FreeBSD.org
Thu Mar 23 16:01:52 UTC 2017


Author: hselasky
Date: Thu Mar 23 16:01:51 2017
New Revision: 315863
URL: https://svnweb.freebsd.org/changeset/base/315863

Log:
  Add proper error checking for the string to number conversion
  functions in the LinuxKPI.
  
  MFC after:		1 week
  Sponsored by:		Mellanox Technologies

Modified:
  head/sys/compat/linuxkpi/common/include/linux/kernel.h

Modified: head/sys/compat/linuxkpi/common/include/linux/kernel.h
==============================================================================
--- head/sys/compat/linuxkpi/common/include/linux/kernel.h	Thu Mar 23 14:35:21 2017	(r315862)
+++ head/sys/compat/linuxkpi/common/include/linux/kernel.h	Thu Mar 23 16:01:51 2017	(r315863)
@@ -256,11 +256,98 @@ scnprintf(char *buf, size_t size, const 
   
 #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))


More information about the svn-src-head mailing list