开发者

Are there any parallel issues with Integer.MAX_VALUE

I have the following code:

if (maxLength>=0 && (++totalLength > maxLength))
    throw new IllegalStateException("Form too large");

in a loop where bytes are read from byte array input stream. The maxLength is set to Integer.MAX_VALUE so I think that the condition could never be true (and I'm not talking about the size of the byte array in input stream which I'm absolutely sure is not long enough). But I get the IllegalStateException thrown from that line!!! Now the real bummer is that when I put a breakpoint on that throw line, everything is ok. How the hell is this possible?

EDIT:

  • both variables are of type int
  • totalLength is a local variable, maxLength is a parameter
  • the debugger don't stop there AND the exception is not thrown at all, when there is a breakpoint on that throw line.
  • I actualy don't know why I'm suspecting parallelism, it's just because it's web application
  • I admit that using MAX_VALUE is very risky (in the next step I will try to decrease this limit), but I would expect some other execption than that in the success branch of if statement. And moreover that byte array used in input stream is really not long enough. This should be plainly impossible situation in JVM:-).
  • The code above is in jetty-util-7.1.5开发者_JAVA技巧.v20100705.jar in the class UrlEncoded and I'm using it by calling

    byte[] decodedBytes;
    byte[] encodedBytes;
    
    // v pripade url encoded requestu je potreba pouze odriznout
    // jmeno falesneho parametru nesouciho kodovany blok
    encodedBytes = Arrays.copyOfRange(content, "encdata=".length(), content.length);
    decodedBytes = decodeBytes(request, encodedBytes);
    
    // priprav desifrovany text jako vstupni proud
    decodedInputStream = new ByteArrayInputStream(decodedBytes);
    
    // pokud je request url encoded je potreba jej manualne parsovat
    // pro potreby funkci vracejicich parametry
    UrlEncoded.decodeTo(decodedInputStream, parameters, request.getCharacterEncoding(), Integer.MAX_VALUE);
    


Are there any parallel issues with Integer.MAX_VALUE

No, this is most likely not due to any race-conditions (unless you have other threads modifying maxLength or so).

According to the JLS on integer is larger than Integer.MAX_VALUE so this is either due to a bug in your VM or based on the false assumption that maxLength is indeed Integer.MAX_VALUE.

The maxLength is set to Integer.MAX_VALUE so I think that the condition could never be true

Make sure totalLength is not a long.

(The snippet below always throws the exception.)

int maxLength = Integer.MAX_VALUE;

long totalLength = 2147483647;

if (maxLength>=0 && (++totalLength > maxLength))
    throw new IllegalStateException("Form too large");

If it's not reproducible with the debugger, just give a more informative error message to the exception, for instance: "Form too large: " + totalLength + " is larger than " + maxLength


Are you sure you want to increment totalLength before the comparison, rather than after? If your totalLength going into that statement is equal to maxLength, the exception will be thrown... is that a possible scenario?

Try totalLength++ instead.

Just a thought. Hope this helps.


Now the real bummer is that when I put a breakpoint on that throw line, everything is ok

Do you mean the debugger doesn't stop there?

From your question title it seems you suspect a racing condition (multithreading) issue. How are the variables defined? Could you post some more code?

Could maxLength be modified elsewhere? Or is it final?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜