mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2025-11-08 13:30:03 -05:00
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:
parent
6b0fb2bc7e
commit
d04e72c9a5
1 changed files with 29 additions and 5 deletions
|
|
@ -19,22 +19,43 @@
|
||||||
#include "tplg_local.h"
|
#include "tplg_local.h"
|
||||||
|
|
||||||
#define SAVE_ALLOC_SHIFT (13) /* 8192 bytes */
|
#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, ...)
|
int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
char buf[1024], *s;
|
char *buf, *s;
|
||||||
size_t n, l, t, pl;
|
size_t n, l, t, pl;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
buf = malloc(PRINT_BUF_SIZE);
|
||||||
|
if (!buf)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
if (pfx == NULL)
|
if (pfx == NULL)
|
||||||
pfx = "";
|
pfx = "";
|
||||||
|
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
n = vsnprintf(buf, sizeof(buf), fmt, va);
|
n = vsnprintf(buf, PRINT_BUF_SIZE, fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
if (n >= sizeof(buf))
|
if (n >= PRINT_BUF_SIZE_MAX) {
|
||||||
return -EOVERFLOW;
|
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);
|
pl = strlen(pfx);
|
||||||
l = *dst ? strlen(*dst) : 0;
|
l = *dst ? strlen(*dst) : 0;
|
||||||
|
|
@ -47,7 +68,8 @@ int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
|
||||||
if (s == NULL) {
|
if (s == NULL) {
|
||||||
free(*dst);
|
free(*dst);
|
||||||
*dst = NULL;
|
*dst = NULL;
|
||||||
return -ENOMEM;
|
ret = -ENOMEM;
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
s = *dst;
|
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, pfx);
|
||||||
strcpy(s + l + pl, buf);
|
strcpy(s + l + pl, buf);
|
||||||
*dst = s;
|
*dst = s;
|
||||||
|
end:
|
||||||
|
free(buf);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue