alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: Piotr Maziarz <piotrx.maziarz@linux.intel.com>
To: alsa-devel@alsa-project.org
Cc: cezary.rojewski@intel.com, amadeuszx.slawinski@intel.com
Subject: [PATCH v2 10/10] topology: Make buffer for saving dynamic size
Date: Mon,  6 Jul 2020 11:06:03 +0200	[thread overview]
Message-ID: <1594026363-30276-11-git-send-email-piotrx.maziarz@linux.intel.com> (raw)
In-Reply-To: <1594026363-30276-1-git-send-email-piotrx.maziarz@linux.intel.com>

Some fields can exceed size limit, e.g. private data has no size
restriction. Therefore it needs to be dynamically increased.

Change-Id: Ie274fd304eba5d600e198f4febbc6216f01b57e1
---
 src/topology/save.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/topology/save.c b/src/topology/save.c
index 4ecf86c..9c74735 100644
--- a/src/topology/save.c
+++ b/src/topology/save.c
@@ -19,22 +19,43 @@
 #include "tplg_local.h"
 
 #define SAVE_ALLOC_SHIFT	(13)	/* 8192 bytes */
+#define PRINT_BUF_SIZE		(1024)
+#define PRINT_BUF_SIZE_MAX	(1024 * 1024)
 
 int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
 {
 	va_list va;
-	char buf[1024], *s;
+	char *buf, *s;
 	size_t n, l, t, pl;
+	int ret = 0;
+
+	buf = malloc(PRINT_BUF_SIZE);
+	if (!buf)
+		return -ENOMEM;
 
 	if (pfx == NULL)
 		pfx = "";
 
 	va_start(va, fmt);
-	n = vsnprintf(buf, sizeof(buf), fmt, va);
+	n = vsnprintf(buf, PRINT_BUF_SIZE, fmt, va);
 	va_end(va);
 
-	if (n >= sizeof(buf))
-		return -EOVERFLOW;
+	if (n >= PRINT_BUF_SIZE_MAX) {
+		ret = -EOVERFLOW;
+		goto end;
+	}
+
+	if (n >= PRINT_BUF_SIZE) {
+		char *tmp = realloc(buf, n + 1);
+		if (!tmp) {
+			ret = -ENOMEM;
+			goto end;
+		}
+		buf = tmp;
+		va_start(va, fmt);
+		n = vsnprintf(buf, n + 1, fmt, va);
+		va_end(va);
+	}
 
 	pl = strlen(pfx);
 	l = *dst ? strlen(*dst) : 0;
@@ -47,7 +68,8 @@ int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
 		if (s == NULL) {
 			free(*dst);
 			*dst = NULL;
-			return -ENOMEM;
+			ret = -ENOMEM;
+			goto end;
 		}
 	} else {
 		s = *dst;
@@ -57,6 +79,8 @@ int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
 		strcpy(s + l, pfx);
 	strcpy(s + l + pl, buf);
 	*dst = s;
+end:
+	free(buf);
 	return 0;
 }
 
-- 
2.7.4


  parent reply	other threads:[~2020-07-06 10:14 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-06  9:05 [PATCH v2 00/10] topology: decode: Various fixes Piotr Maziarz
2020-07-06  9:05 ` [PATCH v2 01/10] topology: decode: Fix channel map memory allocation Piotr Maziarz
2020-07-06  9:05 ` [PATCH v2 02/10] topology: decode: Fix infinite loop in decoding enum control Piotr Maziarz
2020-07-06  9:05 ` [PATCH v2 03/10] topology: decode: Remove decoding values for " Piotr Maziarz
2020-07-06 19:54   ` Pierre-Louis Bossart
2020-07-06  9:05 ` [PATCH v2 04/10] topology: decode: Add enum control texts as separate element Piotr Maziarz
2020-07-06  9:05 ` [PATCH v2 05/10] topology: decode: Fix printing texts section Piotr Maziarz
2020-07-06  9:05 ` [PATCH v2 06/10] topology: decode: Change declaration of enum decoding function Piotr Maziarz
2020-07-06  9:06 ` [PATCH v2 07/10] topology: decode: Fix decoding PCM formats and rates Piotr Maziarz
2020-07-06 19:58   ` Pierre-Louis Bossart
2020-07-06  9:06 ` [PATCH v2 08/10] topology: decode: Print sig_bits field in PCM capabilities section Piotr Maziarz
2020-07-06  9:06 ` [PATCH v2 09/10] topology: decode: Add DAI name printing Piotr Maziarz
2020-07-06  9:06 ` Piotr Maziarz [this message]
2020-07-06 20:01 ` [PATCH v2 00/10] topology: decode: Various fixes Pierre-Louis Bossart
2020-07-14 12:13   ` Piotr Maziarz

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=1594026363-30276-11-git-send-email-piotrx.maziarz@linux.intel.com \
    --to=piotrx.maziarz@linux.intel.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=amadeuszx.slawinski@intel.com \
    --cc=cezary.rojewski@intel.com \
    /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).