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 <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 break; | 409 break; |
410 } | 410 } |
411 } | 411 } |
412 | 412 |
413 // All trailing bytes are unchanged. | 413 // All trailing bytes are unchanged. |
414 for (size_t i = len+1; i < sz+2; ++i) | 414 for (size_t i = len+1; i < sz+2; ++i) |
415 EXPECT_EQ('X', tmp[i]); | 415 EXPECT_EQ('X', tmp[i]); |
416 | 416 |
417 // The text that was generated by SafeSPrintf() should always match the | 417 // The text that was generated by SafeSPrintf() should always match the |
418 // equivalent text generated by sprintf(). Please note that the format | 418 // equivalent text generated by sprintf(). Please note that the format |
419 // string for sprintf() is nor complicated, as it does not have the | 419 // string for sprintf() is nor complicated, as it does not have the |
Mark Mentovai
2014/06/16 16:38:46
Please fix this comment as requested: nor → not.
| |
420 // benefit of getting type information from the C++ compiler. | 420 // benefit of getting type information from the C++ compiler. |
421 // | 421 // |
422 // N.B.: It would be so much cleaner to use snprintf(). But unfortunately, | 422 // 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 | 423 // Visual Studio doesn't support this function, and the work-arounds |
424 // are all really awkward. | 424 // are all really awkward. |
425 char ref[256]; | 425 char ref[256]; |
426 CHECK_LE(sz, sizeof(ref)); | 426 CHECK_LE(sz, sizeof(ref)); |
427 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%llX <NULL>", | 427 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%llX <NULL>", |
428 static_cast<long long>(std::numeric_limits<intptr_t>::min()), | 428 static_cast<long long>(std::numeric_limits<intptr_t>::min()), |
Mark Mentovai
2014/06/16 16:38:46
When I mentioned using PRIdPTR, I was talking abou
| |
429 (long long)PrintLongString); | 429 (unsigned long long)(uintptr_t)PrintLongString); |
Mark Mentovai
2014/06/16 16:38:46
1. What’s with the double cast now?
2. Don’t use
| |
430 ref[sz-1] = '\000'; | 430 ref[sz-1] = '\000'; |
431 | 431 |
432 #if defined(NDEBUG) | 432 #if defined(NDEBUG) |
433 const size_t kSSizeMax = std::numeric_limits<ssize_t>::max(); | 433 const size_t kSSizeMax = std::numeric_limits<ssize_t>::max(); |
434 #else | 434 #else |
435 const size_t kSSizeMax = internal::GetSafeSPrintfSSizeMaxForTest(); | 435 const size_t kSSizeMax = internal::GetSafeSPrintfSSizeMaxForTest(); |
436 #endif | 436 #endif |
437 | 437 |
438 // Compare the output from SafeSPrintf() to the one from sprintf(). | 438 // Compare the output from SafeSPrintf() to the one from sprintf(). |
439 EXPECT_EQ(std::string(ref).substr(0, kSSizeMax-1), std::string(tmp.get())); | 439 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; | 742 void *ptr = str; |
743 char buf[40]; | 743 char buf[40]; |
744 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); | 744 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); |
745 EXPECT_EQ("0x80000000", std::string(buf)); | 745 EXPECT_EQ("0x80000000", std::string(buf)); |
746 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); | 746 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); |
747 EXPECT_EQ("0x80000000", std::string(buf)); | 747 EXPECT_EQ("0x80000000", std::string(buf)); |
748 } | 748 } |
749 | 749 |
750 } // namespace strings | 750 } // namespace strings |
751 } // namespace base | 751 } // namespace base |
OLD | NEW |