Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Safar <marek.safar@gmail.com>2012-08-09 13:51:03 +0400
committerMarek Safar <marek.safar@gmail.com>2012-08-09 13:51:03 +0400
commitf74872ab7f90ccb38f538eec418d4325dcc0e3ab (patch)
treee42354e9ed22358c46d06e8854000baf4860d617
parentaa8ffef3822d5557b99aa874bfb9481e8d2e0a3b (diff)
Don't throw NRE when custom formatter returns null
-rw-r--r--mcs/class/corlib/System/String.cs22
-rw-r--r--mcs/class/corlib/Test/System/StringTest.cs21
2 files changed, 33 insertions, 10 deletions
diff --git a/mcs/class/corlib/System/String.cs b/mcs/class/corlib/System/String.cs
index 1ab450e1c47..9b345f17d72 100644
--- a/mcs/class/corlib/System/String.cs
+++ b/mcs/class/corlib/System/String.cs
@@ -1953,6 +1953,8 @@ namespace System
int ptr = 0;
int start = ptr;
+ var formatter = provider != null ? provider.GetFormat (typeof (ICustomFormatter)) as ICustomFormatter : null;
+
while (ptr < format.length) {
char c = format[ptr ++];
@@ -1981,21 +1983,21 @@ namespace System
object arg = args[n];
string str;
- ICustomFormatter formatter = null;
- if (provider != null)
- formatter = provider.GetFormat (typeof (ICustomFormatter))
- as ICustomFormatter;
if (arg == null)
str = Empty;
else if (formatter != null)
str = formatter.Format (arg_format, arg, provider);
- else if (arg is IFormattable)
- str = ((IFormattable)arg).ToString (arg_format, provider);
else
- str = arg.ToString ();
+ str = null;
- // pad formatted string and append to result
+ if (str == null) {
+ if (arg is IFormattable)
+ str = ((IFormattable)arg).ToString (arg_format, provider);
+ else
+ str = arg.ToString ();
+ }
+ // pad formatted string and append to result
if (width > str.length) {
const char padchar = ' ';
int padlen = width - str.length;
@@ -2008,9 +2010,9 @@ namespace System
result.Append (padchar, padlen);
result.Append (str);
}
- }
- else
+ } else {
result.Append (str);
+ }
start = ptr;
}
diff --git a/mcs/class/corlib/Test/System/StringTest.cs b/mcs/class/corlib/Test/System/StringTest.cs
index 429ff654f0a..ef92166c281 100644
--- a/mcs/class/corlib/Test/System/StringTest.cs
+++ b/mcs/class/corlib/Test/System/StringTest.cs
@@ -24,6 +24,20 @@ namespace MonoTests.System
[TestFixture]
public class StringTest
{
+ class NullFormatter : IFormatProvider, ICustomFormatter
+ {
+ public string Format (string format, object arg, IFormatProvider provider)
+ {
+ return null;
+ }
+
+ public object GetFormat (Type formatType)
+ {
+ return this;
+ }
+ }
+
+
private CultureInfo orgCulture;
[SetUp]
@@ -1162,6 +1176,13 @@ public class StringTest
}
[Test]
+ public void Format ()
+ {
+ var s = String.Format (new NullFormatter (), "{0:}", "test");
+ Assert.AreEqual ("test", s);
+ }
+
+ [Test]
public void TestGetEnumerator ()
{
string s1 = "original";