Skip to contentSkip to conversation

PATCH: Replace asprintf() with snprintf() in vnc.c

Qemu-devel Applied Daniel P. Berrange 3 participants 3 messages Mar 11, 2009 – Mar 13, 2009

lore
Share & tools
lore
#0 lore raw

As previously discussed, this patch removes the non-portable use of asprintf(), replacing it with malloc+snprintf instead

Daniel

vnc.c +7 -2
Index: vnc.c
===================================================================
@@ -53,6 +53,7 @@
     char host[NI_MAXHOST];
     char serv[NI_MAXSERV];
     int err;
+    size_t addrlen;
 
     if ((err = getnameinfo((struct sockaddr *)sa, salen,
                            host, sizeof(host),
@@ -63,8 +64,12 @@
         return NULL;
     }
 
-    if (asprintf(&addr, format, host, serv) < 0)
-        return NULL;
+    /* Enough for the existing format + the 2 vars we're
+     * subsituting in. */
+    addrlen = strlen(format) + strlen(host) + strlen(serv);
+    addr = qemu_malloc(addrlen + 1);
+    snprintf(addr, addrlen, format, host, serv);
+    addr[addrlen] = '\0';
 
     return addr;
 }
Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#1 lore raw

Hi,

now vnc.c compiles nicely on Solaris as well...

Regard
Palle

Quoted 49 lines · Daniel P. Berrange

Date: Wed, 11 Mar 2009 14:55:06 +0000
From: Daniel P. Berrange <[email protected]>
To: [email protected]
Subject: [Qemu-devel] PATCH: Replace asprintf() with snprintf() in vnc.c

As previously discussed, this patch removes the non-portable use of asprintf(), replacing it with malloc+snprintf instead

Daniel

Index: vnc.c
===================================================================

vnc.c +7 -2
@@ -53,6 +53,7 @@
    char host[NI_MAXHOST];
    char serv[NI_MAXSERV];
    int err;
+    size_t addrlen;
 
    if ((err = getnameinfo((struct sockaddr *)sa, salen,
                           host, sizeof(host),
@@ -63,8 +64,12 @@
        return NULL;
    }
 
-    if (asprintf(&addr, format, host, serv) < 0)
-        return NULL;
+    /* Enough for the existing format + the 2 vars we're
+     * subsituting in. */
+    addrlen = strlen(format) + strlen(host) + strlen(serv);
+    addr = qemu_malloc(addrlen + 1);
+    snprintf(addr, addrlen, format, host, serv);
+    addr[addrlen] = '\0';
 
    return addr;
 }
Signature · 5 lines
-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|