Undefined Behaviour in ccCArray.h (signed / unsigned conversion)

ccArrayGetIndexOfObject currently returns ~~1, although the signature requires an unsigned int.
I propose the following patch to ccCArray.h:
<pre>
@ -107,7 +107,7@
}
}
~~/* Returns index of first occurence of object, UXNotFound if object not found./
*/* Returns index of first occurrence of object, UXNotFound if object not found./
static inline unsigned int ccArrayGetIndexOfObject
{
for
@ -118,7 +118,7@
}
}

  • return –1;
  • return UINT_MAX;
    }

/* Returns a Boolean value that indicates whether object is present in array./

Thank you for your work.
#516 is created for this issue.

The following changes would also be necessary:

CCActionManager.cpp:

@@ -286,7 +286,7 @@
    if (pElement)
    {
        unsigned int i = ccArrayGetIndexOfObject(pElement->actions, pAction);
-       if (i != -1)
+       if (i != UINT_MAX)
        {
            removeActionAtIndex(i, pElement);
        }

CCSpriteBatchNode.cpp:

@@ -422,7 +422,7 @@
        // ignore parent Z if parent is spriteSheet
        bool bIgnoreParent = (CCSpriteBatchNode*)(pobSprite->getParent()) == this;
        CCSprite *pPrevious = NULL;
-       if (uChildIndex > 0)
+       if (uChildIndex > 0 && uChildIndex < UINT_MAX)
        {
            pPrevious = (CCSprite*)(pBrothers->objectAtIndex(uChildIndex - 1));
        }
@@ -537,7 +537,7 @@
        pobSprite->useSelfRender();

        unsigned int uIndex = m_pobDescendants->indexOfObject(pobSprite);
-       if (uIndex != -1)
+       if (uIndex != UINT_MAX)
        {
            m_pobDescendants->removeObjectAtIndex(uIndex);

ccCArray.h:

@@ -124,7 +124,7 @@
 /** Returns a Boolean value that indicates whether object is present in array. */
 static inline bool ccArrayContainsObject(ccArray *arr, CCObject* object)
 {
-   return ccArrayGetIndexOfObject(arr, object) != -1;
+   return ccArrayGetIndexOfObject(arr, object) != UINT_MAX;
 }

 /** Appends an object. Bahaviour undefined if array doesn't have enough capacity. */
@@ -210,7 +210,7 @@
 static inline void ccArrayFastRemoveObject(ccArray *arr, CCObject* object)
 {
     unsigned int index = ccArrayGetIndexOfObject(arr, object);
-    if (index != -1)
+    if (index != UINT_MAX)
         ccArrayFastRemoveObjectAtIndex(arr, index);
 }

@@ -220,7 +220,7 @@
 {
    unsigned int index = ccArrayGetIndexOfObject(arr, object);

-   if (index != -1)
+   if (index != UINT_MAX)
    {
        ccArrayRemoveObjectAtIndex(arr, index);
    }

Thank you, how kind are you.

I’m sorry — I have caused a fuss over nothing. The standard apparently (http://stackoverflow.com/questions/2760502/question-about-c-behaviour-for-unsigned-integer-underflow) specifies that –1, when converted to unsigned, must become to UINT_MAX. The current code causes warnings on some compilers, but not undefined behaviour.