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

Handle null values in MetaData.mergeMaps, preventing potential NPE #386

Merged
merged 4 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 4.9.3 (2018-11-29)

### Bug fixes

* Handle null values in MetaData.mergeMaps, preventing potential NPE
[#386](https://github.com/bugsnag/bugsnag-android/pull/386)


## 4.9.2 (2018-11-07)

### Bug fixes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.bugsnag.android.mazerunner.scenarios

import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import java.util.HashMap

/**
* Sends a handled exception to Bugsnag, which includes a nested null value in a metadata map
*/
internal class MetaDataNestedNullScenario(
config: Configuration,
context: Context
) : Scenario(config, context) {

init {
config.setAutoCaptureSessions(false)

}

override fun run() {
super.run()

val configMap = HashMap<String, Any?>()
configMap["test"] = null
Bugsnag.addToTab("Custom", "foo", configMap)

Bugsnag.notify(RuntimeException("MetaDataScenario")) {
val map = HashMap<String, Any?>()
map["test"] = null
it.error?.addToTab("Custom", "foo", map)
}
}

}
5 changes: 5 additions & 0 deletions features/meta_data_scenario.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ Scenario: Sends a handled exception which includes custom metadata added in a no
Then I should receive a request
And the request is a valid for the error reporting API
And the event "metaData.Custom.foo" equals "Hello World!"

Scenario: Add nested null value to metadata tab
When I run "MetaDataNestedNullScenario"
Then I should receive a request
And the request is a valid for the error reporting API
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=4.9.2
VERSION_NAME=4.9.3
GROUP=com.bugsnag
POM_SCM_URL=https://github.com/bugsnag/bugsnag-android
POM_SCM_CONNECTION=scm:[email protected]:bugsnag/bugsnag-android.git
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.bugsnag.android;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class MetaDataMergeTest {

@Test
public void testConcurrentMapMerge() {
MetaData.merge(generateMetaData(), generateMetaData());
}

/**
* Generates a metadata object with a tab value containing a map with a null entry
*/
private MetaData generateMetaData() {
MetaData metaData = new MetaData();
Map<Object, Object> nestedMap = new HashMap<>();
metaData.addToTab("foo", "bar", nestedMap);
nestedMap.put("whoops", null);
return metaData;
}
}
9 changes: 4 additions & 5 deletions sdk/src/main/java/com/bugsnag/android/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ private static Map<String, Object> mergeMaps(@NonNull Map<String, Object>... map
Object overridesValue = map.get(key);

if (overridesValue != null) {
if (baseValue != null
&& baseValue instanceof Map
&& overridesValue instanceof Map) {
if (baseValue instanceof Map && overridesValue instanceof Map) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

baseValue != null is redundant when also using instanceof

// Both original and overrides are Maps, go deeper
@SuppressWarnings("unchecked")
Map<String, Object> first = (Map<String, Object>) baseValue;
Expand All @@ -159,8 +157,9 @@ private static Map<String, Object> mergeMaps(@NonNull Map<String, Object>... map
result.put(key, overridesValue);
}
} else {
// No collision, just use base value
result.put(key, baseValue);
if (baseValue != null) { // No collision, just use base value
result.put(key, baseValue);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/com/bugsnag/android/Notifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class Notifier implements JsonStream.Streamable {

private static final String NOTIFIER_NAME = "Android Bugsnag Notifier";
private static final String NOTIFIER_VERSION = "4.9.2";
private static final String NOTIFIER_VERSION = "4.9.3";
private static final String NOTIFIER_URL = "https://bugsnag.com";

@NonNull
Expand Down