Exception not found, although thrown

Analyzes .NET assemblies for types of exceptions

Moderators: Brian Donahue, Charles Brown, Alex.Davies, AndrewH

Exception not found, although thrown

Postby Uniwares_AS » Mon May 26, 2008 10:12 pm

I have a particular piece of code where EH1 just reports one possible exception (System.ArgumentException) but it definitely throws a System.IndexOutOfRangeException

Here is the code in question:

Code: Select all
      private const int _maxSegments = 10;
      private Object SyncRoot = new Object();
      private Bandwidth _bandwidth;
      private int[] _segments = new int[_maxSegments];
      private int _lastSegment;
      private long _maxBytesPerSegment;
      private long _currentlyUsedBytes;

      public void CountAndWait(int nBytes)
      {
         lock(SyncRoot)
         {
            int nTickCount = Environment.TickCount;

            int nTickSegment = (nTickCount / (1000 / _maxSegments)) % _maxSegments;
            _currentlyUsedBytes = SegmentSum();
            long maxBytes =   _bandwidth.MaxBytesPerSecond;

            if(maxBytes == 0 || _bandwidth.IsUnlimited)
            {
               return;
            }

            if(maxBytes < nBytes)
            {
               SegmentsClear();
               System.Threading.Thread.Sleep(1000);
               return;
            }

            if(_segments[nTickSegment] + nBytes > _maxBytesPerSegment)
            {
               System.Threading.Thread.Sleep(1000 / _maxSegments);

               nTickSegment = (nTickSegment + 1) % _maxSegments;
               _segments[nTickSegment] = nBytes;
            }
            else
            {
               _segments[nTickSegment] += nBytes;
            }

            int nOldestTickSegment = (nTickSegment + 1) % _maxSegments;
            if(_lastSegment != nTickSegment)
            {
               _segments[nOldestTickSegment] = 0;
               _lastSegment = nTickSegment;
            }
         }
      }


The two functions SegmentSum() and SegmentClear() do not throw any exception. _bandwidth is a settings container.

Any clues?
Uniwares_AS
 
Posts: 132
Joined: Thu Oct 11, 2007 3:29 pm

Postby Brian Donahue » Thu May 29, 2008 9:15 am

Hi,

The best thing I can suggest is to turn on or off the use more detailed analysis setting. Exception Hunter tries to eliminate exceptions that cannot possibly happen based on conditional branches and the like. For instance if you test for a null and skip a line of code that can throw a null-reference exception based on the findings, Exception Hunter can work out that the null-reference can never logically happen.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby AndrewH » Thu May 29, 2008 10:29 am

Hi,

I think you're encountering a limitation of the algorithm used by Exception Hunter: it does not report all possible exceptions generated by the .NET runtime. Usually when we can't determine with certainty whether or not an exception will be thrown we err on the side of caution and report it.

However, the IndexOutOfRangeException thrown by the runtime on some array accesses is an exception to this general rule. The problem is that our model doesn't extend to array sizes and has only limited support for integer values: not enough to ever be able to determine whether or not an array access is safe or not.

Array accesses are so common in the .NET framework that this means that we would report this exception for nearly every non-trivial function. The overwhelming majority of these would be from safe code that never throws the exception in reality. We decided that as this exception would be reported everywhere it would be better not to report it as it would be rare that the information is actually useful.

I think that there are improvements that may be made so that we could detect some more obvious cases where this could happen, though there would be concerns about the effect this would have on the performance of the tool.
Andrew Hunter
Software Developer
Red Gate Software Ltd.
AndrewH
 
Posts: 134
Joined: Thu Aug 17, 2006 3:44 pm

Postby Uniwares_AS » Thu May 29, 2008 3:07 pm

I understand. Yet its a curious case, since as far as I can see, there should be no possibility to exceed the bounds of the array, except if the tick count would be negative, which is not the case.

Already tried various settings to see if EH1 shows more info. Without success in this case.

Well, time to engage other tricks.
Uniwares_AS
 
Posts: 132
Joined: Thu Oct 11, 2007 3:29 pm


Return to Exception Hunter 1

Who is online

Users browsing this forum: No registered users and 0 guests