Fixed crash when using rs_sprintf/bd_sprintf or rs_sprintf_append/bd_sprintf_append with empty result (maybe only on Windows).

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5349 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2012-07-29 13:28:45 +00:00
parent 6a24bdc62c
commit 2367653146
2 changed files with 39 additions and 15 deletions

View File

@ -41,7 +41,7 @@ static int vasprintf(char **sptr, const char *fmt, va_list argv)
return wanted;
}
//int asprintf(char **sptr, const char *fmt, ...)
//static int asprintf(char **sptr, const char *fmt, ...)
//{
// int retval;
// va_list argv;
@ -54,15 +54,23 @@ static int vasprintf(char **sptr, const char *fmt, va_list argv)
int bd_sprintf(std::string &str, const char *fmt, ...)
{
char *buffer;
char *buffer = NULL;
va_list ap;
va_start(ap, fmt);
int retval = vasprintf(&buffer, fmt, ap);
va_end(ap);
str = buffer;
free(buffer);
if (retval >= 0) {
if (buffer) {
str = buffer;
free(buffer);
} else {
str.clear();
}
} else {
str.clear();
}
return retval;
}
@ -70,14 +78,18 @@ int bd_sprintf(std::string &str, const char *fmt, ...)
int bd_sprintf_append(std::string &str, const char *fmt, ...)
{
va_list ap;
char *ret;
char *buffer = NULL;
va_start(ap, fmt);
int retval = vasprintf(&ret, fmt, ap);
int retval = vasprintf(&buffer, fmt, ap);
va_end(ap);
str.append(ret);
free(ret);
if (retval >= 0) {
if (buffer) {
str.append(buffer);
free(buffer);
}
}
return retval;
}

View File

@ -230,15 +230,23 @@ static int vasprintf(char **sptr, const char *fmt, va_list argv)
int rs_sprintf(std::string &str, const char *fmt, ...)
{
char *buffer;
char *buffer = NULL;
va_list ap;
va_start(ap, fmt);
int retval = vasprintf(&buffer, fmt, ap);
va_end(ap);
str = buffer;
free(buffer);
if (retval >= 0) {
if (buffer) {
str = buffer;
free(buffer);
} else {
str.clear();
}
} else {
str.clear();
}
return retval;
}
@ -246,14 +254,18 @@ int rs_sprintf(std::string &str, const char *fmt, ...)
int rs_sprintf_append(std::string &str, const char *fmt, ...)
{
va_list ap;
char *ret;
char *buffer = NULL;
va_start(ap, fmt);
int retval = vasprintf(&ret, fmt, ap);
int retval = vasprintf(&buffer, fmt, ap);
va_end(ap);
str.append(ret);
free(ret);
if (retval >= 0) {
if (buffer) {
str.append(buffer);
free(buffer);
}
}
return retval;
}