Chromium Code Reviews| 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> | |
| 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 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| 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>", |
| 428 static_cast<long long>(std::numeric_limits<intptr_t>::min()), | 429 static_cast<long long>(std::numeric_limits<intptr_t>::min()), |
| 429 (long long)PrintLongString); | 430 (intptr_t)PrintLongString); |
|
Inactive
2014/06/04 13:25:32
uintptr_t ? (http://www.nongnu.org/avr-libc/user-m
Yang Gu
2014/06/05 01:35:37
Thanks to point this out. uintptr_t is more meanin
| |
| 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 |