mirror of
https://github.com/introlab/rtabmap.git
synced 2026-10-12 12:59:51 +08:00
Fixing crash on windows when uFormat() is to long
This commit is contained in:
@@ -338,7 +338,8 @@ std::string uFormatv (const char *fmt, va_list args)
|
||||
|
||||
// Try to vsnprintf into our buffer.
|
||||
#ifdef _MSC_VER
|
||||
int needed = vsnprintf_s(buf, size, size, fmt, argsTmp);
|
||||
// _TRUNCATE: return -1 if it doesn't fit (count=size would call the invalid parameter handler and abort)
|
||||
int needed = vsnprintf_s(buf, size, _TRUNCATE, fmt, argsTmp);
|
||||
#else
|
||||
int needed = vsnprintf (buf, size, fmt, argsTmp);
|
||||
#endif
|
||||
|
||||
@@ -201,3 +201,14 @@ TEST(UConversionTest, UFormat)
|
||||
EXPECT_NE(result.find("42"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(UConversionTest, UFormatLongerThanItsFirstBuffer)
|
||||
{
|
||||
// Over the 1024 bytes uFormat first tries: formatted whole, not truncated (MSVC's
|
||||
// checked vsnprintf_s used to abort the process instead).
|
||||
const std::string longText(3000, 'a');
|
||||
std::string result = uFormat("%s %d", longText.c_str(), 42);
|
||||
EXPECT_EQ(result, longText + " 42");
|
||||
result = uFormat("%s", std::string(1023, 'b').c_str());
|
||||
EXPECT_EQ(result, std::string(1023, 'b'));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user