OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/safe_sprintf.h" | 5 #include "base/strings/safe_sprintf.h" |
6 | 6 |
7 #include <inttypes.h> | |
Mark Mentovai
2014/06/15 12:27:26
Does MSVC even have this header?
Yang Gu
2014/06/16 10:39:09
Not quite sure about MSVC. I searched the web and
| |
7 #include <stdio.h> | 8 #include <stdio.h> |
8 #include <string.h> | 9 #include <string.h> |
9 | 10 |
10 #include <limits> | 11 #include <limits> |
11 | 12 |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 // Death tests on Android are currently very flaky. No need to add more flaky | 17 // Death tests on Android are currently very flaky. No need to add more flaky |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 break; | 410 break; |
410 } | 411 } |
411 } | 412 } |
412 | 413 |
413 // All trailing bytes are unchanged. | 414 // All trailing bytes are unchanged. |
414 for (size_t i = len+1; i < sz+2; ++i) | 415 for (size_t i = len+1; i < sz+2; ++i) |
415 EXPECT_EQ('X', tmp[i]); | 416 EXPECT_EQ('X', tmp[i]); |
416 | 417 |
417 // The text that was generated by SafeSPrintf() should always match the | 418 // The text that was generated by SafeSPrintf() should always match the |
418 // equivalent text generated by sprintf(). Please note that the format | 419 // equivalent text generated by sprintf(). Please note that the format |
419 // string for sprintf() is nor complicated, as it does not have the | 420 // string for sprintf() is nor complicated, as it does not have the |
Mark Mentovai
2014/06/15 12:19:22
This should say “not”, not “nor”.
| |
420 // benefit of getting type information from the C++ compiler. | 421 // benefit of getting type information from the C++ compiler. |
421 // | 422 // |
422 // N.B.: It would be so much cleaner to use snprintf(). But unfortunately, | 423 // N.B.: It would be so much cleaner to use snprintf(). But unfortunately, |
423 // Visual Studio doesn't support this function, and the work-arounds | 424 // Visual Studio doesn't support this function, and the work-arounds |
424 // are all really awkward. | 425 // are all really awkward. |
425 char ref[256]; | 426 char ref[256]; |
426 CHECK_LE(sz, sizeof(ref)); | 427 CHECK_LE(sz, sizeof(ref)); |
427 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%llX <NULL>", | 428 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%" PRIXPTR " <NULL>", |
Mark Mentovai
2014/06/15 12:19:21
Why can’t you use %p here?
Yang Gu
2014/06/16 10:39:09
The actual format is in SafeSNPrintf(), where %p i
| |
428 static_cast<long long>(std::numeric_limits<intptr_t>::min()), | 429 static_cast<long long>(std::numeric_limits<intptr_t>::min()), |
Mark Mentovai
2014/06/15 12:19:22
Wouldn’t PRIdPTR (and no cast) be better here and
Mark Mentovai
2014/06/15 12:23:02
Mark Mentovai wrote:
Mark Mentovai
2014/06/15 12:27:26
Mark Mentovai wrote:
Yang Gu
2014/06/16 10:39:09
PRIdPTR is in decimal format, but we actually need
| |
429 (long long)PrintLongString); | 430 (uintptr_t)PrintLongString); |
430 ref[sz-1] = '\000'; | 431 ref[sz-1] = '\000'; |
431 | 432 |
432 #if defined(NDEBUG) | 433 #if defined(NDEBUG) |
433 const size_t kSSizeMax = std::numeric_limits<ssize_t>::max(); | 434 const size_t kSSizeMax = std::numeric_limits<ssize_t>::max(); |
434 #else | 435 #else |
435 const size_t kSSizeMax = internal::GetSafeSPrintfSSizeMaxForTest(); | 436 const size_t kSSizeMax = internal::GetSafeSPrintfSSizeMaxForTest(); |
436 #endif | 437 #endif |
437 | 438 |
438 // Compare the output from SafeSPrintf() to the one from sprintf(). | 439 // Compare the output from SafeSPrintf() to the one from sprintf(). |
439 EXPECT_EQ(std::string(ref).substr(0, kSSizeMax-1), std::string(tmp.get())); | 440 EXPECT_EQ(std::string(ref).substr(0, kSSizeMax-1), std::string(tmp.get())); |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
742 void *ptr = str; | 743 void *ptr = str; |
743 char buf[40]; | 744 char buf[40]; |
744 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); | 745 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); |
745 EXPECT_EQ("0x80000000", std::string(buf)); | 746 EXPECT_EQ("0x80000000", std::string(buf)); |
746 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); | 747 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); |
747 EXPECT_EQ("0x80000000", std::string(buf)); | 748 EXPECT_EQ("0x80000000", std::string(buf)); |
748 } | 749 } |
749 | 750 |
750 } // namespace strings | 751 } // namespace strings |
751 } // namespace base | 752 } // namespace base |
OLD | NEW |