New test.
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixMarshal.cs
index 77069d1f167140dffdeac2173260b6d550bb2e97..9282356acf54dce7a9cb0bf9e2d0549efb51e1df 100644 (file)
@@ -140,6 +140,9 @@ namespace Mono.Unix {
                        if (p == IntPtr.Zero)
                                return null;
 
+                       if (encoding == null)
+                               throw new ArgumentNullException ("encoding");
+
                        int len = GetStringByteLength (p, encoding);
 
                        // Due to variable-length encoding schemes, GetStringByteLength() may
@@ -281,6 +284,8 @@ namespace Mono.Unix {
                {
                        if (count < 0)
                                throw new ArgumentOutOfRangeException ("count", "< 0");
+                       if (encoding == null)
+                               throw new ArgumentNullException ("encoding");
                        if (stringArray == IntPtr.Zero)
                                return new string[count];
 
@@ -310,6 +315,12 @@ namespace Mono.Unix {
 
                public static IntPtr StringToHeap (string s, int index, int count, Encoding encoding)
                {
+                       if (s == null)
+                               return IntPtr.Zero;
+
+                       if (encoding == null)
+                               throw new ArgumentNullException ("encoding");
+
                        int min_byte_count = encoding.GetMaxByteCount(1);
                        char[] copy = s.ToCharArray (index, count);
                        byte[] marshal = new byte [encoding.GetByteCount (copy) + min_byte_count];
@@ -321,7 +332,7 @@ namespace Mono.Unix {
 
                        IntPtr mem = AllocHeap (marshal.Length);
                        if (mem == IntPtr.Zero)
-                               throw new OutOfMemoryException ();
+                               throw new UnixIOException (Native.Errno.ENOMEM);
 
                        bool copied = false;
                        try {
@@ -400,14 +411,15 @@ namespace Mono.Unix {
                        switch (errno) {
                                case Native.Errno.EBADF:
                                case Native.Errno.EINVAL:        return new ArgumentException (message, p);
+
                                case Native.Errno.ERANGE:        return new ArgumentOutOfRangeException (message);
                                case Native.Errno.ENOTDIR:       return new DirectoryNotFoundException (message, p);
-                               case Native.Errno.EFAULT:        return new NullReferenceException (message, p);
+                               case Native.Errno.ENOENT:        return new FileNotFoundException (message, p);
 
-                               case Native.Errno.EACCESS:
-                               case Native.Errno.EISDIR:        return new UnauthorizedAccessException (message, p);
+                               case Native.Errno.EOPNOTSUPP:
+                               case Native.Errno.EPERM:         return new InvalidOperationException (message, p);
 
-                               case Native.Errno.ENOENT:        return new FileNotFoundException (message, p);
+                               case Native.Errno.ENOEXEC:       return new InvalidProgramException (message, p);
 
                                case Native.Errno.EIO:
                                case Native.Errno.ENOSPC:
@@ -416,13 +428,13 @@ namespace Mono.Unix {
                                case Native.Errno.EROFS:
                                case Native.Errno.ESPIPE:        return new IOException (message, p);
 
-                               case Native.Errno.EOPNOTSUPP:
-                               case Native.Errno.EPERM:         return new InvalidOperationException (message, p);
-
-                               case Native.Errno.ENOEXEC:       return new InvalidProgramException (message, p);
-                               case Native.Errno.ENOMEM:        return new OutOfMemoryException (message, p);
+                               case Native.Errno.EFAULT:        return new NullReferenceException (message, p);
                                case Native.Errno.EOVERFLOW:     return new OverflowException (message, p);
                                case Native.Errno.ENAMETOOLONG:  return new PathTooLongException (message, p);
+
+                               case Native.Errno.EACCES:
+                               case Native.Errno.EISDIR:        return new UnauthorizedAccessException (message, p);
+
                                default: /* ignore */     break;
                        }
                        return p;