8

I use an open source to build my project. when I add EGOTextView to the project, it has Semantic Issues like:


Comparison of integers of different signs: 'int' and 'NSUInteger' (aka 'unsigned long')
Comparison of integers of different signs: 'NSInteger' (aka 'long') and 'NSUInteger' (aka 'unsigned long')

For example in source code:


     for (int i = 0; i < lines.count; i++)//lines is an array

I notice the project has build configure file which includes:

// Make CG and NS geometry types be the same. Mostly doesn't matter on iPhone, but this also makes NSInteger types be defined based on 'long' consistently, which avoids conflicting warnings from clang + llvm 2.7 about printf format checking

OTHER_CFLAGS = $(value) -DNS_BUILD_32_LIKE_64

According to the comments, I guess it causes the problems. However, I don't know the meaning for this OTHER_CFLAGS setting. And I also don't know how to fix it so that it can avoid the semantic issues.

Could any one help me?

Thanks!

4 Answers 4

26

Actually, I don't think turning off the compiler warning is the right solution, since comparing an int and an unsigned long introduces a subtle bug.

For example:

unsigned int a = UINT_MAX; // 0xFFFFFFFFU == 4,294,967,295 
signed int b = a; // 0xFFFFFFFF == -1

for (int i = 0; i < b; ++i)
{
    // the loop will have zero iterations because i < b is always false!
}

Basically if you simply cast away (implicitly or explicitly) an unsigned int to an int your code will behave incorrectly if the value of your unsigned int is greater than INT_MAX.

The correct solution is to cast the signed int to unsigned int and to also compare the signed int to zero, covering the case where it is negative:

unsigned int a = UINT_MAX; // 0xFFFFFFFFU == 4,294,967,295 

for (int i = 0; i < 0 || (unsigned)i < a; ++i)
{
    // The loop will have UINT_MAX iterations
}
7

Instead of doing all of this strange type casting all over the place, you ought to first notice why you are comparing different types in the first place: YOU ARE CREATING AN INT!!

do this instead:

    for (unsigned long i = 0; i < lines.count; i++)//lines is an array

...and now you are comparing the same types!

2
  • Something like this was suggested in this answer: stackoverflow.com/a/12400226/27678 OP is likely using int i for something, so we don't know for certain whether it's a good idea to just change its type altogether. In all likelihood they can; we just don't know though.
    – AndyG
    Feb 3, 2014 at 21:39
  • @AndyG This was NOT suggested above. [link](stackoverflow.com/a/12400226/27678) uses an implicit type cast (worse than the originally exposited explicit dynamic typecast which has runtime checking and is clear to read and sticks out while skimming code) to change the type. It is clear that the OP is simply iterating over an array from the original question: unsigned i is appropriate. May 9, 2014 at 19:00
4

The configuration option you're looking at won't do anything about the warning you quoted. What you need to do is go into your build settings and search for the "sign comparison" warning. Turn that off.

enter image description here

2
  • 7
    Instead of turning off warnings, how about fixing the bug? See my answer below.
    – j b
    Dec 12, 2013 at 12:13
  • If the file producing the warning is from an external lib, use the -w flag on the file instead. It will silence warnings on the file, but not on the rest of your project. Jan 31, 2014 at 17:21
3

Instead of turning the warnings of you can also prevent them from occurring.

Your lines.count is of type NSUInteger. Make an int of this first, and then do the comparison:

int count = lines.count;
for (int i = 0; i < count; i++)
1
  • This just disguises the bug the compiler is warning the OP about, it doesn't fix it. See my answer for an explanation why.
    – j b
    Feb 3, 2014 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.