Replace a couple of safe_strncpy/cat with snprintf.
[platform/upstream/flac.git] / src / libFLAC / metadata_iterators.c
index d2a229a..7349ff8 100644 (file)
@@ -37,6 +37,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 
 #include <sys/stat.h> /* for stat(), maybe chmod() */
 
@@ -47,6 +48,7 @@
 #include "share/alloc.h"
 #include "share/compat.h"
 #include "share/macros.h"
+#include "share/safe_str.h"
 #include "private/macros.h"
 #include "private/memory.h"
 
@@ -1376,7 +1378,7 @@ static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain)
 
 static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
 {
-       FILE *f, *tempfile;
+       FILE *f, *tempfile = NULL;
        char *tempfilename;
        FLAC__Metadata_SimpleIteratorStatus status;
        const FLAC__Metadata_Node *node;
@@ -3194,32 +3196,51 @@ FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCal
        return true;
 }
 
+static int
+local_snprintf(char *str, size_t size, const char *fmt, ...)
+{
+       va_list va;
+       int rc ;
+
+       va_start (va, fmt);
+
+#ifdef _MSC_VER
+       rc = vsnprintf_s (str, size, _TRUNCATE, fmt, va);
+       rc = (rc > 0) ? rc : (size == 0 ? 1024 : size * 2);
+#else
+       rc = vsnprintf (str, size, fmt, va);
+#endif
+       va_end (va);
+
+       return rc;
+}
+
 FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
 {
        static const char *tempfile_suffix = ".metadata_edit";
        if(0 == tempfile_path_prefix) {
-               if(0 == (*tempfilename = safe_malloc_add_3op_(strlen(filename), /*+*/strlen(tempfile_suffix), /*+*/1))) {
+               size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1;
+               if(0 == (*tempfilename = safe_malloc_(dest_len))) {
                        *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
                        return false;
                }
-               strcpy(*tempfilename, filename);
-               strcat(*tempfilename, tempfile_suffix);
+               local_snprintf(*tempfilename, dest_len, "%s%s", filename, tempfile_suffix);
        }
        else {
                const char *p = strrchr(filename, '/');
+               size_t dest_len;
                if(0 == p)
                        p = filename;
                else
                        p++;
 
-               if(0 == (*tempfilename = safe_malloc_add_4op_(strlen(tempfile_path_prefix), /*+*/strlen(p), /*+*/strlen(tempfile_suffix), /*+*/2))) {
+               dest_len = strlen(tempfile_path_prefix) + strlen(p) + strlen(tempfile_suffix) + 2;
+
+               if(0 == (*tempfilename = safe_malloc_(dest_len))) {
                        *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
                        return false;
                }
-               strcpy(*tempfilename, tempfile_path_prefix);
-               strcat(*tempfilename, "/");
-               strcat(*tempfilename, p);
-               strcat(*tempfilename, tempfile_suffix);
+               local_snprintf(*tempfilename, dest_len, "%s/%s%s", tempfile_path_prefix, p, tempfile_suffix);
        }
 
        if(0 == (*tempfile = fopen(*tempfilename, "w+b"))) {