Implemented a more compatible method of getting the required buffer size for a sprintf/vsprintf.

This commit is contained in:
Moritz Bunkus 2003-09-24 19:10:16 +00:00
parent 0768ed24c2
commit 28383cf083
5 changed files with 41 additions and 13 deletions

View File

@ -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,

View File

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

View File

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

View File

@ -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 {

View File

@ -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 "