From cb7449002b78cb91f3da8822d3a93ee1d4d1b3f9 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Mon, 14 Nov 2011 12:07:12 +0100 Subject: [PATCH] pacmd: dynamically allocate ibuf and obuf 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 | 21 +++++++++++++++------ 1 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/utils/pacmd.c b/src/utils/pacmd.c index 4166964..248fbd1 100644 --- a/src/utils/pacmd.c +++ b/src/utils/pacmd.c @@ -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]; @@ -102,6 +103,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; @@ -109,11 +115,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++; } @@ -182,7 +188,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; @@ -202,7 +208,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; @@ -260,5 +266,8 @@ fail: if (fd >= 0) pa_close(fd); + pa_xfree(obuf); + pa_xfree(ibuf); + return ret; } -- 1.7.7