From 28383cf083afa2b38d5c10605e4d3c9f9847c3e5 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 24 Sep 2003 19:10:16 +0000 Subject: [PATCH] Implemented a more compatible method of getting the required buffer size for a sprintf/vsprintf. --- src/chapter_parser_xml.cpp | 10 +++++----- src/chapters.cpp | 4 ++-- src/common.cpp | 31 +++++++++++++++++++++++++++++++ src/common.h | 3 +++ src/mkvmerge.cpp | 6 ------ 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/chapter_parser_xml.cpp b/src/chapter_parser_xml.cpp index ed860d555..f01417ef8 100644 --- a/src/chapter_parser_xml.cpp +++ b/src/chapter_parser_xml.cpp @@ -69,12 +69,12 @@ static void cperror(parser_data_t *pdata, const char *fmt, ...) { int len; fix_format(fmt, new_fmt); - len = snprintf(NULL, 0, "Error: Chapter parser failed for '%s', line %d, " - "column %d: ", pdata->file_name, - XML_GetCurrentLineNumber(pdata->parser), - XML_GetCurrentColumnNumber(pdata->parser)); + len = get_arg_len("Error: Chapter parser failed for '%s', line %d, " + "column %d: ", pdata->file_name, + XML_GetCurrentLineNumber(pdata->parser), + XML_GetCurrentColumnNumber(pdata->parser)); va_start(ap, fmt); - len += vsnprintf(NULL, 0, new_fmt.c_str(), ap); + len += get_varg_len(new_fmt.c_str(), ap); new_string = (char *)safemalloc(len + 2); sprintf(new_string, "Error: Chapter parser failed for '%s', line %d, " "column %d: ", pdata->file_name, diff --git a/src/chapters.cpp b/src/chapters.cpp index 3e34f772f..9dee08947 100644 --- a/src/chapters.cpp +++ b/src/chapters.cpp @@ -67,10 +67,10 @@ static void chapter_error(const char *fmt, ...) { char *new_error; int len; - len = snprintf(NULL, 0, "Error: Simple chapter parser: "); + len = strlen("Error: Simple chapter parser: "); va_start(ap, fmt); fix_format(fmt, new_fmt); - len += vsnprintf(NULL, 0, new_fmt.c_str(), ap); + len += get_varg_len(new_fmt.c_str(), ap); new_error = (char *)safemalloc(len + 2); strcpy(new_error, "Error: Simple chapter parser: "); vsprintf(&new_error[strlen(new_error)], new_fmt.c_str(), ap); diff --git a/src/common.cpp b/src/common.cpp index d4a8b62ea..11e0d64cf 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1319,3 +1319,34 @@ void mxexit(int code) { exit(0); } + +int get_arg_len(const char *fmt, ...) { + int size; + va_list ap; + + va_start(ap, fmt); + size = get_varg_len(fmt, ap); + va_end(ap); + + return size; +} + +int get_varg_len(const char *fmt, va_list ap) { + int size, result; + char *dst; + + size = 1024; + dst = (char *)safemalloc(size); + while (1) { + result = vsnprintf(dst, size - 1, fmt, ap); + if (result >= 0) { + safefree(dst); + return result; + } + size += 1024; + dst = (char *)saferealloc(dst, size); + } + safefree(dst); + + return -1; +} diff --git a/src/common.h b/src/common.h index 5003f826f..3485af745 100644 --- a/src/common.h +++ b/src/common.h @@ -142,6 +142,9 @@ bool parse_int(const char *s, int64_t &value); bool parse_int(const char *s, int &value); string to_string(int64_t i); +int get_arg_len(const char *fmt, ...); +int get_varg_len(const char *fmt, va_list ap); + extern int verbose; class bit_cursor_c { diff --git a/src/mkvmerge.cpp b/src/mkvmerge.cpp index 541fd4e4a..73cb84fc2 100644 --- a/src/mkvmerge.cpp +++ b/src/mkvmerge.cpp @@ -1689,12 +1689,6 @@ static void handle_args(int argc, char **argv) { // {{{ global setup and cleanup functions static void setup() { - if ((snprintf(NULL, 0, "hallo") != 5) || - (vsnprintf(NULL, 0, "hallo", NULL) != 5)) - mxerror("Your system does not return proper values for the snprintf() " - "and vsnprintf functions. mkvmerge cannot run securely on this " - "system.\n"); - #if ! defined(COMP_MSC) if (setlocale(LC_CTYPE, "") == NULL) mxerror("Could not set the locale properly. Check the "