fix behavior of *sprintf_s

add philip's test_printf
also add note to archive_builder

This was SVN commit r7089.
This commit is contained in:
janwas 2009-08-08 11:13:05 +00:00
parent 10cc678ffb
commit 0ef5f357be
3 changed files with 45 additions and 2 deletions

View File

@ -928,6 +928,12 @@ extern LibError trace_run(const char* trace_filename);
// run first, to give more weight to it (TSP code should go with first entry
// when #occurrences are equal)
#error "reimplement me"
// update: file enumeration really isn't hard, and splitting separate
// traces into files would greatly simplify deleting old traces after
// awhile and avoid indexing problems due to counting delimiter lines
// (#167). this section should be rewritten.
static const TraceEntry delimiter_entry =
{

View File

@ -220,7 +220,19 @@ int tcat_s(tchar* dst, size_t max_dst_chars, const tchar* src)
int tvsprintf_s(tchar* dst, size_t max_dst_chars, const tchar* fmt, va_list ap)
{
return tvsnprintf(dst, max_dst_chars, fmt, ap);
if(!dst || !fmt || max_dst_chars == 0)
{
errno = EINVAL;
return -1;
}
const int ret = tvsnprintf(dst, max_dst_chars, fmt, ap);
if(ret >= int(max_dst_chars)) // not enough space
{
dst[0] = '\0';
return -1;
}
return ret; // negative if error, else # chars written (excluding '\0')
}

View File

@ -48,7 +48,7 @@ class TestString_s : public CxxTest::TestSuite
static void TEST_LEN(const char* string, size_t limit,
size_t expected_len)
{
TS_ASSERT_EQUALS(strnlen((string), (limit)), (expected_len));
TS_ASSERT_EQUALS(int(strnlen((string), int(limit))), int(expected_len));
}
static void TEST_CPY(char* dst, size_t dst_max, const char* src,
@ -242,4 +242,29 @@ public:
TEST_NCAT(d5,5, s5,2, "12",0,"12ab");
TEST_NCAT(d6,6, s5,10, "",0,"abcde");
}
static void TEST_PRINTF(char* dst, size_t max_dst_chars, const char* dst_val,
int expected_ret, const char* expected_dst, const char* fmt, ...)
{
strcpy(dst, dst_val);
va_list ap;
va_start(ap, fmt);
int ret = vsprintf_s(dst, max_dst_chars, fmt, ap);
va_end(ap);
TS_ASSERT_EQUALS(ret, expected_ret);
TS_ASSERT_STR_EQUALS(dst, expected_dst);
}
void test_printf()
{
TEST_PRINTF(d10,10, s10, 4, "1234", "%d", 1234);
TEST_PRINTF(d10,5, s10, 4, "1234", "%d", 1234);
// VC's *printf raises an error dialog if the buffer is too
// small, so only test our implementation.
#if EMULATE_SECURE_CRT
TEST_PRINTF(d10,4, s10, 0, "", "%d", 1234);
TEST_PRINTF(d10,3, s10, 0, "", "%d", 1234);
#endif
}
};