runtime icon indicating copy to clipboard operation
runtime copied to clipboard

ILC mode will clear out ParamName for ArgumentNullException

Open caesar-chen opened this issue 9 years ago • 3 comments

System.Net.Sockets NullArgs_Throw() test runs fine in netcore/netfx/uap mode. But in ILC ('uap-aot') mode it is failing. It seems that the ParamName field has been optimized away:

        public void NullArgs_Throw(SocketImplementationType type)
        {
            int port;
            using (SocketTestServer.SocketTestServerFactory(type, _serverAddress, out port))
            {
                using (Socket sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
                {
                    sock.Connect(new IPEndPoint(_serverAddress, port));

                    ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() =>
                    {
                        sock.SendPacketsAsync(null);
                    });
                    Assert.Equal("e", ex.ParamName); //failure here, ex.ParamName == null
                }
            }
        }

Here is the implementation for SendPacketsAsync:

        public bool SendPacketsAsync(SocketAsyncEventArgs e)
        {
            ...
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }
            ...
        }

https://docs.microsoft.com/en-us/dotnet/api/system.argumentnullexception.-ctor?view=netcore-1.1#System_ArgumentNullException__ctor_System_String_ No matter what paramName value I passed in, ex.ParamName is always null.

But if I do:

                    SocketAsyncEventArgs e = null;
                    ArgumentNullException testEx = new ArgumentNullException(nameof(e));

This works, testEx.ParamName is "e".

cc: @davidsh

caesar-chen avatar Jun 14 '17 22:06 caesar-chen

Because of this problem, the NullList_Throws() fails as well:

        public void NullList_Throws(SocketImplementationType type)
        {
            ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() =>
            {
                SendPackets(type, (SendPacketsElement[])null, SocketError.Success, 0);
            });

            Assert.Equal("e.SendPacketsElements", ex.ParamName); // failure, ex.ParamName == null
        }

caesar-chen avatar Jun 14 '17 22:06 caesar-chen

This is by design.

The recommended remedy is to use AssertExtensions.Throws<>() to test ParamNames. It will test the paramname on non-uapaot platforms and ignore it on uapaot.

ghost avatar Jun 14 '17 22:06 ghost

@AtsushiKan Thank you! I will fix those test cases.

caesar-chen avatar Jun 14 '17 22:06 caesar-chen