-
Notifications
You must be signed in to change notification settings - Fork 84
Fix memory leaks, error handling, and type-safety issues in N-API bindings #1422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,7 @@ Napi::Value ActionTakeResultRequest(const Napi::CallbackInfo& info) { | |
| return js_obj; | ||
| } | ||
|
|
||
| free(header); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
|
|
@@ -148,6 +149,7 @@ Napi::Value ActionTakeGoalRequest(const Napi::CallbackInfo& info) { | |
| return js_obj; | ||
| } | ||
|
|
||
| free(header); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
|
|
@@ -221,14 +223,14 @@ Napi::Value ActionTakeGoalResponse(const Napi::CallbackInfo& info) { | |
| free(header); | ||
|
|
||
| if (ret != RCL_RET_OK && ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) { | ||
| std::string error_msg = rcl_get_error_string().str; | ||
| rcl_reset_error(); | ||
| Napi::Error::New(env, rcl_get_error_string().str) | ||
| .ThrowAsJavaScriptException(); | ||
| Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
| if (ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) { | ||
| return Napi::Number::New(env, static_cast<int32_t>(sequence_number)); | ||
| return Napi::Number::New(env, static_cast<double>(sequence_number)); | ||
| } | ||
|
Comment on lines
232
to
234
|
||
| return env.Undefined(); | ||
| } | ||
|
|
@@ -250,14 +252,14 @@ Napi::Value ActionTakeCancelResponse(const Napi::CallbackInfo& info) { | |
| free(header); | ||
|
|
||
| if (ret != RCL_RET_OK && ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) { | ||
| std::string error_msg = rcl_get_error_string().str; | ||
| rcl_reset_error(); | ||
| Napi::Error::New(env, rcl_get_error_string().str) | ||
| .ThrowAsJavaScriptException(); | ||
| Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
| if (ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) { | ||
| return Napi::Number::New(env, static_cast<int32_t>(sequence_number)); | ||
| return Napi::Number::New(env, static_cast<double>(sequence_number)); | ||
| } | ||
| return env.Undefined(); | ||
| } | ||
|
|
@@ -279,14 +281,14 @@ Napi::Value ActionTakeResultResponse(const Napi::CallbackInfo& info) { | |
| free(header); | ||
|
|
||
| if (ret != RCL_RET_OK && ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) { | ||
| std::string error_msg = rcl_get_error_string().str; | ||
| rcl_reset_error(); | ||
| Napi::Error::New(env, rcl_get_error_string().str) | ||
| .ThrowAsJavaScriptException(); | ||
| Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
| if (ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) { | ||
| return Napi::Number::New(env, static_cast<int32_t>(sequence_number)); | ||
| return Napi::Number::New(env, static_cast<double>(sequence_number)); | ||
| } | ||
| return env.Undefined(); | ||
| } | ||
|
|
@@ -463,6 +465,7 @@ Napi::Value ActionTakeCancelRequest(const Napi::CallbackInfo& info) { | |
| return js_obj; | ||
| } | ||
|
|
||
| free(header); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
|
|
||
| #include <cstdio> | ||
| #include <memory> | ||
| #include <rcpputils/scope_exit.hpp> | ||
| // NOLINTNEXTLINE | ||
| #include <string> | ||
|
|
||
| #include "macros.h" | ||
|
|
@@ -108,12 +110,6 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { | |
| rcl_ret_t ret = rcl_subscription_options_set_content_filter_options( | ||
| expression.c_str(), argc, (const char**)argv, &subscription_ops); | ||
|
|
||
| if (ret != RCL_RET_OK) { | ||
| std::string error_string = rcl_get_error_string().str; | ||
| rcl_reset_error(); | ||
| Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); | ||
| } | ||
|
|
||
| if (argc) { | ||
| for (int i = 0; i < argc; i++) { | ||
| free(argv[i]); | ||
|
|
@@ -122,7 +118,10 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { | |
| } | ||
|
|
||
| if (ret != RCL_RET_OK) { | ||
| std::string error_string = rcl_get_error_string().str; | ||
| rcl_reset_error(); | ||
| free(subscription); | ||
| Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); | ||
| return env.Undefined(); | ||
| } | ||
| } | ||
|
|
@@ -137,8 +136,8 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { | |
| if (ret != RCL_RET_OK) { | ||
| std::string error_msg = rcl_get_error_string().str; | ||
| rcl_reset_error(); | ||
| Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); | ||
| free(subscription); | ||
| Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
|
|
@@ -157,9 +156,9 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { | |
|
|
||
| return js_obj; | ||
| } else { | ||
| Napi::Error::New(env, GetErrorMessageAndClear()) | ||
| .ThrowAsJavaScriptException(); | ||
| std::string error_msg = GetErrorMessageAndClear(); | ||
| free(subscription); | ||
| Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); | ||
| return env.Undefined(); | ||
| } | ||
| } | ||
|
|
@@ -194,10 +193,16 @@ Napi::Value RclTakeRaw(const Napi::CallbackInfo& info) { | |
| return env.Undefined(); | ||
| } | ||
|
|
||
| RCPPUTILS_SCOPE_EXIT({ | ||
| rcl_ret_t fini_ret = rmw_serialized_message_fini(&msg); | ||
| if (fini_ret != RCL_RET_OK) { | ||
| rcl_reset_error(); | ||
| } | ||
| }); | ||
|
|
||
| Napi::Buffer<char> buffer = Napi::Buffer<char>::Copy( | ||
| env, reinterpret_cast<char*>(msg.buffer), msg.buffer_length); | ||
| THROW_ERROR_IF_NOT_EQUAL(rmw_serialized_message_fini(&msg), RCL_RET_OK, | ||
| "Failed to deallocate message buffer"); | ||
|
|
||
| return buffer; | ||
| } | ||
|
|
||
|
|
@@ -369,6 +374,14 @@ Napi::Value GetContentFilter(const Napi::CallbackInfo& info) { | |
| return env.Undefined(); | ||
| } | ||
|
|
||
| RCPPUTILS_SCOPE_EXIT({ | ||
| rcl_ret_t fini_ret = | ||
| rcl_subscription_content_filter_options_fini(subscription, &options); | ||
| if (fini_ret != RCL_RET_OK) { | ||
| rcl_reset_error(); | ||
| } | ||
| }); | ||
|
Comment on lines
+377
to
+383
|
||
|
|
||
| // Create result object | ||
| Napi::Object result = Napi::Object::New(env); | ||
| result.Set( | ||
|
|
@@ -387,16 +400,6 @@ Napi::Value GetContentFilter(const Napi::CallbackInfo& info) { | |
| } | ||
| result.Set("parameters", parameters); | ||
|
|
||
| // Cleanup | ||
| rcl_ret_t fini_ret = | ||
| rcl_subscription_content_filter_options_fini(subscription, &options); | ||
| if (fini_ret != RCL_RET_OK) { | ||
| std::string error_msg = rcl_get_error_string().str; | ||
| rcl_reset_error(); | ||
| Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); | ||
| return env.Undefined(); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
catchblock, the code mutateserror.messagevia+=. In strict mode this can throw iferrorisn’t anErrorobject (e.g., a rejected promise with a string/primitive), which would mask the original init failure and potentially skip the intended rollback. Consider normalizing to anErrorinstance (or wrapping a new error withcause) before appending details, and ensure the rollback failure is reported without risking a secondary throw.