diff mbox

[1/2] vla: define new safe vla macros

Message ID 1520400451-11475-2-git-send-email-me@tobin.cc (mailing list archive)
State New, archived
Headers show

Commit Message

Tobin Harding March 7, 2018, 5:27 a.m. UTC
We would like to get rid of VLAs all together but in the mean time we
can make their usage safer.

Define a macro to use for declaring a VLA.  Macro includes requested
'size' and 'max' allowed.  Macro allocates storage for an array
equivalent to

	int array[min(size, max)];

We also define a macro to check whether maximum size was reached.

Signed-off-by: Tobin C. Harding <me@tobin.cc>
---
 include/linux/vla.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 include/linux/vla.h
diff mbox

Patch

diff --git a/include/linux/vla.h b/include/linux/vla.h
new file mode 100644
index 000000000000..ca0510d5e416
--- /dev/null
+++ b/include/linux/vla.h
@@ -0,0 +1,15 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_VLA_H
+#define _LINUX_VLA_H
+
+#define VLA_DEFAULT_MAX 256
+
+/* evaluates 'size' and 'max' twice */
+#define VLA_SAFE(type, vla, size, max) type vla[(size) < (max) ? (size) : (max)]
+
+#define VLA_WARN_OVERSIZE(vla, size)				\
+do {								\
+	WARN(sizeof(vla) < size, "vla maximum exceeded\n");	\
+} while (0)
+
+#endif	/* _LINUX_VLA_H */