Skip to content
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

Fix Segmentation Fault in UnauthenticatedTenRPCCall in audit #2243

Merged
merged 2 commits into from
Jan 13, 2025

Conversation

zkokelj
Copy link
Contributor

@zkokelj zkokelj commented Jan 8, 2025

Why this change is needed

A segmentation fault was occurring during the audit function call while formatting RPC call results (res) and errors (err).

More details: https://github.com/ten-protocol/ten-internal/issues/4479

  • replaced %s with %+v for res and err in audit logs to handle complex or nil values gracefully.
  • changed var resp *R to var resp R to avoid unnecessary pointer dereferences
  • added separate audit calls for success and error scenarios to prevent undefined behavior

What changes were made as part of this PR

Please provide a high level list of the changes made

PR checks pre-merging

Please indicate below by ticking the checkbox that you have read and performed the required
PR checks

  • PR checks reviewed and performed

Copy link
Collaborator

@tudor-malene tudor-malene left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, but I have a question

@@ -64,18 +64,24 @@ func UnauthenticatedTenRPCCall[R any](ctx context.Context, w *services.Services,

res, err := cache.WithCache(w.RPCResponsesCache, cfg, generateCacheKey(cacheArgs), func() (*R, error) {
return services.WithPlainRPCConnection(ctx, w.BackendRPC, func(client *rpc.Client) (*R, error) {
var resp *R
var resp R
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the reason why it had to be a pointer.
I'm wondering if we were to make this back a pointer and leave the if at the end, would it still seg-fault?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't remember why *R was used here.

After some research I think we can keep *R and do explicit allocation var resp *R = new(R) as it protects us from nil pointer dereference.
What do you think? Should I switch back to *R?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@zkokelj zkokelj requested a review from tudor-malene January 9, 2025 10:30
Copy link
Collaborator

@tudor-malene tudor-malene left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@zkokelj zkokelj merged commit f383afa into main Jan 13, 2025
2 checks passed
@zkokelj zkokelj deleted the ziga/gateway_fix_ten_gasPrice_error_handling branch January 13, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants