VirtualBox

Ignore:
Timestamp:
Aug 10, 2018 1:43:18 PM (6 years ago)
Author:
vboxsync
Message:

Runtime/RTJson: Promote type assertions In query functions to always
executed type checks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/json.cpp

    r72955 r73616  
    12891289{
    12901290    PRTJSONVALINT pThis = hJsonVal;
     1291    AssertPtrReturn(pThis, RTJSONVALTYPE_INVALID);
     1292
    12911293    if (pThis == NIL_RTJSONVAL)
    12921294        return RTJSONVALTYPE_INVALID;
    1293     AssertPtrReturn(pThis, RTJSONVALTYPE_INVALID);
    12941295
    12951296    return pThis->enmType;
    12961297}
    12971298
     1299
     1300#define AssertJsonTypeReturn(pJson, enmExpectedType, ret) do {          \
     1301        AssertPtrReturn((pJson), (ret));                                \
     1302        AssertReturn((pJson) != NIL_RTJSONVAL, (ret));                  \
     1303        AssertReturn((pJson)->enmType == (enmExpectedType), (ret));     \
     1304    } while (0)
     1305
     1306#define RTJSON_TYPECHECK(pJson, enmExpectedType) do {   \
     1307        if (RT_UNLIKELY((pJson) == NIL_RTJSONVAL))      \
     1308            return VERR_INVALID_HANDLE;                 \
     1309        if ((pJson)->enmType != (enmExpectedType))      \
     1310            return VERR_JSON_VALUE_INVALID_TYPE;        \
     1311    } while (0)
     1312
     1313
     1314#define RTJSON_TYPECHECK_CONTAINER(pJson) do {                  \
     1315        if (RT_UNLIKELY((pJson) == NIL_RTJSONVAL))              \
     1316            return VERR_INVALID_HANDLE;                         \
     1317        if (   (pJson)->enmType != RTJSONVALTYPE_ARRAY          \
     1318            && (pJson)->enmType != RTJSONVALTYPE_OBJECT)        \
     1319            return VERR_JSON_VALUE_INVALID_TYPE;                \
     1320    } while (0)
     1321
     1322
    12981323RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal)
    12991324{
    13001325    PRTJSONVALINT pThis = hJsonVal;
    1301     AssertReturn(pThis != NIL_RTJSONVAL, NULL);
    1302     AssertReturn(pThis->enmType == RTJSONVALTYPE_STRING, NULL);
     1326    AssertJsonTypeReturn(pThis, RTJSONVALTYPE_STRING, NULL);
    13031327
    13041328    return pThis->Type.String.pszStr;
    13051329}
    13061330
     1331
    13071332RTDECL(int) RTJsonValueQueryString(RTJSONVAL hJsonVal, const char **ppszStr)
    13081333{
    13091334    PRTJSONVALINT pThis = hJsonVal;
     1335    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    13101336    AssertPtrReturn(ppszStr, VERR_INVALID_POINTER);
    1311     AssertReturn(pThis != NIL_RTJSONVAL, VERR_INVALID_HANDLE);
    1312     AssertReturn(pThis->enmType == RTJSONVALTYPE_STRING, VERR_JSON_VALUE_INVALID_TYPE);
    1313 
     1337
     1338    RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_STRING);
    13141339    *ppszStr = pThis->Type.String.pszStr;
    13151340    return VINF_SUCCESS;
     
    13191344{
    13201345    PRTJSONVALINT pThis = hJsonVal;
     1346    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    13211347    AssertPtrReturn(pi64Num, VERR_INVALID_POINTER);
    1322     AssertReturn(pThis != NIL_RTJSONVAL, VERR_INVALID_HANDLE);
    1323     AssertReturn(pThis->enmType == RTJSONVALTYPE_NUMBER, VERR_JSON_VALUE_INVALID_TYPE);
    1324 
     1348
     1349    RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_NUMBER);
    13251350    *pi64Num = pThis->Type.Number.i64Num;
    13261351    return VINF_SUCCESS;
     
    13301355{
    13311356    PRTJSONVALINT pThis = hJsonVal;
     1357    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    13321358    AssertPtrReturn(pszName, VERR_INVALID_POINTER);
    13331359    AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
    1334     AssertReturn(pThis != NIL_RTJSONVAL, VERR_INVALID_HANDLE);
    1335     AssertReturn(pThis->enmType == RTJSONVALTYPE_OBJECT, VERR_JSON_VALUE_INVALID_TYPE);
     1360
     1361    RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_OBJECT);
    13361362
    13371363    int rc = VERR_NOT_FOUND;
     
    14071433{
    14081434    PRTJSONVALINT pThis = hJsonVal;
    1409     AssertReturn(pThis != NIL_RTJSONVAL, 0);
    1410     AssertReturn(pThis->enmType == RTJSONVALTYPE_ARRAY, 0);
     1435    AssertJsonTypeReturn(pThis, RTJSONVALTYPE_ARRAY, 0);
    14111436
    14121437    return pThis->Type.Array.cItems;
     
    14161441{
    14171442    PRTJSONVALINT pThis = hJsonVal;
     1443    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    14181444    AssertPtrReturn(pcItems, VERR_INVALID_POINTER);
    1419     AssertReturn(pThis != NIL_RTJSONVAL, VERR_INVALID_HANDLE);
    1420     AssertReturn(pThis->enmType == RTJSONVALTYPE_ARRAY, VERR_JSON_VALUE_INVALID_TYPE);
    1421 
     1445
     1446    RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_ARRAY);
    14221447    *pcItems = pThis->Type.Array.cItems;
    14231448    return VINF_SUCCESS;
     
    14271452{
    14281453    PRTJSONVALINT pThis = hJsonVal;
     1454    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    14291455    AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
    1430     AssertReturn(pThis != NIL_RTJSONVAL, VERR_INVALID_HANDLE);
    1431     AssertReturn(pThis->enmType == RTJSONVALTYPE_ARRAY, VERR_JSON_VALUE_INVALID_TYPE);
    1432     AssertReturn(idx < pThis->Type.Array.cItems, VERR_OUT_OF_RANGE);
     1456
     1457    RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_ARRAY);
     1458    if (RT_UNLIKELY(idx >= pThis->Type.Array.cItems))
     1459        return VERR_OUT_OF_RANGE;
    14331460
    14341461    RTJsonValueRetain(pThis->Type.Array.papItems[idx]);
     
    14401467{
    14411468    PRTJSONVALINT pThis = hJsonVal;
     1469    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    14421470    AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
    1443     AssertReturn(pThis != NIL_RTJSONVAL, VERR_INVALID_HANDLE);
    1444     AssertReturn(pThis->enmType == RTJSONVALTYPE_ARRAY || pThis->enmType == RTJSONVALTYPE_OBJECT,
    1445                  VERR_JSON_VALUE_INVALID_TYPE);
     1471
     1472    RTJSON_TYPECHECK_CONTAINER(pThis);
    14461473
    14471474    PRTJSONITINT pIt = (PRTJSONITINT)RTMemTmpAllocZ(sizeof(RTJSONITINT));
     
    14601487{
    14611488    PRTJSONITINT pIt = hJsonIt;
     1489    AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
    14621490    AssertReturn(pIt != NIL_RTJSONIT, VERR_INVALID_HANDLE);
    14631491    AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
     
    15001528{
    15011529    PRTJSONITINT pIt = hJsonIt;
     1530    AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
    15021531    AssertReturn(pIt != NIL_RTJSONIT, VERR_INVALID_HANDLE);
    15031532
     
    15291558{
    15301559    PRTJSONITINT pThis = hJsonIt;
     1560    AssertPtrReturnVoid(pThis);
     1561
    15311562    if (pThis == NIL_RTJSONIT)
    15321563        return;
    1533     AssertPtrReturnVoid(pThis);
    15341564
    15351565    RTJsonValueRelease(pThis->pJsonVal);
    15361566    RTMemTmpFree(pThis);
    15371567}
    1538 
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