pacmd: dynamically allocate ibuf and obuf
authorPino Toscano <toscano.pino@tiscali.it>
Mon, 14 Nov 2011 11:07:12 +0000 (12:07 +0100)
committerArun Raghavan <arun.raghavan@collabora.co.uk>
Mon, 2 Jan 2012 18:50:04 +0000 (00:20 +0530)
Use pa_pipe_buf to determine the minimum size for ibuf and obuf, taking into
account the two descriptors that use each of them.

See bug #42715

src/utils/pacmd.c

index 6d4cc9b..802832c 100644 (file)
@@ -47,8 +47,9 @@ int main(int argc, char*argv[]) {
     int fd = -1;
     int ret = 1, i;
     struct sockaddr_un sa;
-    char ibuf[PIPE_BUF], obuf[PIPE_BUF];
-    size_t ibuf_index, ibuf_length, obuf_index, obuf_length;
+    char *ibuf = NULL;
+    char *obuf = NULL;
+    size_t ibuf_size, ibuf_index, ibuf_length, obuf_size, obuf_index, obuf_length;
     char *cli;
     pa_bool_t ibuf_eof, obuf_eof, ibuf_closed, obuf_closed;
     struct pollfd pollfd[3];
@@ -104,6 +105,11 @@ int main(int argc, char*argv[]) {
         goto fail;
     }
 
+    i = pa_pipe_buf(fd);
+    ibuf_size = PA_MIN(i, pa_pipe_buf(STDIN_FILENO));
+    ibuf = pa_xmalloc(ibuf_size);
+    obuf_size = PA_MIN(i, pa_pipe_buf(STDOUT_FILENO));
+    obuf = pa_xmalloc(obuf_size);
     ibuf_index = ibuf_length = obuf_index = obuf_length = 0;
     ibuf_eof = obuf_eof = ibuf_closed = obuf_closed = FALSE;
 
@@ -111,11 +117,11 @@ int main(int argc, char*argv[]) {
         for (i = 1; i < argc; i++) {
             size_t k;
 
-            k = PA_MIN(sizeof(ibuf) - ibuf_length, strlen(argv[i]));
+            k = PA_MIN(ibuf_size - ibuf_length, strlen(argv[i]));
             memcpy(ibuf + ibuf_length, argv[i], k);
             ibuf_length += k;
 
-            if (ibuf_length < sizeof(ibuf)) {
+            if (ibuf_length < ibuf_size) {
                 ibuf[ibuf_length] = i < argc-1 ? ' ' : '\n';
                 ibuf_length++;
             }
@@ -184,7 +190,7 @@ int main(int argc, char*argv[]) {
                 ssize_t r;
                 pa_assert(ibuf_length <= 0);
 
-                if ((r = pa_read(STDIN_FILENO, ibuf, sizeof(ibuf), &stdin_type)) <= 0) {
+                if ((r = pa_read(STDIN_FILENO, ibuf, ibuf_size, &stdin_type)) <= 0) {
                     if (r < 0) {
                         pa_log(_("read(): %s"), strerror(errno));
                         goto fail;
@@ -204,7 +210,7 @@ int main(int argc, char*argv[]) {
                 ssize_t r;
                 pa_assert(obuf_length <= 0);
 
-                if ((r = pa_read(fd, obuf, sizeof(obuf), &fd_type)) <= 0) {
+                if ((r = pa_read(fd, obuf, obuf_size, &fd_type)) <= 0) {
                     if (r < 0) {
                         pa_log(_("read(): %s"), strerror(errno));
                         goto fail;
@@ -262,5 +268,8 @@ fail:
     if (fd >= 0)
         pa_close(fd);
 
+    pa_xfree(obuf);
+    pa_xfree(ibuf);
+
     return ret;
 }