Copilot commented on code in PR #2150:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2150#discussion_r3073324935


##########
libminifi/include/core/extension/Extension.h:
##########
@@ -40,6 +40,11 @@ class Extension {
     void* user_data;
   };
 
+  struct Context {
+    std::shared_ptr<minifi::Configure> config;
+    std::function<Extension*(Info)> create;
+  };

Review Comment:
   `Extension.h` now uses `std::function` (and returns `std::optional`) but the 
header doesn’t include `<functional>` / `<optional>`. This will fail to compile 
depending on include order; please add the missing standard headers in this 
file.



##########
libminifi/src/core/extension/Extension.cpp:
##########
@@ -158,10 +150,19 @@ bool Extension::initialize(const 
std::shared_ptr<minifi::Configure>& configure)
     return false;
   }
   logger_->log_debug("Found initializer for '{}'", library_name_);
-  auto init_fn = reinterpret_cast<void(*)(MinifiExtension*, 
MinifiConfig*)>(init_symbol_ptr);
-  auto config_handle = reinterpret_cast<MinifiConfig*>(configure.get());
-  auto extension_handle = reinterpret_cast<MinifiExtension*>(this);
-  init_fn(extension_handle, config_handle);
+
+  auto init_fn = 
reinterpret_cast<void(*)(MinifiExtensionContext*)>(init_symbol_ptr);
+  Context extension_context{
+    .config = configure,
+    .create = [&] (Info info) -> Extension* {
+      if (info_) {
+        return nullptr;
+      }
+      info_ = std::move(info);
+      return this;
+    }
+  };
+  init_fn(reinterpret_cast<MinifiExtensionContext*>(&extension_context));
   if (!info_) {

Review Comment:
   `Extension::initialize()` passes a pointer to a stack-allocated `Context` 
(`extension_context`) into extension code. If an extension stores this handle 
and uses it after `MinifiInit*Extension` returns (e.g., via `MinifiConfigGet`), 
this becomes a use-after-free. Please either (1) document clearly in the public 
C API that `MinifiExtensionContext*` is only valid during the init call, or (2) 
make the context storage have a lifetime that outlives the init call 
(member/heap allocation).



##########
minifi-api/include/minifi-c/minifi-c.h:
##########
@@ -188,11 +188,11 @@ typedef struct MinifiExtensionCreateInfo {
   MinifiStringView version;
   void(*deinit)(void* user_data);
   void* user_data;
-  size_t processors_count;
-  const MinifiProcessorClassDefinition* processors_ptr;
 } MinifiExtensionCreateInfo;
 
-MinifiStatus MINIFI_CREATE_EXTENSION_FN(MinifiExtension* extension, const 
MinifiExtensionCreateInfo* create_info);
+MinifiExtension* MINIFI_CREATE_EXTENSION_FN(MinifiExtensionContext* 
extension_context, const MinifiExtensionCreateInfo* create_info);
+
+MinifiStatus MinifiRegisterProcessor(MinifiExtension* extension, const 
MinifiProcessorClassDefinition* processor);
 

Review Comment:
   This PR makes breaking changes to the stable C extension ABI (e.g., 
`MinifiInitExtension`/`MinifiCreateExtension` signatures and replacing 
`MinifiConfig*` with `MinifiExtensionContext*`), but `MINIFI_API_VERSION` in 
this header remains unchanged. Please bump the C API version (and update min 
supported version logic if needed) so older extensions won’t be loaded against 
the new ABI.



##########
Extensions.md:
##########
@@ -28,19 +28,18 @@ Moreover the actual resource registration 
(processors/controller services) has t
 One possible example of this is:
 
 ```C++
-extern "C" const char* const MinifiApiVersion = MINIFI_API_VERSION;
+extern "C" const uint32_t MinifiApiVersion = MINIFI_API_VERSION;
 
-extern "C" void MinifiInitExtension(MinifiExtension* extension, MinifiConfig* 
/*config*/) {
+extern "C" void MinifiInitExtension(MinifiExtensionContext* extension_context) 
{
+  MinifiExtensionCreateInfo ext_create_info{
+    .name = minifi::api::utils::toStringView(MAKESTRING(EXTENSION_NAME)),
+    .version = minifi::api::utils::toStringView(MAKESTRING(EXTENSION_VERSION)),
+    .deinit = nullptr,
+    .user_data = nullptr
+  };
+  auto* extension = MinifiCreateExtension(extension_context, &ext_create_info);
   
minifi::api::core::useProcessorClassDescription<minifi::extensions::llamacpp::processors::RunLlamaCppInference>([&]
 (const MinifiProcessorClassDefinition& description) {
-    MinifiExtensionCreateInfo ext_create_info{
-      .name = minifi::api::utils::toStringView(MAKESTRING(EXTENSION_NAME)),
-      .version = 
minifi::api::utils::toStringView(MAKESTRING(EXTENSION_VERSION)),
-      .deinit = nullptr,
-      .user_data = nullptr,
-      .processors_count = 1,
-      .processors_ptr = &description,
-    };
-    MinifiCreateExtension(extension, &ext_create_info);
+    MinifiRegisterProcessor(extension, &description);
   });

Review Comment:
   The C-extension example now uses `MinifiApiVersion` as a `uint32_t`, but the 
surrounding text still describes a `MinifiCApiVersion` `const char*` symbol. 
Please update the documentation so the symbol name/type description matches the 
actual required symbol. Also consider checking the `MinifiCreateExtension()` 
return value in the example to avoid showing a pattern that can abort on 
failure.



##########
extensions/llamacpp/processors/ExtensionInitializer.cpp:
##########
@@ -26,16 +26,15 @@ namespace minifi = org::apache::nifi::minifi;
 
 CEXTENSIONAPI const uint32_t MinifiApiVersion = MINIFI_API_VERSION;
 
-CEXTENSIONAPI void MinifiInitExtension(MinifiExtension* extension, 
MinifiConfig* /*config*/) {
+CEXTENSIONAPI void MinifiInitExtension(MinifiExtensionContext* 
extension_context) {
+  MinifiExtensionCreateInfo ext_create_info{
+    .name = minifi::api::utils::toStringView(MAKESTRING(EXTENSION_NAME)),
+    .version = minifi::api::utils::toStringView(MAKESTRING(EXTENSION_VERSION)),
+    .deinit = nullptr,
+    .user_data = nullptr
+  };
+  auto* extension = MinifiCreateExtension(extension_context, &ext_create_info);

Review Comment:
   `MinifiCreateExtension()` can return `MINIFI_NULL` (e.g., if creation fails 
or is called more than once). The returned pointer is used unconditionally in 
`MinifiRegisterProcessor`, which will hit `gsl_Assert(extension_handle)` and 
abort. Please guard `extension` and handle the error path (or check the 
returned `MinifiStatus`) before registering processors.
   ```suggestion
     auto* extension = MinifiCreateExtension(extension_context, 
&ext_create_info);
     if (extension == MINIFI_NULL) {
       return;
     }
   ```



##########
libminifi/include/core/extension/Extension.h:
##########
@@ -51,7 +56,9 @@ class Extension {
 
   bool initialize(const std::shared_ptr<minifi::Configure>& configure);
 
-  bool setInfo(Info info);
+  std::optional<Info> getInfo() const {
+    return info_.value();

Review Comment:
   `getInfo()` returns `std::optional<Info>` but currently does `return 
info_.value();`, which throws `std::bad_optional_access` when `info_` is unset 
(e.g., if an extension calls `MinifiRegisterProcessor` before 
`MinifiCreateExtension`). Return `info_` directly (or `std::nullopt` when 
empty) to keep this API non-throwing.
   ```suggestion
       return info_;
   ```



##########
libminifi/src/minifi-c.cpp:
##########
@@ -225,37 +225,49 @@ void useCProcessorClassDescription(const 
MinifiProcessorClassDefinition& class_d
 
 extern "C" {
 
-MinifiStatus MinifiCreateExtension(MinifiExtension* extension, const 
MinifiExtensionCreateInfo* extension_create_info) {
-  gsl_Assert(extension);
+MinifiExtension* MinifiCreateExtension(MinifiExtensionContext* 
extension_context, const MinifiExtensionCreateInfo* extension_create_info) {
+  gsl_Assert(extension_context);
   gsl_Assert(extension_create_info);
   auto extension_name = toString(extension_create_info->name);
   minifi::BundleIdentifier bundle{
     .name = extension_name,
     .version = toString(extension_create_info->version)
   };
-  auto& bundle_components = 
minifi::ClassDescriptionRegistry::getMutableClassDescriptions()[bundle];
-  for (size_t proc_idx = 0; proc_idx < 
extension_create_info->processors_count; ++proc_idx) {
-    
minifi::utils::useCProcessorClassDescription(extension_create_info->processors_ptr[proc_idx],
 [&] (const auto& description, const auto& c_class_description) {
-      
minifi::core::ClassLoader::getDefaultClassLoader().getClassLoader(extension_name).registerClass(
-        c_class_description.name,
-        std::make_unique<CProcessorFactory>(extension_name, 
toString(extension_create_info->processors_ptr[proc_idx].full_name), 
c_class_description));
-      bundle_components.processors.emplace_back(description);
-    });
-  }
-  bool success = 
reinterpret_cast<org::apache::nifi::minifi::core::extension::Extension*>(extension)->setInfo(org::apache::nifi::minifi::core::extension::Extension::Info{
+  auto* extension = 
reinterpret_cast<org::apache::nifi::minifi::core::extension::Extension::Context*>(extension_context)->create(org::apache::nifi::minifi::core::extension::Extension::Info{

Review Comment:
   `bundle` is constructed but no longer used in `MinifiCreateExtension()`, 
which will trigger an `-Wunused-variable` warning. Either remove it or use it 
(e.g., if you still intend to pre-create the registry entry for the bundle).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to