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 <limits> | 7 #include <limits> |
8 | 8 |
9 #if !defined(NDEBUG) | 9 #if !defined(NDEBUG) |
10 // In debug builds, we use RAW_CHECK() to print useful error messages, if | 10 // In debug builds, we use RAW_CHECK() to print useful error messages, if |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 public: | 100 public: |
101 // |buffer| is caller-allocated storage that SafeSPrintf() writes to. It | 101 // |buffer| is caller-allocated storage that SafeSPrintf() writes to. It |
102 // has |size| bytes of writable storage. It is the caller's responsibility | 102 // has |size| bytes of writable storage. It is the caller's responsibility |
103 // to ensure that the buffer is at least one byte in size, so that it fits | 103 // to ensure that the buffer is at least one byte in size, so that it fits |
104 // the trailing NUL that will be added by the destructor. The buffer also | 104 // the trailing NUL that will be added by the destructor. The buffer also |
105 // must be smaller or equal to kSSizeMax in size. | 105 // must be smaller or equal to kSSizeMax in size. |
106 Buffer(char* buffer, size_t size) | 106 Buffer(char* buffer, size_t size) |
107 : buffer_(buffer), | 107 : buffer_(buffer), |
108 size_(size - 1), // Account for trailing NUL byte | 108 size_(size - 1), // Account for trailing NUL byte |
109 count_(0) { | 109 count_(0) { |
110 // This test should work on all C++11 compilers, but apparently something is | 110 // The following assertion does not build on Mac and Android. This is because |
111 // not working on all versions of clang just yet (e.g. on Mac, IOS, and | 111 // static_assert only works with compile-time constants, but mac uses |
112 // Android). We are conservative and exclude all of clang for the time being. | 112 // libstdc++4.2 and android uses stlport, which both don't mark |
113 // TODO(markus): Check if this restriction can be lifted. | 113 // numeric_limits::max() as constexp. |
114 #if __cplusplus >= 201103 && !defined(__clang__) | 114 #if __cplusplus >= 201103 && !defined(OS_ANDROID) && !defined(OS_MACOSX) && !def
ined(OS_IOS) |
115 COMPILE_ASSERT(kSSizeMaxConst == std::numeric_limits<ssize_t>::max(), | 115 COMPILE_ASSERT(kSSizeMaxConst == \ |
| 116 static_cast<size_t>(std::numeric_limits<ssize_t>::max()), |
116 kSSizeMax_is_the_max_value_of_an_ssize_t); | 117 kSSizeMax_is_the_max_value_of_an_ssize_t); |
117 #endif | 118 #endif |
118 DEBUG_CHECK(size > 0); | 119 DEBUG_CHECK(size > 0); |
119 DEBUG_CHECK(size <= kSSizeMax); | 120 DEBUG_CHECK(size <= kSSizeMax); |
120 } | 121 } |
121 | 122 |
122 ~Buffer() { | 123 ~Buffer() { |
123 // The code calling the constructor guaranteed that there was enough space | 124 // The code calling the constructor guaranteed that there was enough space |
124 // to store a trailing NUL -- and in debug builds, we are actually | 125 // to store a trailing NUL -- and in debug builds, we are actually |
125 // verifying this with DEBUG_CHECK()s in the constructor. So, we can | 126 // verifying this with DEBUG_CHECK()s in the constructor. So, we can |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 DEBUG_CHECK(src[0] != '%' || src[1] == '%'); | 673 DEBUG_CHECK(src[0] != '%' || src[1] == '%'); |
673 if (src[0] == '%' && src[1] == '%') { | 674 if (src[0] == '%' && src[1] == '%') { |
674 ++src; | 675 ++src; |
675 } | 676 } |
676 } | 677 } |
677 return buffer.GetCount(); | 678 return buffer.GetCount(); |
678 } | 679 } |
679 | 680 |
680 } // namespace strings | 681 } // namespace strings |
681 } // namespace base | 682 } // namespace base |
OLD | NEW |