From 5f4f7eccc53e18bb3ad8ea6ef1b3a5684200bcc4 Mon Sep 17 00:00:00 2001 From: Grzegorz Nosek Date: Thu, 7 Mar 2024 11:09:42 +0100 Subject: [PATCH] fix(plugins): make list_fields return a const pointer The fields returned by a plugin can be a static string and the plugin framework doesn't have any business in modifying it so let's make it const (to avoid a copy for plugins that can return a static string). Note: I only bump the plugin API version by 0.0.1 since the changes in this PR do not affect binary compatibility in any way. Signed-off-by: Grzegorz Nosek --- userspace/libsinsp/plugin_table_api.cpp | 4 ++-- userspace/libsinsp/test/plugins/sample_table.h | 2 +- userspace/plugin/plugin_api.h | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/userspace/libsinsp/plugin_table_api.cpp b/userspace/libsinsp/plugin_table_api.cpp index b7fbe4cb91..a83d962635 100755 --- a/userspace/libsinsp/plugin_table_api.cpp +++ b/userspace/libsinsp/plugin_table_api.cpp @@ -734,7 +734,7 @@ struct sinsp_table_wrapper const sinsp_plugin* m_table_plugin_owner; ss_plugin_table_input* m_table_plugin_input; - static ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields) + static const ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields) { auto t = static_cast(_t); @@ -1231,7 +1231,7 @@ ss_plugin_rc sinsp_table_wrapper::write_entry_field(ss_plugin_table_t* _t, ss_pl // For sinsp-defined tables, the ss_plugin_table_input is a wrapper around // the libsinsp::state::table interface. For plugin-defined tables, the // ss_plugin_table_input is provided by the table-owner plugin itself. -static ss_plugin_table_fieldinfo* dispatch_list_fields(ss_plugin_table_t *_t, uint32_t *nfields) +static const ss_plugin_table_fieldinfo* dispatch_list_fields(ss_plugin_table_t *_t, uint32_t *nfields) { auto t = static_cast(_t); return t->fields_ext->list_table_fields(t->table, nfields); diff --git a/userspace/libsinsp/test/plugins/sample_table.h b/userspace/libsinsp/test/plugins/sample_table.h index f6df195afd..a172b89774 100644 --- a/userspace/libsinsp/test/plugins/sample_table.h +++ b/userspace/libsinsp/test/plugins/sample_table.h @@ -74,7 +74,7 @@ class sample_table return t->entries.size(); } - static ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields) + static const ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields) { auto t = static_cast(_t); *nfields = (uint32_t) t->fields.size(); diff --git a/userspace/plugin/plugin_api.h b/userspace/plugin/plugin_api.h index c4f91ca71a..7caa82dc43 100644 --- a/userspace/plugin/plugin_api.h +++ b/userspace/plugin/plugin_api.h @@ -31,7 +31,7 @@ extern "C" { // todo(jasondellaluce): when/if major changes to v4, check and solve all todos #define PLUGIN_API_VERSION_MAJOR 3 #define PLUGIN_API_VERSION_MINOR 4 -#define PLUGIN_API_VERSION_PATCH 0 +#define PLUGIN_API_VERSION_PATCH 1 // // Just some not so smart defines to retrieve plugin api version as string @@ -51,7 +51,7 @@ extern "C" { // give this name to the associated *_ext struct. typedef struct { - ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields); + const ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields); ss_plugin_table_field_t* (*get_table_field)(ss_plugin_table_t* t, const char* name, ss_plugin_state_type data_type); ss_plugin_table_field_t* (*add_table_field)(ss_plugin_table_t* t, const char* name, ss_plugin_state_type data_type); } ss_plugin_table_fields_vtable; @@ -66,7 +66,7 @@ typedef struct // available in the entries of the table. nfields will be filled with the number // of elements of the returned array. The array's memory is owned by the // tables's owner. Returns NULL in case of error. - ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields); + const ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields); // // Returns an opaque pointer representing an accessor to a data field // present in all entries of the table, given its name and type.