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 potential NPE by separating transaction contexts. #1164

Closed
wants to merge 1 commit into from

Conversation

maciejwalkowiak
Copy link
Contributor

📜 Description

Fix potential NPE by separating transaction contexts from event contexts.

💡 Motivation and Context

Even though event contexts look similar to transaction contexts, there are different nullability constraints - with transactions, its guaranteed that trace cannot be null. With this change, we fix potential NPE reported in #1160.

💚 How did you test it?

Unit tests.

📝 Checklist

  • I reviewed submitted code
  • I added tests to verify changes
  • All tests passing
  • No breaking changes

🔮 Next steps

Perhaps change the Contexts class to also not inherit from the HashMap?

@maciejwalkowiak
Copy link
Contributor Author

@marandaneto there are few tests to add there, but before I put more time into it please take a look if you believe we should go this direction

@codecov-io
Copy link

Codecov Report

Merging #1164 (10e051c) into main (a385352) will decrease coverage by 0.42%.
The diff coverage is 55.35%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1164      +/-   ##
============================================
- Coverage     75.25%   74.83%   -0.43%     
- Complexity     1662     1681      +19     
============================================
  Files           173      176       +3     
  Lines          5812     5912     +100     
  Branches        568      583      +15     
============================================
+ Hits           4374     4424      +50     
- Misses         1174     1216      +42     
- Partials        264      272       +8     
Impacted Files Coverage Δ Complexity Δ
...entry/src/main/java/io/sentry/NoOpTransaction.java 29.16% <0.00%> (ø) 7.00 <0.00> (ø)
...entry/src/main/java/io/sentry/SentryBaseEvent.java 91.66% <ø> (-0.44%) 20.00 <0.00> (-1.00)
...apters/TransactionContextsDeserializerAdapter.java 33.33% <33.33%> (ø) 4.00 <4.00> (?)
...y/src/main/java/io/sentry/TransactionContexts.java 61.53% <61.53%> (ø) 12.00 <12.00> (?)
...adapters/TransactionContextsSerializerAdapter.java 73.91% <73.91%> (ø) 3.00 <3.00> (?)
...try/src/main/java/io/sentry/SentryTransaction.java 98.24% <90.00%> (+0.03%) 27.00 <8.00> (+1.00)
sentry/src/main/java/io/sentry/GsonSerializer.java 97.05% <100.00%> (+0.08%) 9.00 <0.00> (ø)
sentry/src/main/java/io/sentry/SentryEvent.java 69.38% <100.00%> (+0.63%) 44.00 <2.00> (+1.00)
...o/sentry/adapters/ContextsDeserializerAdapter.java 34.04% <0.00%> (-4.26%) 5.00% <0.00%> (-1.00%)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a385352...10e051c. Read the comment docs.

@@ -97,6 +97,9 @@
/** List of breadcrumbs recorded before this event. */
private List<Breadcrumb> breadcrumbs;

/** Contexts describing the environment (e.g. device, os or browser). */
private final @NotNull Contexts contexts = new Contexts();
Copy link
Contributor

Choose a reason for hiding this comment

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

in theory, this is a Nullable field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been changed to not-nullable in previous PR.

JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
if (json != null && !json.isJsonNull()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we extract the content of this method to a Util class and reuse it in ContextsDeserializerAdapter and ContextsSerializerAdapter? they look pretty much the same.

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'll see what i can do - they don't share the same class hierarchy, at least not at the moment.

}

final JsonObject object = new JsonObject();
object.add(SpanContext.TYPE, context.serialize(src.getTrace(), Object.class));
Copy link
Contributor

Choose a reason for hiding this comment

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

ContextsDeserializerAdapter uses SpanContext.TYPE and apparently after this PR, it'd not be necessary anymore, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be, as SpanContext (aka "trace") is also a part of events.

@maciejwalkowiak
Copy link
Contributor Author

@marandaneto if we go this path, perhaps it makes sense to change also Contexts class to NOT inherit from hashmap.

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class TransactionContexts {
Copy link
Contributor

Choose a reason for hiding this comment

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

while I agree that this would avoid all sort of NPE aroundContexts and trace, looks like a junk of code duplication, wondering if just moving the Contexts inheritance away and adding an other field in there would not solve the problem for good (not adding a new class TransactionContexts).
Also, Contexts would need to become @NotNull by default on SentryBaseEvent.

maybe @bruno-garcia would like to chime in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then we would eliminate in practical terms the possibility of NPE but we won't get away with warnings in the IDE.

@maciejwalkowiak
Copy link
Contributor Author

Not valid anymore.

@marandaneto marandaneto deleted the gh-1160 branch January 29, 2021 12:50
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.

3 participants