diff options
author | vboxsync <vboxsync@cfe28804-0f27-0410-a406-dd0f0b0b656f> | 2010-02-26 12:59:51 +0000 |
---|---|---|
committer | vboxsync <vboxsync@cfe28804-0f27-0410-a406-dd0f0b0b656f> | 2010-02-26 12:59:51 +0000 |
commit | 93e0b69ffe17711a29283c02bf67af81d8fdbdf8 (patch) | |
tree | a681a83950682c02ce92d951026c47466c7911f7 /src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp | |
parent | d25ffb07fe325094a0e9a27ea005ae2cbff41c7c (diff) | |
download | VirtualBox-svn-93e0b69ffe17711a29283c02bf67af81d8fdbdf8.tar.gz |
RTSystemQueryDmiString-darwin.cpp: fixed leaks and buffer overflow handling.
git-svn-id: https://www.virtualbox.org/svn/vbox/trunk@26843 cfe28804-0f27-0410-a406-dd0f0b0b656f
Diffstat (limited to 'src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp')
-rw-r--r-- | src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp b/src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp index 817fbe9c122..d5a2c7d6e00 100644 --- a/src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp +++ b/src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp @@ -35,19 +35,25 @@ #include <iprt/system.h> #include "internal/iprt.h" -#include <iprt/err.h> #include <iprt/assert.h> +#include <iprt/err.h> +#include <iprt/mem.h> #include <iprt/string.h> #include <mach/mach_port.h> #include <IOKit/IOKitLib.h> + +/******************************************************************************* +* Defined Constants And Macros * +*******************************************************************************/ #define IOCLASS_PLATFORMEXPERTDEVICE "IOPlatformExpertDevice" #define PROP_PRODUCT_NAME "product-name" #define PROP_PRODUCT_VERSION "version" #define PROP_PRODUCT_SERIAL "IOPlatformSerialNumber" #define PROP_PRODUCT_UUID "IOPlatformUUID" + RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf) { AssertPtrReturn(pszBuf, VERR_INVALID_POINTER); @@ -79,6 +85,7 @@ RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t c if (!ClassToMatch) return VERR_NOT_SUPPORTED; + /* IOServiceGetMatchingServices will always consume ClassToMatch. */ io_iterator_t Iterator; kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator); if (kr != kIOReturnSuccess) @@ -91,24 +98,49 @@ RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t c if ( enmString == RTSYSDMISTR_PRODUCT_NAME || enmString == RTSYSDMISTR_PRODUCT_VERSION) { - CFDataRef DataRef = (CFDataRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef, kCFAllocatorDefault, kNilOptions); + CFDataRef DataRef = (CFDataRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef, + kCFAllocatorDefault, kNilOptions); if (DataRef) { - size_t cbData = CFDataGetLength(DataRef); - const uint8_t *pu8Data = CFDataGetBytePtr(DataRef); - memcpy(pszBuf, pu8Data, RT_MIN(cbData, cbBuf)); - pszBuf[RT_MIN(cbData + 1, cbBuf)] = '\0'; - rc = VINF_SUCCESS; + size_t cbData = CFDataGetLength(DataRef); + const char *pchData = (const char *)CFDataGetBytePtr(DataRef); + rc = RTStrCopyEx(pszBuf, cbBuf, pchData, cbData); + CFRelease(DataRef); break; } } else { - CFStringRef StringRef = (CFStringRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef, kCFAllocatorDefault, kNilOptions); + CFStringRef StringRef = (CFStringRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef, + kCFAllocatorDefault, kNilOptions); if (StringRef) { - Boolean res = CFStringGetCString(StringRef, pszBuf, cbBuf, kCFStringEncodingASCII); - rc = res == TRUE ? VINF_SUCCESS : VERR_NOT_SUPPORTED; + Boolean fRc = CFStringGetCString(StringRef, pszBuf, cbBuf, kCFStringEncodingUTF8); + if (fRc) + rc = VINF_SUCCESS; + else + { + CFIndex cwc = CFStringGetLength(StringRef); + size_t cbTmp = cwc + 1; + char *pszTmp = (char *)RTMemTmpAlloc(cbTmp); + int cTries = 1; + while ( pszTmp + && (fRc = CFStringGetCString(StringRef, pszTmp, cbTmp, kCFStringEncodingUTF8)) == FALSE + && cTries++ < 4) + { + RTMemTmpFree(pszTmp); + cbTmp *= 2; + pszTmp = (char *)RTMemTmpAlloc(cbTmp); + } + if (fRc) + rc = RTStrCopy(pszBuf, cbBuf, pszTmp); + else if (!pszTmp) + rc = VERR_NO_TMP_MEMORY; + else + rc = VERR_ACCESS_DENIED; + RTMemFree(pszTmp); + } + CFRelease(StringRef); break; } } @@ -118,3 +150,4 @@ RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t c IOObjectRelease(Iterator); return rc; } + |