[vbox-dev] [PATCH] Fix integer truncation when writing XML files

Benjamin Gilbert bgilbert at cs.cmu.edu
Thu Jan 27 03:53:10 GMT 2011


Hello,

When VirtualBox >= 4.0.0 writes out the machine XML file, it truncates
<CpuIdLeaf> attribute values to nine decimal digits.  This can corrupt
some CPUID register values and always corrupts leaf IDs >= 0x80000000,
causing all leaves in the extended CPUID region to be ignored the next
time the machine is loaded (and dropped from the XML the next time it
is saved).  The cause is commit r33469, which reduced the size of
buffers in the XML formatting code below the maximum needed to
represent integer data types.  The patch below, which is released under
the MIT license, corrects this.

--Benjamin Gilbert


--- src/VBox/Runtime/r3/xml.cpp	(revision 35690)
+++ src/VBox/Runtime/r3/xml.cpp	(working copy)
@@ -1097,7 +1097,7 @@
  */
 AttributeNode* ElementNode::setAttribute(const char *pcszName, int32_t i)
 {
-    char szValue[10];
+    char szValue[12];  // negative sign + 10 digits + \0
     RTStrPrintf(szValue, sizeof(szValue), "%RI32", i);
     AttributeNode *p = setAttribute(pcszName, szValue);
     return p;
@@ -1116,7 +1116,7 @@
  */
 AttributeNode* ElementNode::setAttribute(const char *pcszName, uint32_t u)
 {
-    char szValue[10];
+    char szValue[11];  // 10 digits + \0
     RTStrPrintf(szValue, sizeof(szValue), "%RU32", u);
     AttributeNode *p = setAttribute(pcszName, szValue);
     return p;
@@ -1135,7 +1135,7 @@
  */
 AttributeNode* ElementNode::setAttribute(const char *pcszName, int64_t i)
 {
-    char szValue[20];
+    char szValue[21];  // negative sign + 19 digits + \0
     RTStrPrintf(szValue, sizeof(szValue), "%RI64", i);
     AttributeNode *p = setAttribute(pcszName, szValue);
     return p;
@@ -1154,7 +1154,7 @@
  */
 AttributeNode* ElementNode::setAttribute(const char *pcszName, uint64_t u)
 {
-    char szValue[20];
+    char szValue[21];  // 20 digits + \0
     RTStrPrintf(szValue, sizeof(szValue), "%RU64", u);
     AttributeNode *p = setAttribute(pcszName, szValue);
     return p;
@@ -1173,7 +1173,7 @@
  */
 AttributeNode* ElementNode::setAttributeHex(const char *pcszName, uint32_t u)
 {
-    char szValue[10];
+    char szValue[11];  // "0x" + 8 digits + \0
     RTStrPrintf(szValue, sizeof(szValue), "0x%RX32", u);
     AttributeNode *p = setAttribute(pcszName, szValue);
     return p;





More information about the vbox-dev mailing list