Main Site Documentation

Compiler Warning: opcode 'conv.ovf.i' -- overflow will not throw exception


#1

I received this strange warning “opcode ‘conv.ovf.i’ – overflow will not throw exception” in a project that I’m working on. I was only able to find one other mention of this warning in a Google search (http://groups.google.com/group/microsoft.public.dotnet.framework.microframework/browse_thread/thread/48f10d2344c080f7), but the user had no answer for it. The warning was pointing to the byte array declaration:

byte[] postData = new byte[contLen];

in the function:


protected byte[] ReceivePostData(HttpListenerRequest request)
{
     long contLen = request.ContentLength64;

     byte[] postData = new byte[contLen];

     int dataRead = request.InputStream.Read(postData, 0, (int)contLen);
     if (dataRead != 0)
     {
            return postData;
      }
}

After looking at the code a little longer, I realized I was casting the long to an int for the

request.InputStream.Read()

function, I decided to try and cast it at the byte array declaration as well, so the declaration reads:

byte[] postData = new byte[(int)contLen];

.

This cast eliminated this warning.

I wanted to share this in case anyone else encountered this warning, as it is not immediately obvious what is causing this warning.


#2

Thanks for sharing :wink:


#3

Nice catch.

@ Brady: I hitted reply on Allen Dang’s question before I realised you are the person that should reply to his question, so please do :slight_smile: The fun thing is, Allen Dang works (or worked) at microsoft when he was asking that question.


#4

It would seem that

request.InputStream.Read()

is only able to read in a buffer lenth up to an ‘int’ in size. If that is the case there is no reason to declare contlen as a ‘long’ as that just seems to confuse the matter. What not try this:

int contLen = (int)request.ContentLength64;

(and get rid of your other casts)

That way your intent is clear and you won’t accidnetly try and use ‘contLen’ as a ‘long’ again.

Just a thought…