Skip to content

Commit

Permalink
Merge a7be328 into 59720bf
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Nov 26, 2018
2 parents 59720bf + a7be328 commit 2851d4f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 4.X.X (TBD)

### 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bugsnag.android;

import static org.junit.Assert.assertFalse;

import org.junit.Test;

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

public class MetaDataMergeTest {

@Test
public void testConcurrentMapMerge() {
MetaData metaData = new MetaData();

Map<Object, Object> nestedMap = new HashMap<>();
metaData.addToTab("foo", "bar", nestedMap);
nestedMap.put("whoops", null);

Map<String, Object> mergedMap = MetaData.merge(metaData, metaData).store;
assertFalse(mergedMap.containsKey("whoops"));
}
}
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) {
// 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

0 comments on commit 2851d4f

Please sign in to comment.