Infinite loop in DigitsNumberValidator

21 views
Skip to first unread message

Koen Maes

unread,
Dec 18, 2011, 3:01:56 PM12/18/11
to gwt-val...@googlegroups.com
In public class DigitsNumberValidator implements ConstraintValidator<Digits, Number>

This method isValid(Number value, ConstraintValidatorContext context)
when entered with value = new Integer(2), loops forever on the bold text. variable integerPart becomes and stays 0, so condition never changes.

@Override
public boolean isValid(Number value, ConstraintValidatorContext context) {
if(value == null) return true;
boolean result = true;
//in this case "integer" refers to the whole number part of the input value
long integerPart = value.longValue();
//fraction part refers to the portions of the number less than a whole number
double fractionPart = value.doubleValue() - integerPart;
//initialize the digit counts
int intDigits = 0;
int frcDigits = 0;
//count the digits while the integer part is non zero and whole
while(integerPart >= 0) {
//increment count
intDigits++;
//remove a digit, should work fine because of long type truncation
integerPart = integerPart / 10;
}
//count the digits while the fractional part is non zero and still fractional
while(fractionPart <= 0) {
//increment count
frcDigits++;
//chop off a digit, and subtract the integer portion
fractionPart = (fractionPart * 10)-((int)fractionPart);
}
//the digits validator specifies the maximum number of digits, going over that will result in 
//a failing validation
if(intDigits > this.integer || frcDigits > this.fraction) {
result = false;
}
return result;
}

Koen Maes

unread,
Dec 18, 2011, 3:03:22 PM12/18/11
to gwt-val...@googlegroups.com
I probably shouldnt be checking the integer part of an Integer either

chris.r

unread,
Dec 19, 2011, 11:29:39 AM12/19/11
to gwt-validation
As of r282 there is no longer a loop in DigitsNumberValidator...

http://code.google.com/p/gwt-validation/source/browse/trunk/src/main/java/com/em/validation/client/validators/DigitsNumberValidator.java

I took out the loop because I suspected that all the type coercion
going on was producing an infinite loop. I suspect that this was
related to issue #53. I can no longer reproduce the issue but I made
two separate changes that impacted it.

I should probably build a 2.0 jar...

Reply all
Reply to author
Forward
0 new messages