VirtualBox

Changeset 79677 in vbox for trunk/include


Ignore:
Timestamp:
Jul 10, 2019 3:45:05 PM (6 years ago)
Author:
vboxsync
Message:

Runtime/r3/xml.cpp: Introduce methods which limit the size of element and attribute values when querying them. Just for sanitizing, not that the buffer size is actually limited.
Main/Appliance+ExtPack: Use size checks to play safe with XML sata.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/xml.h

    r79569 r79677  
    5858typedef struct _xmlNode xmlNode;
    5959
     60#define RT_XML_CONTENT_SMALL _8K
     61#define RT_XML_CONTENT_LARGE _128K
     62#define RT_XML_ATTR_TINY 64
     63#define RT_XML_ATTR_SMALL _1K
     64#define RT_XML_ATTR_MEDIUM _8K
     65#define RT_XML_ATTR_LARGE _64K
     66
    6067/** @} */
    6168
     
    253260     * Opens a file with the given name in the given mode. If @a aMode is Read
    254261     * or ReadWrite, the file must exist. If @a aMode is Write, the file must
    255      * not exist. Otherwise, an EIPRTFailure excetion will be thrown.
     262     * not exist. Otherwise, an EIPRTFailure exception will be thrown.
    256263     *
    257264     * @param aMode     File mode.
     
    410417
    411418    const char *getValue() const;
     419    const char *getValueN(size_t cchValueLimit) const;
    412420    bool copyValue(int32_t &i) const;
    413421    bool copyValue(uint32_t &i) const;
     
    594602        if (pElem)
    595603            return pElem->getValue();
     604        return NULL;
     605    }
     606
     607    /** Finds the first child with matching the give name and optionally namspace,
     608     *  returning its value. Checks the length against the limit.
     609     *
     610     * @returns Pointer to the child string value, NULL if not found or no value.
     611     * @param   pcszPath        Path to the child element.  Slashes can be used to
     612     *                          make a simple path to any decendant.
     613     * @param   cchValueLimit   If the length of the returned value exceeds this
     614     *                          limit a EIPRTFailure exception will be thrown.
     615     * @param   pcszNamespace   The namespace to match, NULL (default) match any
     616     *                          namespace.  When using a path, this matches all
     617     *                          elements along the way.
     618     * @see     findChildElement, findChildElementP
     619     */
     620    const char *findChildElementValuePN(const char *pcszPath, size_t cchValueLimit, const char *pcszNamespace = NULL) const
     621    {
     622        const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
     623        if (pElem)
     624            return pElem->getValueN(cchValueLimit);
    596625        return NULL;
    597626    }
     
    646675    }
    647676
     677    /** Combines findChildElementP and findAttributeValueN.
     678     *
     679     * @returns Pointer to attribute string value, NULL if either the element or
     680     *          the attribute was not found.
     681     * @param   pcszPath            The attribute name.  Slashes can be used to make a
     682     *                              simple path to any decendant.
     683     * @param   pcszAttribute       The attribute name.
     684     * @param   cchValueLimit       If the length of the returned value exceeds this
     685     *                              limit a EIPRTFailure exception will be thrown.
     686     * @param   pcszPathNamespace   The namespace to match @a pcszPath with, NULL
     687     *                              (default) match any namespace.  When using a
     688     *                              path, this matches all elements along the way.
     689     * @param   pcszAttributeNamespace  The namespace prefix to apply to the
     690     *                              attribute, NULL (default) match any namespace.
     691     * @see     findChildElementP and findAttributeValue
     692     */
     693    const char *findChildElementAttributeValuePN(const char *pcszPath, const char *pcszAttribute,
     694                                                 size_t cchValueLimit,
     695                                                 const char *pcszPathNamespace = NULL,
     696                                                 const char *pcszAttributeNamespace = NULL) const
     697    {
     698        const ElementNode *pElem = findChildElementP(pcszPath, pcszPathNamespace);
     699        if (pElem)
     700            return pElem->findAttributeValueN(pcszAttribute, cchValueLimit, pcszAttributeNamespace);
     701        return NULL;
     702    }
     703
    648704
    649705    /** @name Tree enumeration.
     
    733789        if (pAttr)
    734790            return pAttr->getValue();
     791        return NULL;
     792    }
     793    /** Find the first attribute with the given name, returning its value string.
     794     * @returns Pointer to the attribute string value.
     795     * @param   pcszName        The attribute name.
     796     * @param   cchValueLimit   If the length of the returned value exceeds this
     797     *                          limit a EIPRTFailure exception will be thrown.
     798     * @param   pcszNamespace   The namespace name, default is NULL which means
     799     *                          anything goes.
     800     * @see getAttributeValue
     801     */
     802    const char *findAttributeValueN(const char *pcszName, size_t cchValueLimit, const char *pcszNamespace = NULL) const
     803    {
     804        const AttributeNode *pAttr = findAttribute(pcszName, pcszNamespace);
     805        if (pAttr)
     806            return pAttr->getValueN(cchValueLimit);
    735807        return NULL;
    736808    }
     
    752824    bool getAttributeValue(const char *pcszMatch, bool &f, const char *pcszNamespace = NULL) const
    753825    { return getAttributeValue(pcszMatch, &f, pcszNamespace); }
     826    bool getAttributeValueN(const char *pcszMatch, const char *&pcsz, size_t cchValueLimit, const char *pcszNamespace = NULL) const
     827    { return getAttributeValueN(pcszMatch, &pcsz, cchValueLimit, pcszNamespace); }
     828    bool getAttributeValueN(const char *pcszMatch, RTCString &str, size_t cchValueLimit, const char *pcszNamespace = NULL) const
     829    { return getAttributeValueN(pcszMatch, &str, cchValueLimit, pcszNamespace); }
     830    bool getAttributeValuePathN(const char *pcszMatch, RTCString &str, size_t cchValueLimit, const char *pcszNamespace = NULL) const
     831    { return getAttributeValueN(pcszMatch, &str, cchValueLimit, pcszNamespace); }
    754832
    755833    /** @name Variants that for clarity does not use references for output params.
     
    763841    bool getAttributeValue(const char *pcszMatch, uint64_t *pu, const char *pcszNamespace = NULL) const;
    764842    bool getAttributeValue(const char *pcszMatch, bool *pf, const char *pcszNamespace = NULL) const;
     843    bool getAttributeValueN(const char *pcszMatch, const char **ppcsz, size_t cchValueLimit, const char *pcszNamespace = NULL) const;
     844    bool getAttributeValueN(const char *pcszMatch, RTCString *pStr, size_t cchValueLimit, const char *pcszNamespace = NULL) const;
     845    bool getAttributeValuePathN(const char *pcszMatch, RTCString *pStr, size_t cchValueLimit, const char *pcszNamespace = NULL) const;
    765846    /** @} */
    766847
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