From 0ef5f357bec75782ba8e2b592957169daa2a7635 Mon Sep 17 00:00:00 2001 From: janwas Date: Sat, 8 Aug 2009 11:13:05 +0000 Subject: [PATCH] fix behavior of *sprintf_s add philip's test_printf also add note to archive_builder This was SVN commit r7089. --- source/lib/file/archive/archive_builder.cpp | 6 +++++ source/lib/secure_crt.cpp | 14 ++++++++++- source/lib/tests/test_secure_crt.h | 27 ++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/source/lib/file/archive/archive_builder.cpp b/source/lib/file/archive/archive_builder.cpp index 3617514949..465c967d56 100644 --- a/source/lib/file/archive/archive_builder.cpp +++ b/source/lib/file/archive/archive_builder.cpp @@ -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 = { diff --git a/source/lib/secure_crt.cpp b/source/lib/secure_crt.cpp index 279b27256d..04c13b8866 100644 --- a/source/lib/secure_crt.cpp +++ b/source/lib/secure_crt.cpp @@ -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') } diff --git a/source/lib/tests/test_secure_crt.h b/source/lib/tests/test_secure_crt.h index ce3e740971..6e9efe57c8 100644 --- a/source/lib/tests/test_secure_crt.h +++ b/source/lib/tests/test_secure_crt.h @@ -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 + } };