Don't allow empty context keys/values (#688)

This commit is contained in:
Luck 2018-01-15 20:06:20 +00:00
parent 8a374aed04
commit ed223f0e4e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 29 additions and 5 deletions

View File

@ -122,11 +122,33 @@ abstract class AbstractContextSet implements ContextSet {
} }
static String sanitizeKey(String key) { static String sanitizeKey(String key) {
return Objects.requireNonNull(key, "key is null").toLowerCase().intern(); Objects.requireNonNull(key, "key is null");
if (stringIsEmpty(key)) {
throw new IllegalArgumentException("key is (effectively) empty");
}
return key.toLowerCase().intern();
} }
static String sanitizeValue(String value) { static String sanitizeValue(String value) {
return Objects.requireNonNull(value, "value is null").intern(); Objects.requireNonNull(value, "value is null");
if (stringIsEmpty(value)) {
throw new IllegalArgumentException("value is (effectively) empty");
}
return value.intern();
}
private static boolean stringIsEmpty(String s) {
if (s.isEmpty()) {
return true;
}
for (char c : s.toCharArray()) {
if (c != ' ') {
return false;
}
}
return true;
} }
} }

View File

@ -51,7 +51,9 @@ import javax.annotation.Nonnull;
* {@link String#toLowerCase() lowercase} by all implementations. * {@link String#toLowerCase() lowercase} by all implementations.
* Values however are case-sensitive.</p> * Values however are case-sensitive.</p>
* *
* <p>Context keys and values may not be null.</p> * <p>Context keys and values may not be null or empty. A key/value will be
* deemed empty if it's length is zero, or if it consists of only space
* characters.</p>
* *
* <p>Two default ContextSet implementations are provided. * <p>Two default ContextSet implementations are provided.
* {@link MutableContextSet} allows the addition and removal of context keys * {@link MutableContextSet} allows the addition and removal of context keys

View File

@ -134,12 +134,12 @@ public class ArgumentUtils {
} }
String key = pair.substring(0, index); String key = pair.substring(0, index);
if (key.equals("")) { if (key.equals("") || key.trim().isEmpty()) {
continue; continue;
} }
String value = pair.substring(index + 1); String value = pair.substring(index + 1);
if (value.equals("")) { if (value.equals("") || value.trim().isEmpty()) {
continue; continue;
} }