Skip to content

[GEN][ZH] Demote throw to assert in AsciiString::format_va, UnicodeString::format_va #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions Generals/Code/GameEngine/Source/Common/System/AsciiString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,23 +371,24 @@ void AsciiString::format(const char* format, ...)
// -----------------------------------------------------
void AsciiString::format_va(const AsciiString& format, va_list args)
{
validate();
char buf[MAX_FORMAT_BUF_LEN];
if (_vsnprintf(buf, sizeof(buf)/sizeof(char)-1, format.str(), args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
format_va(format.str(), args);
}

// -----------------------------------------------------
void AsciiString::format_va(const char* format, va_list args)
{
validate();
char buf[MAX_FORMAT_BUF_LEN];
if (_vsnprintf(buf, sizeof(buf)/sizeof(char)-1, format, args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
const int result = _vsnprintf(buf, sizeof(buf)/sizeof(char)-1, format, args);
if (result >= 0)
{
set(buf);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we set buf[MAX_FORMAT_BUF_LEN-1] = 0; while we are at it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding _vsnprintf & _snprintf, I planned to do another change.

validate();
}
else
{
DEBUG_ASSERTCRASH(false, ("AsciiString::format_va failed with code:%d format:\"%s\"", result, format));
}
}

// -----------------------------------------------------
Expand Down
21 changes: 11 additions & 10 deletions Generals/Code/GameEngine/Source/Common/System/UnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,24 @@ void UnicodeString::format(const WideChar* format, ...)
// -----------------------------------------------------
void UnicodeString::format_va(const UnicodeString& format, va_list args)
{
validate();
WideChar buf[MAX_FORMAT_BUF_LEN];
if (_vsnwprintf(buf, sizeof(buf)/sizeof(WideChar)-1, format.str(), args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
format_va(format.str(), args);
}

// -----------------------------------------------------
void UnicodeString::format_va(const WideChar* format, va_list args)
{
validate();
WideChar buf[MAX_FORMAT_BUF_LEN];
if (_vsnwprintf(buf, sizeof(buf)/sizeof(WideChar)-1, format, args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
const int result = _vsnwprintf(buf, sizeof(buf)/sizeof(WideChar)-1, format, args);
if (result >= 0)
{
set(buf);
validate();
}
else
{
DEBUG_ASSERTCRASH(false, ("UnicodeString::format_va failed with code:%d", result));
}
}

//-----------------------------------------------------------------------------
Expand Down
21 changes: 11 additions & 10 deletions GeneralsMD/Code/GameEngine/Source/Common/System/AsciiString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,24 @@ void AsciiString::format(const char* format, ...)
// -----------------------------------------------------
void AsciiString::format_va(const AsciiString& format, va_list args)
{
validate();
char buf[MAX_FORMAT_BUF_LEN];
if (_vsnprintf(buf, sizeof(buf)/sizeof(char)-1, format.str(), args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
format_va(format.str(), args);
}

// -----------------------------------------------------
void AsciiString::format_va(const char* format, va_list args)
{
validate();
char buf[MAX_FORMAT_BUF_LEN];
if (_vsnprintf(buf, sizeof(buf)/sizeof(char)-1, format, args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
const int result = _vsnprintf(buf, sizeof(buf)/sizeof(char)-1, format, args);
if (result >= 0)
{
set(buf);
validate();
}
else
{
DEBUG_ASSERTCRASH(false, ("AsciiString::format_va failed with code:%d format:\"%s\"", result, format));
}
}

// -----------------------------------------------------
Expand Down
21 changes: 11 additions & 10 deletions GeneralsMD/Code/GameEngine/Source/Common/System/UnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,24 @@ void UnicodeString::format(const WideChar* format, ...)
// -----------------------------------------------------
void UnicodeString::format_va(const UnicodeString& format, va_list args)
{
validate();
WideChar buf[MAX_FORMAT_BUF_LEN];
if (_vsnwprintf(buf, sizeof(buf)/sizeof(WideChar)-1, format.str(), args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
format_va(format.str(), args);
}

// -----------------------------------------------------
void UnicodeString::format_va(const WideChar* format, va_list args)
{
validate();
WideChar buf[MAX_FORMAT_BUF_LEN];
if (_vsnwprintf(buf, sizeof(buf)/sizeof(WideChar)-1, format, args) < 0)
throw ERROR_OUT_OF_MEMORY;
set(buf);
validate();
const int result = _vsnwprintf(buf, sizeof(buf)/sizeof(WideChar)-1, format, args);
if (result >= 0)
{
set(buf);
validate();
}
else
{
DEBUG_ASSERTCRASH(false, ("UnicodeString::format_va failed with code:%d", result));
}
}

//-----------------------------------------------------------------------------
Expand Down
Loading