OSDN Git Service

topology: Make buffer for saving dynamic size
authorPiotr Maziarz <piotrx.maziarz@linux.intel.com>
Mon, 31 Aug 2020 09:09:03 +0000 (11:09 +0200)
committerJaroslav Kysela <perex@perex.cz>
Mon, 31 Aug 2020 11:03:01 +0000 (13:03 +0200)
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>
src/topology/save.c

index 4ecf86c..9c74735 100644 (file)
 #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;
 }