diff mbox series

[1/2] tracefs: Use new BUILD_BUG_ON() and ARRAY_SIZE() macros for options_map intergity

Message ID 20210202200010.887769022@goodmis.org (mailing list archive)
State Accepted
Commit c90f84645e4847de82c3898c1ecbad9f1301f7f5
Headers show
Series libtracefs: Minor formating updates to options code | expand

Commit Message

Steven Rostedt Feb. 2, 2021, 7:54 p.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

Instead of having the options_map use TRACEFS_OPTIONS_MAX as its index, have
the array extend by the elements, and use the new macros BUILD_BUG_ON() and
ARRAY_SIZE() to make sure that the array always is the same size as the
TRACEFS_OPTIONS_MAX.

No functional changes.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 include/tracefs-local.h | 6 ++++++
 src/tracefs-tools.c     | 5 ++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/include/tracefs-local.h b/include/tracefs-local.h
index 3a7ec32a8853..187870e7c491 100644
--- a/include/tracefs-local.h
+++ b/include/tracefs-local.h
@@ -8,6 +8,12 @@ 
 
 #define __hidden __attribute__((visibility ("hidden")))
 
+#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
+
+/* Will cause a division by zero warning if cond is true */
+#define BUILD_BUG_ON(cond)			\
+	do { if (!(1/!(cond))) { } } while (0)
+
 /* Can be overridden */
 void warning(const char *fmt, ...);
 
diff --git a/src/tracefs-tools.c b/src/tracefs-tools.c
index 6e7fce53cef1..7f69cde93115 100644
--- a/src/tracefs-tools.c
+++ b/src/tracefs-tools.c
@@ -19,7 +19,7 @@ 
 
 #define TRACE_CTRL	"tracing_on"
 
-static const char * const options_map[TRACEFS_OPTION_MAX] = {
+static const char * const options_map[] = {
 	"unknown", "annotate", "bin", "blk_cgname", "blk_cgroup", "blk_classic",
 	"block", "context-info", "disable_on_free", "display-graph", "event-fork",
 	"funcgraph-abstime", "funcgraph-cpu", "funcgraph-duration", "funcgraph-irqs",
@@ -132,6 +132,9 @@  int tracefs_trace_off_fd(int fd)
  */
 const char *tracefs_option_name(enum tracefs_option_id id)
 {
+	/* Make sure options map contains all the options */
+	BUILD_BUG_ON(ARRAY_SIZE(options_map) != TRACEFS_OPTION_MAX);
+
 	if (id < TRACEFS_OPTION_MAX)
 		return options_map[id];