Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The left operand of '>' is a garbage value

When i build and anylayse my app , i get the below memeory warning

"The left operand of '>' is a garbage value" at this line isTrue = (newLength > 20) ? NO : YES;

What is the problem over here.Thanks for any help

 (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength;
    BOOL isTrue;
    if(textField == txtFirstName){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue = (newLength > 20) ? NO : YES;
    }
    if(textField == txtLastName){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue = (newLength > 20) ? NO : YES;
    }
    if(textField == txtEmail){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 100) ? NO : YES;
    }
    if(textField == txtCompanyName){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    if(textField == txtPassword){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    if(textField == txtRe_EnterPassword){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    if(textField == txtZipCode){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 20) ? NO : YES;
    }
    if(textField == txtCountry){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    isTrue = (newLength > 20) ? NO : YES;

    return isTrue;
}
like image 518
user198725878 Avatar asked Oct 28 '11 05:10

user198725878


2 Answers

The problem occurs when all of the if conditions fail, ie. if textField is not one of the set you are expecting it to be.

In this case, newLength is never assigned to, and will be a random garbage value. Whilst this is probably "never going to happen", the static analyzer is not aware of that.

You should initialize newLength to have a default value, perhaps zero.

like image 171
Alex Deem Avatar answered Sep 28 '22 13:09

Alex Deem


the analyzer cannot confirm that your 'switch-like construct' does not 'fall through'. newLength's value may never be set if all those if cases fail. then in that event, it would qualify as a garbage value.

like image 43
justin Avatar answered Sep 28 '22 11:09

justin