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 <penb...@kernel.org>
---
 ChangeLog                |    5 +++++
 java/util/Formatter.java |   21 ++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5690754..5a75061 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2012-03-15  Pekka Enberg  <penb...@kernel.org>
+
+       * java/util/Formatter.java:
+       (format): Fix NPE errors.
+
 2011-07-20  Ivan Maidanski  <iv...@mail.ru>
 
        * java/util/Collections.java:
diff --git a/java/util/Formatter.java b/java/util/Formatter.java
index 62f6845..466fab5 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)
-- 
1.7.7.6


Reply via email to