[cp-patches] [RFC PATCH 3/4] Fix NPE in java/util/Formatter.format() method

2012-10-15 Thread Pekka Enberg
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




Re: [cp-patches] [RFC PATCH 3/4] Fix NPE in java/util/Formatter.format() method

2012-10-15 Thread Andrew Hughes


- Original Message -
 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

The ChangeLogs in all these patches are incomplete.  This one
and the Collections one are particularly bad.  There are two
methods called format so it isn't clear to what this refers.
In Collections, the ChangeLog is just unreadable.
-- 
Andrew :)

Free Java Software Engineer
Red Hat, Inc. (http://www.redhat.com)

PGP Key: 248BDC07 (https://keys.indymedia.org/)
Fingerprint = EC5A 1F5E C0AD 1D15 8F1F  8F91 3B96 A578 248B DC07