(commons-configuration) branch master updated: Guard MapConfiguration against null maps #381

2024-04-23 Thread ggregory
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git


The following commit(s) were added to refs/heads/master by this push:
 new 068568bd Guard MapConfiguration against null maps #381
068568bd is described below

commit 068568bd7f4d64c589bf34b45ef76c3ab004d6c9
Author: Gary Gregory 
AuthorDate: Tue Apr 23 18:06:50 2024 -0400

Guard MapConfiguration against null maps #381
---
 src/changes/changes.xml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6cedf714..0df32b54 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,7 @@
   Fail-fast with a 
NullPointerException if 
XMLPropertiesConfiguration.XMLPropertiesConfiguration(Element) is called with 
null.
   Fail-fast with a 
NullPointerException if a SubsetConfiguration constructor is called with a null 
Configuration.
   Methods should not be empty #393.
+  Guard MapConfiguration against null maps #381. 
   
   Bump 
commons-logging:commons-logging from 1.3.0 to 1.3.1 #390.
   Bump 
commons-io:commons-io from 2.15.1 to 2.16.1 #394, #400.



(commons-configuration) branch master updated: Guard MapConfiguration against null maps (#381)

2024-04-23 Thread ggregory
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git


The following commit(s) were added to refs/heads/master by this push:
 new 3453e987 Guard MapConfiguration against null maps (#381)
3453e987 is described below

commit 3453e9879de212af13b4317305969fc3ad5aa4d4
Author: Heewon Lee <94441510+pingpin...@users.noreply.github.com>
AuthorDate: Wed Apr 24 07:05:45 2024 +0900

Guard MapConfiguration against null maps (#381)

MapConfiguration has a `protected` property `map`, which is provided
upon instantiation and cannot be changed later. The lack of `null`
guards for this property leads to inevitable NPEs at an undetermined
later time. This PR implements a fail-fast guard.
---
 .../java/org/apache/commons/configuration2/MapConfiguration.java  | 5 +++--
 .../org/apache/commons/configuration2/TestMapConfiguration.java   | 8 
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/configuration2/MapConfiguration.java 
b/src/main/java/org/apache/commons/configuration2/MapConfiguration.java
index 4319bdf7..aff3b204 100644
--- a/src/main/java/org/apache/commons/configuration2/MapConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/MapConfiguration.java
@@ -22,6 +22,7 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Properties;
 
 import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
@@ -87,7 +88,7 @@ public class MapConfiguration extends AbstractConfiguration 
implements Cloneable
  * @param map the map
  */
 public MapConfiguration(final Map map) {
-this.map = (Map) map;
+this.map = (Map) Objects.requireNonNull(map, "map");
 }
 
 /**
@@ -100,7 +101,7 @@ public class MapConfiguration extends AbstractConfiguration 
implements Cloneable
  * @since 1.8
  */
 public MapConfiguration(final Properties props) {
-map = toMap(props);
+map = toMap(Objects.requireNonNull(props));
 }
 
 /**
diff --git 
a/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java 
b/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java
index c447e654..d0f22d51 100644
--- a/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java
@@ -19,11 +19,13 @@ package org.apache.commons.configuration2;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 
 import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
 import org.apache.commons.configuration2.convert.DisabledListDelimiterHandler;
@@ -154,4 +156,10 @@ public class TestMapConfiguration extends 
TestAbstractConfiguration {
 config.setListDelimiterHandler(new DisabledListDelimiterHandler());
 assertEquals(SPACE_VALUE, config.getProperty(KEY));
 }
+
+@Test
+public void testNullMap() {
+assertThrows(NullPointerException.class, () -> new 
MapConfiguration((Map) null));
+assertThrows(NullPointerException.class, () -> new 
MapConfiguration((Properties) null));
+}
 }