VirtualBox

Changeset 80373 in vbox for trunk/include/VBox


Ignore:
Timestamp:
Aug 21, 2019 2:51:02 PM (5 years ago)
Author:
vboxsync
Message:

com/array.h: handling empty base elements better (hopefully will eliminate static code analysis issues) by using a const array as the representation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/array.h

    r76585 r80373  
    224224{
    225225
     226/** Used for dummy element access in com::SafeArray, avoiding crashes. */
     227extern const char Zeroes[16];
     228
     229
    226230#ifdef VBOX_WITH_XPCOM
    227231
     
    618622     * Creates a null array.
    619623     */
    620     SafeArray() {}
     624    SafeArray() { }
    621625
    622626    /**
     
    734738        AssertReturnVoid(!isNull());
    735739
    736         int i = 0;
     740        size_t i = 0;
    737741        for (typename Map::const_iterator it = aMap.begin();
    738742             it != aMap.end(); ++ it, ++ i)
     
    935939    /**
    936940     * Array access operator that returns an array element by reference. A bit
    937      * safer than #raw(): asserts and returns an invalid reference if this
    938      * instance is null or if the index is out of bounds.
     941     * safer than #raw(): asserts and returns a reference to a static zero
     942     * element (const, i.e. writes will fail) if this instance is null or
     943     * if the index is out of bounds.
    939944     *
    940945     * @note For weak instances, this call will succeed but the behavior of
     
    944949    T &operator[] (size_t aIdx)
    945950    {
    946         AssertReturn(m.arr != NULL,  *((T *)NULL));
    947         AssertReturn(aIdx < size(), *((T *)NULL));
     951        /** @todo r=klaus should do this as a AssertCompile, but cannot find a way which works. */
     952        Assert(sizeof(T) <= sizeof(Zeroes));
     953        AssertReturn(m.arr != NULL, *(T *)&Zeroes[0]);
     954        AssertReturn(aIdx < size(), *(T *)&Zeroes[0]);
    948955#ifdef VBOX_WITH_XPCOM
    949956        return m.arr[aIdx];
    950957#else
    951         AssertReturn(m.raw != NULL,  *((T *)NULL));
     958        AssertReturn(m.raw != NULL, *(T *)&Zeroes[0]);
    952959        return m.raw[aIdx];
    953960#endif
     
    959966    const T operator[] (size_t aIdx) const
    960967    {
    961         AssertReturn(m.arr != NULL,  *((T *)1));
    962         AssertReturn(aIdx < size(), *((T *)1));
     968        AssertReturn(m.arr != NULL, *(const T *)&Zeroes[0]);
     969        AssertReturn(aIdx < size(), *(const T *)&Zeroes[0]);
    963970#ifdef VBOX_WITH_XPCOM
    964971        return m.arr[aIdx];
    965972#else
    966         AssertReturn(m.raw != NULL,  *((T *)NULL));
     973        AssertReturn(m.raw != NULL, *(const T *)&Zeroes[0]);
    967974        return m.raw[aIdx];
    968975#endif
     
    13771384    public:
    13781385
    1379         nsIDRef(nsID * &aVal) : mVal(aVal) {}
    1380 
    1381         operator const nsID &() const { return mVal ? *mVal : *Empty; }
    1382         operator nsID() const { return mVal ? *mVal : *Empty; }
    1383 
    1384         const nsID *operator&() const { return mVal ? mVal : Empty; }
     1386        nsIDRef(nsID * &aVal) : mVal(aVal) { AssertCompile(sizeof(nsID) <= sizeof(Zeroes)); }
     1387
     1388        operator const nsID &() const { return mVal ? *mVal : *(const nsID *)&Zeroes[0]; }
     1389        operator nsID() const { return mVal ? *mVal : *(nsID *)&Zeroes[0]; }
     1390
     1391        const nsID *operator&() const { return mVal ? mVal : (const nsID *)&Zeroes[0]; }
    13851392
    13861393        nsIDRef &operator= (const nsID &aThat)
     
    13971404        nsID * &mVal;
    13981405
    1399         static const nsID *Empty;
    1400 
    14011406        friend class SafeGUIDArray;
    14021407    };
     
    14301435        Assert(m.arr != NULL);
    14311436        Assert(aIdx < size());
    1432         return m.arr[aIdx] ? *m.arr[aIdx] : *nsIDRef::Empty;
     1437        return m.arr[aIdx] ? *m.arr[aIdx] : *(const nsID *)&Zeroes[0];
    14331438    }
    14341439};
     
    14481453
    14491454    /** See SafeArray<>::SafeArray(). */
    1450     SafeConstGUIDArray() {}
     1455    SafeConstGUIDArray() { AssertCompile(sizeof(nsID) <= sizeof(Zeroes)); }
    14511456
    14521457    /* See SafeArray<>::SafeArray(ComSafeArrayIn(T, aArg)). */
     
    14631468    const nsID &operator[] (size_t aIdx) const
    14641469    {
    1465         AssertReturn(m.arr != NULL,  **((const nsID * *)1));
    1466         AssertReturn(aIdx < size(), **((const nsID * *)1));
     1470        AssertReturn(m.arr != NULL,  *(const nsID *)&Zeroes[0]);
     1471        AssertReturn(aIdx < size(), *(const nsID *)&Zeroes[0]);
    14671472        return *m.arr[aIdx];
    14681473    }
     
    16561661        AssertReturnVoid(!Base::isNull());
    16571662
    1658         int i = 0;
     1663        size_t i = 0;
    16591664        for (typename List::const_iterator it = aCntr.begin();
    16601665             it != aCntr.end(); ++ it, ++ i)
     
    16851690        AssertReturnVoid(!Base::isNull());
    16861691
    1687         int i = 0;
     1692        size_t i = 0;
    16881693        for (typename List::const_iterator it = aCntr.begin();
    16891694             it != aCntr.end(); ++ it, ++ i)
     
    17171722        AssertReturnVoid(!Base::isNull());
    17181723
    1719         int i = 0;
     1724        size_t i = 0;
    17201725        for (typename Map::const_iterator it = aMap.begin();
    17211726             it != aMap.end(); ++ it, ++ i)
     
    17491754        AssertReturnVoid(!Base::isNull());
    17501755
    1751         int i = 0;
     1756        size_t i = 0;
    17521757        for (typename Map::const_iterator it = aMap.begin();
    17531758             it != aMap.end(); ++ it, ++ i)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette