diff options
author | Steffen Kieß <kiess@ki4.de> | 2020-01-27 19:00:16 +0300 |
---|---|---|
committer | Alexander Köplinger <alex.koeplinger@outlook.com> | 2020-01-27 19:00:16 +0300 |
commit | 1fd0748362e9014469fc7e6390826c7a5fdab027 (patch) | |
tree | 9d67adab6dc23f6ccb6dac38e41a1eedca5d019d /mcs/class | |
parent | 66ff74a1f37a327da3148990b292c8e1c6ec6f69 (diff) |
Fix UnixEncoding.GetBytes() for empty strings (#18520)
Current versions of the mono return a null pointer as address of the first
element for empty arrays, i.e.
fixed (byte* ptr = new byte[0]) Console.WriteLine (ptr == null);
will print "true". This causes a problem in the UnixEncoding class where it
triggers a check for null pointers. Fix this by allowing null pointers when
the number of elements is zero.
Diffstat (limited to 'mcs/class')
-rw-r--r-- | mcs/class/Mono.Posix/Mono.Unix/UnixEncoding.cs | 4 | ||||
-rw-r--r-- | mcs/class/Mono.Posix/Test/Mono.Unix/UnixEncodingTest.cs | 17 |
2 files changed, 19 insertions, 2 deletions
diff --git a/mcs/class/Mono.Posix/Mono.Unix/UnixEncoding.cs b/mcs/class/Mono.Posix/Mono.Unix/UnixEncoding.cs index d869b1b90c6..26529629fe0 100644 --- a/mcs/class/Mono.Posix/Mono.Unix/UnixEncoding.cs +++ b/mcs/class/Mono.Posix/Mono.Unix/UnixEncoding.cs @@ -311,8 +311,8 @@ public class UnixEncoding : Encoding public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount) { - if (bytes == null || chars == null) - throw new ArgumentNullException (bytes == null ? "bytes" : "chars"); + if ((bytes == null && byteCount != 0) || (chars == null && charCount != 0)) + throw new ArgumentNullException ((bytes == null && byteCount != 0) ? "bytes" : "chars"); if (charCount < 0 || byteCount < 0) throw new ArgumentOutOfRangeException (charCount < 0 ? "charCount" : "byteCount"); diff --git a/mcs/class/Mono.Posix/Test/Mono.Unix/UnixEncodingTest.cs b/mcs/class/Mono.Posix/Test/Mono.Unix/UnixEncodingTest.cs index 2f0747294d3..042c8dacf4b 100644 --- a/mcs/class/Mono.Posix/Test/Mono.Unix/UnixEncodingTest.cs +++ b/mcs/class/Mono.Posix/Test/Mono.Unix/UnixEncodingTest.cs @@ -995,6 +995,23 @@ namespace MonoTests.Mono.Unix { ); } + [Test] + public void TestEmptyString () + { + byte[] data = new byte [] {}; + Encoding enc = new UnixEncoding (); + + string s = enc.GetString (data); + Assert.AreEqual (s, "", "#1"); + char[] chars = enc.GetChars (data); + Assert.AreEqual (chars.Length, 0, "#2"); + + byte[] b1 = enc.GetBytes (""); + Assert.AreEqual (b1.Length, 0, "#3"); + byte[] b2 = enc.GetBytes (new char[] {}); + Assert.AreEqual (b2.Length, 0, "#3"); + } + private void Compare (string prefix, string start, byte[] end) { byte[] bytes = unix.GetBytes (start); |