Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: base/strings/safe_sprintf.h

Issue 102993006: Remove C++11 specific version of SafeSNPrintf() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 // This ensures that the caller can always add one to the signed return code 97 // This ensures that the caller can always add one to the signed return code
98 // in order to determine the amount of storage that needs to be allocated. 98 // in order to determine the amount of storage that needs to be allocated.
99 // 99 //
100 // While the code supports type checking and while it is generally very careful 100 // While the code supports type checking and while it is generally very careful
101 // to avoid printing incorrect values, it tends to be conservative in printing 101 // to avoid printing incorrect values, it tends to be conservative in printing
102 // as much as possible, even when given incorrect parameters. Typically, in 102 // as much as possible, even when given incorrect parameters. Typically, in
103 // case of an error, the format string will not be expanded. (i.e. something 103 // case of an error, the format string will not be expanded. (i.e. something
104 // like SafeSPrintf(buf, "%p %d", 1, 2) results in "%p 2"). See above for 104 // like SafeSPrintf(buf, "%p %d", 1, 2) results in "%p 2"). See above for
105 // the use of RAW_CHECK() in debug builds, though. 105 // the use of RAW_CHECK() in debug builds, though.
106 // 106 //
107 // The pre-C++11 version cannot handle more than ten arguments. 107 // The pre-C++11 version cannot handle more than ten arguments.
Nico 2013/12/05 21:32:17 remove this line too
Inactive 2013/12/05 21:36:39 Done.
108 // 108 //
109 // Basic example: 109 // Basic example:
110 // char buf[20]; 110 // char buf[20];
111 // base::strings::SafeSPrintf(buf, "The answer: %2d", 42); 111 // base::strings::SafeSPrintf(buf, "The answer: %2d", 42);
112 // 112 //
113 // Example with dynamically sized buffer (async-signal-safe). This code won't 113 // Example with dynamically sized buffer (async-signal-safe). This code won't
114 // work on Visual studio, as it requires dynamically allocating arrays on the 114 // work on Visual studio, as it requires dynamically allocating arrays on the
115 // stack. Consider picking a smaller value for |kMaxSize| if stack size is 115 // stack. Consider picking a smaller value for |kMaxSize| if stack size is
116 // limited and known. On the other hand, if the parameters to SafeSNPrintf() 116 // limited and known. On the other hand, if the parameters to SafeSNPrintf()
117 // are trusted and not controllable by the user, you can consider eliminating 117 // are trusted and not controllable by the user, you can consider eliminating
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
194
195 template<typename... Args>
196 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
198 // array to make it easier to iterate over them.
199 const internal::Arg arg_array[] = { args... };
200 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
201 }
202
203 template<size_t N, typename... Args>
204 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
206 // array to make it easier to iterate over them.
207 const internal::Arg arg_array[] = { args... };
208 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
209 }
210
211 #else // Pre-C++11
212
213 // TODO(markus): C++11 has a much more concise and readable solution for 193 // TODO(markus): C++11 has a much more concise and readable solution for
214 // expressing what we are doing here. Delete the fall-back code for older 194 // 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. 195 // compilers as soon as we have fully switched to C++11.
Inactive 2013/12/05 21:36:39 I also removed the last part of this comment.
216 196
217 template<class T0, class T1, class T2, class T3, class T4, 197 template<class T0, class T1, class T2, class T3, class T4,
218 class T5, class T6, class T7, class T8, class T9> 198 class T5, class T6, class T7, class T8, class T9>
219 ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, 199 ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
220 T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, 200 T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
221 T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { 201 T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) {
222 // Use Arg() object to record type information and then copy arguments to an 202 // Use Arg() object to record type information and then copy arguments to an
223 // array to make it easier to iterate over them. 203 // array to make it easier to iterate over them.
224 const internal::Arg arg_array[] = { 204 const internal::Arg arg_array[] = {
225 arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 205 arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array)); 399 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
420 } 400 }
421 401
422 template<size_t N, class T0> 402 template<size_t N, class T0>
423 ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0) { 403 ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0) {
424 // Use Arg() object to record type information and then copy arguments to an 404 // Use Arg() object to record type information and then copy arguments to an
425 // array to make it easier to iterate over them. 405 // array to make it easier to iterate over them.
426 const internal::Arg arg_array[] = { arg0 }; 406 const internal::Arg arg_array[] = { arg0 };
427 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array)); 407 return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
428 } 408 }
429 #endif
430 409
431 // Fast-path when we don't actually need to substitute any arguments. 410 // Fast-path when we don't actually need to substitute any arguments.
432 BASE_EXPORT ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt); 411 BASE_EXPORT ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt);
433 template<size_t N> 412 template<size_t N>
434 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) { 413 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) {
435 return SafeSNPrintf(buf, N, fmt); 414 return SafeSNPrintf(buf, N, fmt);
436 } 415 }
437 416
438 } // namespace strings 417 } // namespace strings
439 } // namespace base 418 } // namespace base
440 419
441 #endif // BASE_STRINGS_SAFE_SPRINTF_H_ 420 #endif // BASE_STRINGS_SAFE_SPRINTF_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698