qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 01/49] char: remove fixed length filename allocation
Date: Tue, 26 Jan 2016 14:46:33 +0100	[thread overview]
Message-ID: <1453816041-36362-2-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1453816041-36362-1-git-send-email-pbonzini@redhat.com>

From: "Daniel P. Berrange" <berrange@redhat.com>

A variety of places were snprintf()ing into a fixed length
filename buffer. Some of the buffers were stack allocated,
while another was heap allocated with g_malloc(). Switch
them all to heap allocated using g_strdup_printf() avoiding
arbitrary length restrictions.

This also facilitates later patches which will want to
populate the filename by calling external functions
which do not support use of a pre-allocated buffer.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Message-Id: <1453202071-10289-2-git-send-email-berrange@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu-char.c | 86 +++++++++++++++++++++++++++++++------------------------------
 1 file changed, 44 insertions(+), 42 deletions(-)

diff --git a/qemu-char.c b/qemu-char.c
index e133f4f..8e96f90 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -88,39 +88,37 @@
 
 #define READ_BUF_LEN 4096
 #define READ_RETRIES 10
-#define CHR_MAX_FILENAME_SIZE 256
 #define TCP_MAX_FDS 16
 
 /***********************************************************/
 /* Socket address helpers */
 
-static int SocketAddress_to_str(char *dest, int max_len,
-                                const char *prefix, SocketAddress *addr,
-                                bool is_listen, bool is_telnet)
+static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr,
+                                  bool is_listen, bool is_telnet)
 {
     switch (addr->type) {
     case SOCKET_ADDRESS_KIND_INET:
-        return snprintf(dest, max_len, "%s%s:%s:%s%s", prefix,
-                        is_telnet ? "telnet" : "tcp", addr->u.inet->host,
-                        addr->u.inet->port, is_listen ? ",server" : "");
+        return g_strdup_printf("%s%s:%s:%s%s", prefix,
+                               is_telnet ? "telnet" : "tcp", addr->u.inet->host,
+                               addr->u.inet->port, is_listen ? ",server" : "");
         break;
     case SOCKET_ADDRESS_KIND_UNIX:
-        return snprintf(dest, max_len, "%sunix:%s%s", prefix,
-                        addr->u.q_unix->path, is_listen ? ",server" : "");
+        return g_strdup_printf("%sunix:%s%s", prefix,
+                               addr->u.q_unix->path,
+                               is_listen ? ",server" : "");
         break;
     case SOCKET_ADDRESS_KIND_FD:
-        return snprintf(dest, max_len, "%sfd:%s%s", prefix, addr->u.fd->str,
-                        is_listen ? ",server" : "");
+        return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd->str,
+                               is_listen ? ",server" : "");
         break;
     default:
         abort();
     }
 }
 
-static int sockaddr_to_str(char *dest, int max_len,
-                           struct sockaddr_storage *ss, socklen_t ss_len,
-                           struct sockaddr_storage *ps, socklen_t ps_len,
-                           bool is_listen, bool is_telnet)
+static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len,
+                             struct sockaddr_storage *ps, socklen_t ps_len,
+                             bool is_listen, bool is_telnet)
 {
     char shost[NI_MAXHOST], sserv[NI_MAXSERV];
     char phost[NI_MAXHOST], pserv[NI_MAXSERV];
@@ -129,9 +127,9 @@ static int sockaddr_to_str(char *dest, int max_len,
     switch (ss->ss_family) {
 #ifndef _WIN32
     case AF_UNIX:
-        return snprintf(dest, max_len, "unix:%s%s",
-                        ((struct sockaddr_un *)(ss))->sun_path,
-                        is_listen ? ",server" : "");
+        return g_strdup_printf("unix:%s%s",
+                               ((struct sockaddr_un *)(ss))->sun_path,
+                               is_listen ? ",server" : "");
 #endif
     case AF_INET6:
         left  = "[";
@@ -142,14 +140,14 @@ static int sockaddr_to_str(char *dest, int max_len,
                     sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
         getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
                     pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
-        return snprintf(dest, max_len, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
-                        is_telnet ? "telnet" : "tcp",
-                        left, shost, right, sserv,
-                        is_listen ? ",server" : "",
-                        left, phost, right, pserv);
+        return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
+                               is_telnet ? "telnet" : "tcp",
+                               left, shost, right, sserv,
+                               is_listen ? ",server" : "",
+                               left, phost, right, pserv);
 
     default:
-        return snprintf(dest, max_len, "unknown");
+        return g_strdup_printf("unknown");
     }
 }
 
@@ -1074,15 +1072,18 @@ static CharDriverState *qemu_chr_open_pipe(const char *id,
 {
     ChardevHostdev *opts = backend->u.pipe;
     int fd_in, fd_out;
-    char filename_in[CHR_MAX_FILENAME_SIZE];
-    char filename_out[CHR_MAX_FILENAME_SIZE];
+    char *filename_in;
+    char *filename_out;
     const char *filename = opts->device;
     ChardevCommon *common = qapi_ChardevHostdev_base(backend->u.pipe);
 
-    snprintf(filename_in, CHR_MAX_FILENAME_SIZE, "%s.in", filename);
-    snprintf(filename_out, CHR_MAX_FILENAME_SIZE, "%s.out", filename);
+
+    filename_in = g_strdup_printf("%s.in", filename);
+    filename_out = g_strdup_printf("%s.out", filename);
     TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
     TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
+    g_free(filename_in);
+    g_free(filename_out);
     if (fd_in < 0 || fd_out < 0) {
 	if (fd_in >= 0)
 	    close(fd_in);
@@ -2115,7 +2116,7 @@ static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
     OVERLAPPED ov;
     int ret;
     DWORD size;
-    char openname[CHR_MAX_FILENAME_SIZE];
+    char *openname;
 
     s->fpipe = TRUE;
 
@@ -2130,11 +2131,12 @@ static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
         goto fail;
     }
 
-    snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
+    openname = g_strdup_printf("\\\\.\\pipe\\%s", filename);
     s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
                               PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
                               PIPE_WAIT,
                               MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
+    g_free(openname);
     if (s->hcom == INVALID_HANDLE_VALUE) {
         error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
         s->hcom = NULL;
@@ -2913,8 +2915,9 @@ static void tcp_chr_disconnect(CharDriverState *chr)
     s->chan = NULL;
     closesocket(s->fd);
     s->fd = -1;
-    SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
-                         "disconnected:", s->addr, s->is_listen, s->is_telnet);
+    g_free(chr->filename);
+    chr->filename = SocketAddress_to_str("disconnected:", s->addr,
+                                         s->is_listen, s->is_telnet);
     qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
     if (s->reconnect_time) {
         qemu_chr_socket_restart_timer(chr);
@@ -2989,16 +2992,16 @@ static void tcp_chr_connect(void *opaque)
     socklen_t ss_len = sizeof(ss), ps_len = sizeof(ps);
 
     memset(&ss, 0, ss_len);
+    g_free(chr->filename);
     if (getsockname(s->fd, (struct sockaddr *) &ss, &ss_len) != 0) {
-        snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
-                 "Error in getsockname: %s\n", strerror(errno));
+        chr->filename = g_strdup_printf("Error in getsockname: %s\n",
+                                        strerror(errno));
     } else if (getpeername(s->fd, (struct sockaddr *) &ps, &ps_len) != 0) {
-        snprintf(chr->filename, CHR_MAX_FILENAME_SIZE,
-                 "Error in getpeername: %s\n", strerror(errno));
+        chr->filename = g_strdup_printf("Error in getpeername: %s\n",
+                                        strerror(errno));
     } else {
-        sockaddr_to_str(chr->filename, CHR_MAX_FILENAME_SIZE,
-                        &ss, ss_len, &ps, ps_len,
-                        s->is_listen, s->is_telnet);
+        chr->filename = sockaddr_to_str(&ss, ss_len, &ps, ps_len,
+                                        s->is_listen, s->is_telnet);
     }
 
     s->connected = 1;
@@ -4335,9 +4338,8 @@ static CharDriverState *qmp_chardev_open_socket(const char *id,
     /* be isn't opened until we get a connection */
     chr->explicit_be_open = true;
 
-    chr->filename = g_malloc(CHR_MAX_FILENAME_SIZE);
-    SocketAddress_to_str(chr->filename, CHR_MAX_FILENAME_SIZE, "disconnected:",
-                         addr, is_listen, is_telnet);
+    chr->filename = SocketAddress_to_str("disconnected:",
+                                         addr, is_listen, is_telnet);
 
     if (is_listen) {
         if (is_telnet) {
-- 
1.8.3.1

  reply	other threads:[~2016-01-26 13:47 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-26 13:46 [Qemu-devel] [PULL 00/49] chardev, NBD, cpus, scripts/ changes for 2015-01-26 Paolo Bonzini
2016-01-26 13:46 ` Paolo Bonzini [this message]
2016-01-26 13:46 ` [Qemu-devel] [PULL 02/49] char: convert from GIOChannel to QIOChannel Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 03/49] char: don't assume telnet initialization will not block Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 04/49] char: introduce support for TLS encrypted TCP chardev backend Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 05/49] docs: Style the command and its options in the synopsis Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 06/49] qemu-char: avoid leak in qemu_chr_open_pp_fd Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 07/49] scripts/kvm/kvm_stat: Cleanup of multiple imports Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 08/49] scripts/kvm/kvm_stat: Replaced os.listdir with os.walk Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 09/49] scripts/kvm/kvm_stat: Make constants uppercase Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 10/49] scripts/kvm/kvm_stat: Removed unneeded PERF constants Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 11/49] scripts/kvm/kvm_stat: Mark globals in functions Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 12/49] scripts/kvm/kvm_stat: Invert dictionaries Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 13/49] scripts/kvm/kvm_stat: Cleanup of path variables Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 14/49] scripts/kvm/kvm_stat: Improve debugfs access checking Paolo Bonzini
2016-02-02 14:02   ` Christian Borntraeger
2016-02-02 14:25     ` Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 15/49] scripts/kvm/kvm_stat: Introduce main function Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 16/49] scripts/kvm/kvm_stat: Fix spaces around keyword assignments Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 17/49] scripts/kvm/kvm_stat: Rename variables that redefine globals Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 18/49] scripts/kvm/kvm_stat: Moved DebugfsProvider Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 19/49] scripts/kvm/kvm_stat: Fixup syscall error reporting Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 20/49] scripts/kvm/kvm_stat: Set sensible no. files rlimit Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 21/49] scripts/kvm/kvm_stat: Cleanup of platform detection Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 22/49] scripts/kvm/kvm_stat: Make cpu detection a function Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 23/49] scripts/kvm/kvm_stat: Rename _perf_event_open Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 24/49] scripts/kvm/kvm_stat: Introduce properties for providers Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 25/49] scripts/kvm/kvm_stat: Cleanup of TracepointProvider Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 26/49] scripts/kvm/kvm_stat: Cleanup cpu list retrieval Paolo Bonzini
2016-01-26 13:46 ` [Qemu-devel] [PULL 27/49] scripts/kvm/kvm_stat: Encapsulate filters variable Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 28/49] scripts/kvm/kvm_stat: Cleanup of Stats class Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 29/49] scripts/kvm/kvm_stat: Cleanup of Groups class Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 30/49] scripts/kvm/kvm_stat: Cleanup of Event class Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 31/49] scripts/kvm/kvm_stat: Group arch specific data Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 32/49] scripts/kvm/kvm_stat: Remove unneeded X86_EXIT_REASONS Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 33/49] scripts/kvm/kvm_stat: Make tui function a class Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 34/49] scripts/kvm/kvm_stat: Fix output formatting Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 35/49] scripts/kvm/kvm_stat: Cleanup and pre-init perf_event_attr Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 36/49] scripts/kvm/kvm_stat: Read event values as u64 Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 37/49] scripts/kvm/kvm_stat: Fix rlimit for unprivileged users Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 38/49] scripts/kvm/kvm_stat: Fixup filtering Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 39/49] scripts/kvm/kvm_stat: Add interactive filtering Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 40/49] scripts/kvm/kvm_stat: Add optparse description Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 41/49] cpus: use broadcast on qemu_pause_cond Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 42/49] memory: exit when hugepage allocation fails if mem-prealloc Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 43/49] nbd: add missed aio_context_acquire in nbd_export_new Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 44/49] scripts/dump-guest-memory.py: Move constants to the top Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 45/49] scripts/dump-guest-memory.py: Make methods functions Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 46/49] scripts/dump-guest-memory.py: Improve python 3 compatibility Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 47/49] scripts/dump-guest-memory.py: Cleanup functions Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 48/49] scripts/dump-guest-memory.py: Introduce multi-arch support Paolo Bonzini
2016-01-26 13:47 ` [Qemu-devel] [PULL 49/49] scripts/dump-guest-memory.py: Fix module docstring Paolo Bonzini
2016-01-26 14:38 ` [Qemu-devel] [PULL 00/49] chardev, NBD, cpus, scripts/ changes for 2015-01-26 Peter Maydell
2016-01-26 15:29   ` Daniel P. Berrange
2016-01-26 16:06   ` [Qemu-devel] [PATCH] char: make io_channel_send be used unconditionally Daniel P. Berrange

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1453816041-36362-2-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).