Outdooractive / route-me

Open source map library for iOS
Other
122 stars 400 forks source link

Unbracketed IF bugs #90

Closed rpcarver closed 12 years ago

rpcarver commented 12 years ago

I have found a few examples of unbracketed if statements that will cause bugs when built for release. The following is one example. It can be found in RMDatabaseCache.m in the function - (UIImage )cachedImage:(RMTile)tile withCacheKey:(NSString )aCacheKey

                 if (result == NO)
                     RMLog(@"Error expiring cache");

                 [[db executeQuery:@"VACUUM"] close];

In release mode (with the current pch) this will become

                 if (result == NO)
                     // do nothing.

                 [[db executeQuery:@"VACUUM"] close];

Please do this old programmer's heart a favor and make it part of your coding standards to bracket ALL your if statements. If it only removes one potential bug down the line its worth it. I swear! :)

BTW - you guys are doing a great job!

-- Randy

trasch commented 12 years ago

Your second code snippet is actually wrong, it should be:

                 if (result == NO)
                     ; // do nothing.

                 [[db executeQuery:@"VACUUM"] close];

Note the single ; in front of the comment, this is because the semicolon is not part of the macro and will terminate the expression.

See this small example to test this:

#include <stdio.h>

#if 0
#define MY_MACRO printf("Hello, ")
#else
#define MY_MACRO
#endif

int main()
{
    if (0)
        MY_MACRO;

    printf("World!\n");
}

This small programm will always print "World!", no matter if the macro is empty or not.

I'm not the god of macro programmers (I have a record saying otherwise), so if you find other examples of where a macro might be wrong then don't hesitate to open an issue here, and thanks for your effors!