topology: Make buffer for saving dynamic size

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

Signed-off-by: Piotr Maziarz <piotrx.maziarz@linux.intel.com>
Reviewed-by: Cezary Rojewski <cezary.rojewski@intel.com>
Reviewed-by: Amadeusz Sławiński <amadeuszx.slawinski@linux.intel.com>
Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Piotr Maziarz 2020-08-31 11:09:03 +02:00 committed by Jaroslav Kysela
parent 6b0fb2bc7e
commit d04e72c9a5

View file

@ -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;
}