diff mbox series

[v2,08/11] libtraceeval: Add size checks to insert and query functions

Message ID 20230927123314.989589-9-rostedt@goodmis.org (mailing list archive)
State Accepted
Headers show
Series libtraceeval: Even more updates! | expand

Commit Message

Steven Rostedt Sept. 27, 2023, 12:33 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Currently, there's nothing that checks the size of the keys/values being
passed into traceeval_query() and traceeval_insert(). Make functions that
take the size of those arrays:

   traceeval_query_size()
   traceeval_insert_size()

and convert the traceeval_query() and traceeval_insert() into macros that
calculate the size of the fields to pass in:

  #define TRACEEVAL_ARRAY_SIZE(data)  \
     	(sizeof(data) / sizeof((data)[0]))

  #define traceeval_query(teval, keys, results) \
     traceeval_query_size(teval, TRACEEVAL_ARRAY_SIZE(keys), results)

and this will allow the code to check to make sure the size matches what
is expected (note, that currently is not done, but can be added).

If the keys or vals is not an array, but instead a pointer, if one of the
macros is used, the compiler will complain with:

 warning: division sizeof (const struct traceeval_data *) / sizeof (const struct traceeval_data)
          does not compute the number of array elements [-Wsizeof-pointer-div]
   20 | #define TRACEEVAL_ARRAY_SIZE(data)      (sizeof(data) / sizeof((data)[0]))
      |                                                       ^
 include/traceeval-hist.h:206:43: note: in expansion of macro TRACEEVAL_ARRAY_SIZE
  206 |         traceeval_query_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), results)
      |                                           ^~~~~~~~~~~~~~~~~~~~

Then the user would need to call the "_size()" function directly
(as is done in the sample code, where the keys received by an iterator is
 used for a query)

Now that there are size functions for traceeval_insert() and
traceveal_query(), add checks to make sure that the size being passed in
is actually the size expected.

Reviewed-by: Ross Zwisler <zwisler@google.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 include/traceeval-hist.h | 19 ++++++++++++++-----
 src/histograms.c         | 16 +++++++++++-----
 2 files changed, 25 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/include/traceeval-hist.h b/include/traceeval-hist.h
index 78cfac5982ee..7f48bb92cc96 100644
--- a/include/traceeval-hist.h
+++ b/include/traceeval-hist.h
@@ -17,6 +17,8 @@ 
 /* Field name/descriptor for number of hits */
 #define TRACEEVAL_VAL_HITS ((const char *)(-1UL))
 
+#define TRACEEVAL_ARRAY_SIZE(data)	(sizeof(data) / sizeof((data)[0]))
+
 /* Data type distinguishers */
 enum traceeval_data_type {
 	TRACEEVAL_TYPE_NONE,
@@ -186,15 +188,22 @@  struct traceeval *traceeval_init(struct traceeval_type *keys,
 
 void traceeval_release(struct traceeval *teval);
 
-int traceeval_insert(struct traceeval *teval,
-		     const struct traceeval_data *keys,
-		     const struct traceeval_data *vals);
+int traceeval_insert_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys,
+			  const struct traceeval_data *vals, size_t nr_vals);
+
+#define traceeval_insert(teval, keys, vals)				\
+	traceeval_insert_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), \
+			      vals, TRACEEVAL_ARRAY_SIZE(vals))
 
 int traceeval_remove(struct traceeval *teval,
 		     const struct traceeval_data *keys);
 
-int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
-		    const struct traceeval_data **results);
+int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
+			 size_t nr_keys, const struct traceeval_data **results);
+
+#define traceeval_query(teval, keys, results)				\
+	traceeval_query_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), results)
 
 void traceeval_results_release(struct traceeval *teval,
 			       const struct traceeval_data *results);
diff --git a/src/histograms.c b/src/histograms.c
index 954042da28e4..ab8a560fe14d 100644
--- a/src/histograms.c
+++ b/src/histograms.c
@@ -675,8 +675,8 @@  fail:
  *
  * Returns 1 if found, 0 if not found, and -1 on error.
  */
-int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
-		    const struct traceeval_data **results)
+int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
+			 size_t nr_keys, const struct traceeval_data **results)
 {
 	struct entry *entry;
 	int check;
@@ -684,6 +684,9 @@  int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
 	if (!teval || !keys || !results)
 		return -1;
 
+	if (nr_keys != teval->nr_key_types)
+		return -1;
+
 	/* find key and copy its corresponding value pair */
 	if ((check = get_entry(teval, keys, &entry)) < 1)
 		return check;
@@ -933,14 +936,17 @@  unsigned long long traceeval_stat_count(struct traceeval_stat *stat)
  *
  * Returns 0 on success, and -1 on error.
  */
-int traceeval_insert(struct traceeval *teval,
-		     const struct traceeval_data *keys,
-		     const struct traceeval_data *vals)
+int traceeval_insert_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys,
+			  const struct traceeval_data *vals, size_t nr_vals)
 {
 	struct entry *entry;
 	int check;
 	int i;
 
+	if (nr_keys != teval->nr_key_types || nr_vals != teval->nr_val_types)
+		return -1;
+
 	entry = NULL;
 	check = get_entry(teval, keys, &entry);