[libvirt] [PATCH 10/10] vbox: avoid sprintf

Eric Blake eblake at redhat.com
Wed Aug 18 23:45:13 UTC 2010


Work in progress:
This gets 2 of the 5 suspect uses; I ran out of time today.

* src/vbox/vbox_tmpl.c (vboxStartMachine): Use virAsprintf instead.
---

I could check this in as is (after tweaking the commit comment)
and do the other three sprintf uses in another patch, but I'd
rather merge all sprintf changes in this file to a single commit.
At any rate, reviewing this portion for any memory leaks now will
be helpful.

 src/vbox/vbox_tmpl.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index c28981c..6b9de6c 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -3162,7 +3162,6 @@ vboxStartMachine(virDomainPtr dom, int i, IMachine *machine, vboxIID *iid
     PRUnichar *valueDisplayUtf16 = NULL;
     char      *valueDisplayUtf8  = NULL;
     IProgress *progress          = NULL;
-    char displayutf8[32]         = {0};
     PRUnichar *env               = NULL;
     PRUnichar *sessionType       = NULL;
     nsresult rc;
@@ -3242,8 +3241,13 @@ vboxStartMachine(virDomainPtr dom, int i, IMachine *machine, vboxIID *iid

     if (guiPresent) {
         if (guiDisplay) {
-            sprintf(displayutf8, "DISPLAY=%.24s", guiDisplay);
-            VBOX_UTF8_TO_UTF16(displayutf8, &env);
+            char *displayutf8;
+            if (virAsprintf(&displayutf8, "DISPLAY=%s", guiDisplay) < 0)
+                virReportOOMError();
+            else {
+                VBOX_UTF8_TO_UTF16(displayutf8, &env);
+                VIR_FREE(displayutf8);
+            }
             VIR_FREE(guiDisplay);
         }

@@ -3252,8 +3256,13 @@ vboxStartMachine(virDomainPtr dom, int i, IMachine *machine, vboxIID *iid

     if (sdlPresent) {
         if (sdlDisplay) {
-            sprintf(displayutf8, "DISPLAY=%.24s", sdlDisplay);
-            VBOX_UTF8_TO_UTF16(displayutf8, &env);
+            char *displayutf8;
+            if (virAsprintf(&displayutf8, "DISPLAY=%s", sdlDisplay) < 0)
+                virReportOOMError();
+            else {
+                VBOX_UTF8_TO_UTF16(displayutf8, &env);
+                VIR_FREE(displayutf8);
+            }
             VIR_FREE(sdlDisplay);
         }

-- 
1.7.2.1




More information about the libvir-list mailing list