Skip to content

numericRangeCheck logic tree fix for default no errors #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2014

Conversation

stevewasiura
Copy link
Contributor

discovered bug when inputs within range, default return value "OK" not getting returned to caller due to else branch

removed else branch & curly braces.

explained:
input val = 1, allowedrange=min1
logic tree entry will be at line 1034 indexOf min
but value will not be less than minval, so error return is skipped, but the else branch on line 1039 will not be entered, so it is also skipped, and nothing is returned to the caller.

since nothing is returned first, program flow will continue into new line 1039 and func will return array with single element val="ok"

here is expanded function for testing by paste replace into script and look at console.log for debugging:

/**
* Test numeric against allowed range
*
* @param $value int
* @param $rangeAllowed str; (1-2, min1, max2)
* @return array
*/
numericRangeCheck : function($value, $rangeAllowed)
{ console.log('numericRangeCheck inputs: value "'+$value+'", rangeAllowed "' +$rangeAllowed+'"' );
// split by dash
var range = $.split($rangeAllowed, '-');
console.log('range='+range);
// range ?
if (range.length == 2 && ($value < parseInt(range[0],10) || $value > parseInt(range[1],10) ))
{ // value is out of range
console.log('value "'+$value+'", outside of range "' +$rangeAllowed+'"' );
return [ "out", range[0], range[1] ] ;
}
else if ($rangeAllowed.indexOf('min') === 0) // min
{ console.log('range min');
// check if above min
var minQty = parseInt($rangeAllowed.substr(3),10);
console.log(minQty);
if($value < minQty) {
console.log('value "'+$value+'", less than min "' +minQty+'"' );
return [ "min", minQty ] ;
}
}
else if ($rangeAllowed.indexOf('max') === 0) // max
{ console.log('range max');
var maxQty = parseInt($rangeAllowed.substr(3),10);
if($value > maxQty) {
console.log('value "'+$value+'", more than max "' +maxQty+'"' );
return [ "max", maxQty ] ;
}
}

       // value is in allowed range
       console.log('value "'+$value+'", within range "' +$rangeAllowed+'"' );
       return [ "ok" ];
    },

discovered bug when inputs within range, default return value "OK" not getting returned to caller due to else branch

removed else branch & curly braces.

explained:
input val = 1, allowedrange=min1
logic tree entry will be at line 1034 indexOf min
but value will not be less than minval, so error return is skipped, but the else branch on line 1039 will not be entered, so it is also skipped, and nothing is returned to the caller.

since nothing is returned first, program flow will continue into new line 1039 and func will return array with single element val="ok"



here is expanded function for testing by paste replace into script and look at console.log for debugging:

/**
        * Test numeric against allowed range
        *
        * @param $value int
        * @param $rangeAllowed str; (1-2, min1, max2)
        * @return array
        */
        numericRangeCheck : function($value, $rangeAllowed) 
        {   console.log('numericRangeCheck inputs: value "'+$value+'", rangeAllowed "' +$rangeAllowed+'"' );
           // split by dash
           var range = $.split($rangeAllowed, '-');
           console.log('range='+range);
           // range ?
           if (range.length == 2 && ($value < parseInt(range[0],10) || $value > parseInt(range[1],10) ))
               {   // value is out of range
                   console.log('value "'+$value+'", outside of range "' +$rangeAllowed+'"' );
                   return [ "out", range[0], range[1] ] ;
               }
               else if ($rangeAllowed.indexOf('min') === 0) // min
                   {   console.log('range min');
                       // check if above min
                       var minQty = parseInt($rangeAllowed.substr(3),10);
                       console.log(minQty);
                       if($value < minQty) { 
                           console.log('value "'+$value+'", less than min "' +minQty+'"' );
                            return [ "min", minQty ] ; 
                       }
                   }
                   else if ($rangeAllowed.indexOf('max') === 0) // max
                       {   console.log('range max'); 
                           var maxQty = parseInt($rangeAllowed.substr(3),10);
                            if($value > maxQty) { 
                                console.log('value "'+$value+'", more than max "' +maxQty+'"' );
                                return [ "max", maxQty ] ; 
                            }
                       }

           // value is in allowed range
           console.log('value "'+$value+'", within range "' +$rangeAllowed+'"' );
           return [ "ok" ];
        },
victorjonsson added a commit that referenced this pull request Sep 1, 2014
numericRangeCheck logic tree fix for default no errors
@victorjonsson victorjonsson merged commit 1b99b52 into victorjonsson:master Sep 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants