# HG changeset patch # User bpb # Date 1429046749 25200 # Tue Apr 14 14:25:49 2015 -0700 # Node ID 5e1e09318383e71d171323349d214a4450ef96fe # Parent 5c30932415d8518b53fc9461eedd51c4db1d242e 8075156: (prefs) remove() should disallow the use of the null control character '\u0000' as key Summary: Extend disallowing null control character key to remove() Reviewed-by: XXX diff --git a/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java b/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java --- a/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java +++ b/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -77,10 +77,9 @@ * under which these calls cannot even enqueue the requested operation for * later processing. Even under these circumstances it is generally better to * simply ignore the invocation and return, rather than throwing an - * exception. Under these circumstances, however, all subsequent invocations - * of flush() and sync should return false, as - * returning true would imply that all previous operations had - * successfully been made permanent. + * exception. Under these circumstances, however, subsequently invoking + * flush() or sync would not imply that all previous + * operations had successfully been made permanent. * *
There is one circumstance under which putSpi, removeSpi and
* childSpi should throw an exception: if the caller lacks
@@ -122,6 +121,13 @@
*/
public abstract class AbstractPreferences extends Preferences {
/**
+ * The code point U+0000, assigned to the null control character, is the
+ * only character encoded in Unicode and ISO/IEC 10646 that is always
+ * invalid in any XML 1.0 and 1.1 document.
+ */
+ static final String CODE_POINT_U0000 = String.valueOf('\u0000');
+
+ /**
* Our name relative to parent.
*/
private final String name;
@@ -233,6 +239,8 @@
* @throws IllegalArgumentException if key.length() exceeds
* MAX_KEY_LENGTH or if value.length exceeds
* MAX_VALUE_LENGTH.
+ * @throws IllegalArgumentException if either key or value contain
+ * the null control character, code point U+0000.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
*/
@@ -243,6 +251,10 @@
throw new IllegalArgumentException("Key too long: "+key);
if (value.length() > MAX_VALUE_LENGTH)
throw new IllegalArgumentException("Value too long: "+value);
+ if (key.indexOf(CODE_POINT_U0000) != -1)
+ throw new IllegalArgumentException("Key contains code point U+0000");
+ if (value.indexOf(CODE_POINT_U0000) != -1)
+ throw new IllegalArgumentException("Value contains code point U+0000");
synchronized(lock) {
if (removed)
@@ -274,10 +286,14 @@
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null. (A
* null default is permitted.)
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public String get(String key, String def) {
if (key==null)
throw new NullPointerException("Null key");
+ if (key.indexOf(CODE_POINT_U0000) != -1)
+ throw new IllegalArgumentException("Key contains code point U+0000");
synchronized(lock) {
if (removed)
throw new IllegalStateException("Node has been removed.");
@@ -305,10 +321,14 @@
* @param key key whose mapping is to be removed from the preference node.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
* @throws NullPointerException {@inheritDoc}.
*/
public void remove(String key) {
Objects.requireNonNull(key, "Specified key cannot be null");
+ if (key.indexOf(CODE_POINT_U0000) != -1)
+ throw new IllegalArgumentException("Key contains code point U+0000");
synchronized(lock) {
if (removed)
throw new IllegalStateException("Node has been removed.");
@@ -352,6 +372,8 @@
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key.length() exceeds
* MAX_KEY_LENGTH.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
*/
@@ -380,6 +402,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public int getInt(String key, int def) {
int result = def;
@@ -407,6 +431,8 @@
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key.length() exceeds
* MAX_KEY_LENGTH.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
*/
@@ -435,6 +461,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public long getLong(String key, long def) {
long result = def;
@@ -462,6 +490,8 @@
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key.length() exceeds
* MAX_KEY_LENGTH.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
*/
@@ -493,6 +523,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public boolean getBoolean(String key, boolean def) {
boolean result = def;
@@ -520,6 +552,8 @@
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key.length() exceeds
* MAX_KEY_LENGTH.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
*/
@@ -548,6 +582,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public float getFloat(String key, float def) {
float result = def;
@@ -575,6 +611,8 @@
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key.length() exceeds
* MAX_KEY_LENGTH.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
*/
@@ -603,6 +641,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public double getDouble(String key, double def) {
double result = def;
@@ -626,6 +666,8 @@
* @throws NullPointerException if key or value is null.
* @throws IllegalArgumentException if key.length() exceeds MAX_KEY_LENGTH
* or if value.length exceeds MAX_VALUE_LENGTH*3/4.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
*/
@@ -649,6 +691,8 @@
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null. (A
* null value for def is permitted.)
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public byte[] getByteArray(String key, byte[] def) {
byte[] result = def;
diff --git a/src/java.prefs/share/classes/java/util/prefs/Preferences.java b/src/java.prefs/share/classes/java/util/prefs/Preferences.java
--- a/src/java.prefs/share/classes/java/util/prefs/Preferences.java
+++ b/src/java.prefs/share/classes/java/util/prefs/Preferences.java
@@ -489,7 +489,7 @@
* MAX_VALUE_LENGTH.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
- * @throws IllegalArgumentException if either the key or the value contain
+ * @throws IllegalArgumentException if either key or value contain
* the null control character, code point U+0000.
*/
public abstract void put(String key, String value);
@@ -514,6 +514,8 @@
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null. (A
* null value for def is permitted.)
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public abstract String get(String key, String def);
@@ -530,6 +532,8 @@
* @throws NullPointerException if key is null.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
*/
public abstract void remove(String key);
@@ -566,6 +570,8 @@
* MAX_KEY_LENGTH.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @see #getInt(String,int)
*/
public abstract void putInt(String key, int value);
@@ -597,6 +603,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
* @see #putInt(String,int)
* @see #get(String,String)
*/
@@ -616,6 +624,8 @@
* MAX_KEY_LENGTH.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @see #getLong(String,long)
*/
public abstract void putLong(String key, long value);
@@ -647,6 +657,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
* @see #putLong(String,long)
* @see #get(String,String)
*/
@@ -666,6 +678,8 @@
* MAX_KEY_LENGTH.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @see #getBoolean(String,boolean)
* @see #get(String,String)
*/
@@ -702,6 +716,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
* @see #get(String,String)
* @see #putBoolean(String,boolean)
*/
@@ -721,6 +737,8 @@
* MAX_KEY_LENGTH.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @see #getFloat(String,float)
*/
public abstract void putFloat(String key, float value);
@@ -751,6 +769,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
* @see #putFloat(String,float)
* @see #get(String,String)
*/
@@ -770,6 +790,8 @@
* MAX_KEY_LENGTH.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @see #getDouble(String,double)
*/
public abstract void putDouble(String key, double value);
@@ -800,6 +822,8 @@
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
* @see #putDouble(String,double)
* @see #get(String,String)
*/
@@ -825,6 +849,8 @@
* or if value.length exceeds MAX_VALUE_LENGTH*3/4.
* @throws IllegalStateException if this node (or an ancestor) has been
* removed with the {@link #removeNode()} method.
+ * @throws IllegalArgumentException if key contains
+ * the null control character, code point U+0000.
* @see #getByteArray(String,byte[])
* @see #get(String,String)
*/
@@ -864,6 +890,8 @@
* removed with the {@link #removeNode()} method.
* @throws NullPointerException if key is null. (A
* null value for def is permitted.)
+ * @throws IllegalArgumentException if key contains the null control
+ * character, code point U+0000.
* @see #get(String,String)
* @see #putByteArray(String,byte[])
*/
diff --git a/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java b/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java
--- a/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java
+++ b/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -49,13 +49,6 @@
*/
class FileSystemPreferences extends AbstractPreferences {
- /**
- * The code point U+0000, assigned to the null control character, is the
- * only character encoded in Unicode and ISO/IEC 10646 that is always
- * invalid in any XML 1.0 and 1.1 document.
- */
- private static final String CODE_POINT_U0000 = String.valueOf('\u0000');
-
static {
PrivilegedAction