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 #ifndef BASE_STRINGS_SAFE_SPRINTF_H_ | 5 #ifndef BASE_STRINGS_SAFE_SPRINTF_H_ |
6 #define BASE_STRINGS_SAFE_SPRINTF_H_ | 6 #define BASE_STRINGS_SAFE_SPRINTF_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 #if !defined(NDEBUG) | 183 #if !defined(NDEBUG) |
184 // In debug builds, allow unit tests to artificially lower the kSSizeMax | 184 // In debug builds, allow unit tests to artificially lower the kSSizeMax |
185 // constant that is used as a hard upper-bound for all buffers. In normal | 185 // constant that is used as a hard upper-bound for all buffers. In normal |
186 // use, this constant should always be std::numeric_limits<ssize_t>::max(). | 186 // use, this constant should always be std::numeric_limits<ssize_t>::max(). |
187 BASE_EXPORT void SetSafeSPrintfSSizeMaxForTest(size_t max); | 187 BASE_EXPORT void SetSafeSPrintfSSizeMaxForTest(size_t max); |
188 BASE_EXPORT size_t GetSafeSPrintfSSizeMaxForTest(); | 188 BASE_EXPORT size_t GetSafeSPrintfSSizeMaxForTest(); |
189 #endif | 189 #endif |
190 | 190 |
191 } // namespace internal | 191 } // namespace internal |
192 | 192 |
193 #if __cplusplus >= 201103 // C++11 | 193 #if __cplusplus >= 201103 && !defined(COMPILER_GCC) // C++11 |
Nico
2013/12/05 21:11:35
Huh where did that come from? We should probably d
Inactive
2013/12/05 21:15:15
Hmm. This is a bit more readable than the non-C++1
| |
194 | |
195 // The following does not build with gcc yet. | |
194 | 196 |
195 template<typename... Args> | 197 template<typename... Args> |
196 ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, Args... args) { | 198 ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, Args... args) { |
197 // Use Arg() object to record type information and then copy arguments to an | 199 // Use Arg() object to record type information and then copy arguments to an |
198 // array to make it easier to iterate over them. | 200 // array to make it easier to iterate over them. |
199 const internal::Arg arg_array[] = { args... }; | 201 const internal::Arg arg_array[] = { args... }; |
200 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array)); | 202 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array)); |
201 } | 203 } |
202 | 204 |
203 template<size_t N, typename... Args> | 205 template<size_t N, typename... Args> |
204 ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, Args... args) { | 206 ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, Args... args) { |
205 // Use Arg() object to record type information and then copy arguments to an | 207 // Use Arg() object to record type information and then copy arguments to an |
206 // array to make it easier to iterate over them. | 208 // array to make it easier to iterate over them. |
207 const internal::Arg arg_array[] = { args... }; | 209 const internal::Arg arg_array[] = { args... }; |
208 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array)); | 210 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array)); |
209 } | 211 } |
210 | 212 |
211 #else // Pre-C++11 | 213 #else // Pre-C++11 |
212 | 214 |
213 // TODO(markus): C++11 has a much more concise and readable solution for | 215 // TODO(markus): C++11 has a much more concise and readable solution for |
Inactive
2013/12/05 21:15:15
See this comment as well.
| |
214 // expressing what we are doing here. Delete the fall-back code for older | 216 // expressing what we are doing here. Delete the fall-back code for older |
215 // compilers as soon as we have fully switched to C++11. | 217 // compilers as soon as we have fully switched to C++11. |
216 | 218 |
217 template<class T0, class T1, class T2, class T3, class T4, | 219 template<class T0, class T1, class T2, class T3, class T4, |
218 class T5, class T6, class T7, class T8, class T9> | 220 class T5, class T6, class T7, class T8, class T9> |
219 ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, | 221 ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, |
220 T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, | 222 T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, |
221 T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { | 223 T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { |
222 // Use Arg() object to record type information and then copy arguments to an | 224 // Use Arg() object to record type information and then copy arguments to an |
223 // array to make it easier to iterate over them. | 225 // array to make it easier to iterate over them. |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 BASE_EXPORT ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt); | 434 BASE_EXPORT ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt); |
433 template<size_t N> | 435 template<size_t N> |
434 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) { | 436 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) { |
435 return SafeSNPrintf(buf, N, fmt); | 437 return SafeSNPrintf(buf, N, fmt); |
436 } | 438 } |
437 | 439 |
438 } // namespace strings | 440 } // namespace strings |
439 } // namespace base | 441 } // namespace base |
440 | 442 |
441 #endif // BASE_STRINGS_SAFE_SPRINTF_H_ | 443 #endif // BASE_STRINGS_SAFE_SPRINTF_H_ |
OLD | NEW |