Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit c54332c

Browse files
committed
Merge pull request #146 from mortezag/fixStringConcat
Fix NullReferenceException in String.Concat(params object[] args)
2 parents 56ca26b + 6c90af3 commit c54332c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Framework/Subset_of_CorLib/System/String.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,10 @@ public static String Concat(params Object[] args)
228228

229229
int length = args.Length;
230230
String[] sArgs = new String[length];
231-
int totalLength = 0;
232231

233232
for (int i = 0; i < length; i++)
234233
{
235234
sArgs[i] = ((args[i] == null) ? (String.Empty) : (args[i].ToString()));
236-
totalLength += sArgs[i].Length;
237235
}
238236

239237
return String.Concat(sArgs);

Test/Platform/Tests/CLR/mscorlib/systemlib/systemlib2/SystemStringTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,35 @@ public MFTestResults Length_Test71()
436436
testResult &= (str1.Length == 9);
437437
return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
438438
}
439-
439+
440+
[TestMethod]
441+
public MFTestResults Concat_Test1()
442+
{
443+
/// <summary>
444+
/// 1. Tests the string concat of several object where one of the arguments returns a null value for ToString()
445+
/// </summary>
446+
///
447+
448+
try
449+
{
450+
string str = "a" + 1 + "b" + new ToStringReturnsNull();
451+
return (str == "a1b" ? MFTestResults.Pass : MFTestResults.Fail);
452+
}
453+
catch
454+
{
455+
return MFTestResults.Fail;
456+
}
457+
}
458+
}
459+
460+
/// <summary>
461+
/// A class whose ToString method return null
462+
/// </summary>
463+
public class ToStringReturnsNull
464+
{
465+
public override string ToString()
466+
{
467+
return null;
468+
}
440469
}
441470
}

0 commit comments

Comments
 (0)