summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPekka Enberg <penberg@kernel.org>2012-03-15 21:25:24 +0200
committerPekka Enberg <penberg@kernel.org>2012-10-15 13:22:19 +0300
commit3906c7137b7b048ca990af825526e8ba4610235d (patch)
tree4af234ffd20793bc6c9fce76b6d47e87eae6bd4d
parenta1e2fbeff01860fc2c7ad5c8a0a6982e5ca14b95 (diff)
downloadclasspath-3906c7137b7b048ca990af825526e8ba4610235d.tar.gz
Fix NPE in java/util/Formatter.format() method
This patch fixes NPE for the following Malva test cases: assertEquals("false", String.format("%b", (Object[])null)); assertEquals("null", String.format("%h", (Object[])null)); assertEquals("null", String.format("%s", (Object[])null)); assertEquals("null", String.format("%c", (Object[])null)); assertEquals("null", String.format("%d", (Object[])null)); assertEquals("null", String.format("%o", (Object[])null)); assertEquals("null", String.format("%x", (Object[])null)); Signed-off-by: Pekka Enberg <penberg@kernel.org>
-rw-r--r--ChangeLog10
-rw-r--r--java/util/Formatter.java21
2 files changed, 28 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 384918be8..746a1f7af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2012-03-15 Pekka Enberg <penberg@kernel.org>
+ * java/util/Formatter.java:
+ (icharacterFormat(Object,int,int,int,char):
+ Fix NullPointerException for null characters.
+ (basicIntegralConversion(Object, int, int, int, int, char):
+ Fix NullPointerException for null integers.
+ (format(Locale, String, Object...)):
+ Fix NullPointerException for null object.
+
+2012-03-15 Pekka Enberg <penberg@kernel.org>
+
* java/lang/String.java:
(codePointAt(int))): Fix exception type.
(codePointBefore(int)): Fix exception type.
diff --git a/java/util/Formatter.java b/java/util/Formatter.java
index 62f684562..466fab535 100644
--- a/java/util/Formatter.java
+++ b/java/util/Formatter.java
@@ -678,6 +678,12 @@ public final class Formatter
conversion);
noPrecision(precision);
+ if (arg == null)
+ {
+ genericFormat("null", flags, width, precision);
+ return;
+ }
+
int theChar;
if (arg instanceof Character)
theChar = ((Character) arg).charValue();
@@ -748,6 +754,12 @@ public final class Formatter
int radix, char conversion)
{
assert radix == 8 || radix == 10 || radix == 16;
+
+ if (arg == null)
+ {
+ return new CPStringBuilder("null");
+ }
+
noPrecision(precision);
// Some error checking.
@@ -1353,9 +1365,12 @@ public final class Formatter
argumentIndex = previousArgumentIndex;
// Argument indices start at 1 but array indices at 0.
--argumentIndex;
- if (argumentIndex < 0 || argumentIndex >= args.length)
- throw new MissingFormatArgumentException(format.substring(start, index));
- argument = args[argumentIndex];
+ if (args != null)
+ {
+ if (argumentIndex < 0 || argumentIndex >= args.length)
+ throw new MissingFormatArgumentException(format.substring(start, index));
+ argument = args[argumentIndex];
+ }
}
switch (conversion)