1
0
forked from 0ad/0ad

Fix scanf buffer size

This was SVN commit r7242.
This commit is contained in:
Ykkrosh 2010-01-04 19:15:24 +00:00
parent b0dcf49893
commit 33ab686128
2 changed files with 10 additions and 2 deletions

View File

@ -55,4 +55,12 @@ public:
TS_ASSERT_WSTR_EQUALS(t4.Pathname().string(), L"example two.txt");
TS_ASSERT_EQUALS(t4.Size(), (size_t)16777216);
}
void test_maxpath()
{
std::wstring path1 = std::wstring(PATH_MAX, L'x');
std::wstring buf1 = L"0: L \"" + path1 + L"\" 0\n";
TraceEntry t1(buf1);
TS_ASSERT_WSTR_EQUALS(t1.Pathname().string(), path1);
}
};

View File

@ -49,14 +49,14 @@ TraceEntry::TraceEntry(EAction action, const fs::wpath& pathname, size_t size)
TraceEntry::TraceEntry(const std::wstring& text)
{
wchar_t pathname[PATH_MAX] = L"";
wchar_t pathname[PATH_MAX+1] = L""; // includes space for terminator
wchar_t action;
#if EMULATE_SECURE_CRT
#define TRACE_FORMAT L"%f: %lc \"%" STRINGIZE(PATH_MAX) "l[^\"]\" %zd\n" /* use a macro to allow compile-time type-checking */
const int fieldsRead = swscanf(text.c_str(), TRACE_FORMAT, &m_timestamp, &action, pathname, &m_size);
#else
#define TRACE_FORMAT L"%f: %lc \"%l[^\"]\" %d\n"
const int fieldsRead = swscanf_s(text.c_str(), TRACE_FORMAT, &m_timestamp, &action, 1, pathname, PATH_MAX, &m_size);
const int fieldsRead = swscanf_s(text.c_str(), TRACE_FORMAT, &m_timestamp, &action, 1, pathname, ARRAY_SIZE(pathname), &m_size);
#endif
debug_assert(fieldsRead == 4);
debug_assert(action == 'L' || action == 'S');