Skip to content

Commit 322cc1d

Browse files
committed
Fix potential NPEs in DefaultComponentBuilder in addAttribute methods (apache#2791)
1 parent d8cbe77 commit 322cc1d

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/api/ComponentBuilder.java

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public interface ComponentBuilder<T extends ComponentBuilder<T>> extends Builder
2929

3030
/**
3131
* Adds a String attribute.
32+
* <p>
33+
* If the given {@code level} value is {@code null}, the component attribute with the given key
34+
* will be removed (if present).
35+
* </p>
3236
* @param key The attribute key.
3337
* @param value The value of the attribute.
3438
* @return This ComponentBuilder.
@@ -37,6 +41,10 @@ public interface ComponentBuilder<T extends ComponentBuilder<T>> extends Builder
3741

3842
/**
3943
* Adds a logging Level attribute.
44+
* <p>
45+
* If the given {@code level} value is {@code null}, the component attribute with the given key
46+
* will be removed (if present).
47+
* </p>
4048
* @param key The attribute key.
4149
* @param level The logging Level.
4250
* @return This ComponentBuilder.
@@ -45,6 +53,10 @@ public interface ComponentBuilder<T extends ComponentBuilder<T>> extends Builder
4553

4654
/**
4755
* Adds an enumeration attribute.
56+
* <p>
57+
* If the given {@code level} value is {@code null}, the component attribute with the given key
58+
* will be removed (if present).
59+
* </p>
4860
* @param key The attribute key.
4961
* @param value The enumeration.
5062
* @return This ComponentBuilder.
@@ -69,6 +81,10 @@ public interface ComponentBuilder<T extends ComponentBuilder<T>> extends Builder
6981

7082
/**
7183
* Adds an Object attribute.
84+
* <p>
85+
* If the given {@code value} is {@code null}, the component attribute with the given key
86+
* will be removed (if present).
87+
* </p>
7288
* @param key The attribute key.
7389
* @param value The object value.
7490
* @return This ComponentBuilder.

log4j-core/src/main/java/org/apache/logging/log4j/core/config/builder/impl/DefaultComponentBuilder.java

+32-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.LinkedHashMap;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Optional;
24+
2325
import org.apache.logging.log4j.Level;
2426
import org.apache.logging.log4j.core.config.Configuration;
2527
import org.apache.logging.log4j.core.config.builder.api.Component;
@@ -57,43 +59,51 @@ public DefaultComponentBuilder(final CB builder, final String name, final String
5759
this.value = value;
5860
}
5961

62+
/** {@inheritDoc} */
6063
@Override
6164
public T addAttribute(final String key, final boolean value) {
6265
return put(key, Boolean.toString(value));
6366
}
6467

68+
/** {@inheritDoc} */
6569
@Override
6670
public T addAttribute(final String key, final Enum<?> value) {
67-
return put(key, value.name());
71+
return put(key, Optional.ofNullable(value).map(Enum::name).orElse(null));
6872
}
6973

74+
/** {@inheritDoc} */
7075
@Override
7176
public T addAttribute(final String key, final int value) {
7277
return put(key, Integer.toString(value));
7378
}
7479

80+
/** {@inheritDoc} */
7581
@Override
7682
public T addAttribute(final String key, final Level level) {
77-
return put(key, level.toString());
83+
return put(key, Optional.ofNullable(level).map(Level::toString).orElse(null));
7884
}
7985

86+
/** {@inheritDoc} */
8087
@Override
8188
public T addAttribute(final String key, final Object value) {
82-
return put(key, value.toString());
89+
return put(key, Optional.ofNullable(value).map(Object::toString).orElse(null));
8390
}
8491

92+
/** {@inheritDoc} */
8593
@Override
8694
public T addAttribute(final String key, final String value) {
8795
return put(key, value);
8896
}
8997

98+
/** {@inheritDoc} */
9099
@Override
91100
@SuppressWarnings("unchecked")
92101
public T addComponent(final ComponentBuilder<?> builder) {
93102
components.add(builder.build());
94103
return (T) this;
95104
}
96105

106+
/** {@inheritDoc} */
97107
@Override
98108
public Component build() {
99109
final Component component = new Component(type, name, value);
@@ -102,19 +112,37 @@ public Component build() {
102112
return component;
103113
}
104114

115+
/** {@inheritDoc} */
105116
@Override
106117
public CB getBuilder() {
107118
return builder;
108119
}
109120

121+
/** {@inheritDoc} */
110122
@Override
111123
public String getName() {
112124
return name;
113125
}
114126

127+
/**
128+
* Puts the given key/value pair to the attribute map.
129+
* <p>
130+
* If the value is {@code null} the component attribute with the given {@code key} will
131+
* instead be removed from the map.
132+
* </p>
133+
* @param key the key
134+
* @param value the value
135+
* @return this builder (for chaining)
136+
*/
115137
@SuppressWarnings("unchecked")
116138
protected T put(final String key, final String value) {
117-
attributes.put(key, value);
139+
140+
if (value != null) {
141+
attributes.put(key, value);
142+
} else {
143+
attributes.remove(key);
144+
}
145+
118146
return (T) this;
119147
}
120148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="2791" link="https://github.com/apache/logging-log4j2/issues/2791"/>
7+
<description format="asciidoc">
8+
Fix problem when null attribute values are set on DefaultComponentBuilder.
9+
GitHub issue #2791.
10+
</description>
11+
</entry>

0 commit comments

Comments
 (0)