Use "= delete" for DISALLOW_COPY and DISALLOW_ASSIGN.
[chromium-blink-merge.git] / base / strings / safe_sprintf.cc
blobb1fcf45b24f7a19f76eb6a175abc82467daa41ea
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
3 // found in the LICENSE file.
5 #include "base/strings/safe_sprintf.h"
7 #include <limits>
9 #if !defined(NDEBUG)
10 // In debug builds, we use RAW_CHECK() to print useful error messages, if
11 // SafeSPrintf() is called with broken arguments.
12 // As our contract promises that SafeSPrintf() can be called from any
13 // restricted run-time context, it is not actually safe to call logging
14 // functions from it; and we only ever do so for debug builds and hope for the
15 // best. We should _never_ call any logging function other than RAW_CHECK(),
16 // and we should _never_ include any logging code that is active in production
17 // builds. Most notably, we should not include these logging functions in
18 // unofficial release builds, even though those builds would otherwise have
19 // DCHECKS() enabled.
20 // In other words; please do not remove the #ifdef around this #include.
21 // Instead, in production builds we opt for returning a degraded result,
22 // whenever an error is encountered.
23 // E.g. The broken function call
24 // SafeSPrintf("errno = %d (%x)", errno, strerror(errno))
25 // will print something like
26 // errno = 13, (%x)
27 // instead of
28 // errno = 13 (Access denied)
29 // In most of the anticipated use cases, that's probably the preferred
30 // behavior.
31 #include "base/logging.h"
32 #define DEBUG_CHECK RAW_CHECK
33 #else
34 #define DEBUG_CHECK(x) do { if (x) { } } while (0)
35 #endif
37 namespace base {
38 namespace strings {
40 // The code in this file is extremely careful to be async-signal-safe.
42 // Most obviously, we avoid calling any code that could dynamically allocate
43 // memory. Doing so would almost certainly result in bugs and dead-locks.
44 // We also avoid calling any other STL functions that could have unintended
45 // side-effects involving memory allocation or access to other shared
46 // resources.
48 // But on top of that, we also avoid calling other library functions, as many
49 // of them have the side-effect of calling getenv() (in order to deal with
50 // localization) or accessing errno. The latter sounds benign, but there are
51 // several execution contexts where it isn't even possible to safely read let
52 // alone write errno.
54 // The stated design goal of the SafeSPrintf() function is that it can be
55 // called from any context that can safely call C or C++ code (i.e. anything
56 // that doesn't require assembly code).
58 // For a brief overview of some but not all of the issues with async-signal-
59 // safety, refer to:
60 // http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html
62 namespace {
63 const size_t kSSizeMaxConst = ((size_t)(ssize_t)-1) >> 1;
65 const char kUpCaseHexDigits[] = "0123456789ABCDEF";
66 const char kDownCaseHexDigits[] = "0123456789abcdef";
69 #if defined(NDEBUG)
70 // We would like to define kSSizeMax as std::numeric_limits<ssize_t>::max(),
71 // but C++ doesn't allow us to do that for constants. Instead, we have to
72 // use careful casting and shifting. We later use a COMPILE_ASSERT to
73 // verify that this worked correctly.
74 namespace {
75 const size_t kSSizeMax = kSSizeMaxConst;
77 #else // defined(NDEBUG)
78 // For efficiency, we really need kSSizeMax to be a constant. But for unit
79 // tests, it should be adjustable. This allows us to verify edge cases without
80 // having to fill the entire available address space. As a compromise, we make
81 // kSSizeMax adjustable in debug builds, and then only compile that particular
82 // part of the unit test in debug builds.
83 namespace {
84 static size_t kSSizeMax = kSSizeMaxConst;
87 namespace internal {
88 void SetSafeSPrintfSSizeMaxForTest(size_t max) {
89 kSSizeMax = max;
92 size_t GetSafeSPrintfSSizeMaxForTest() {
93 return kSSizeMax;
96 #endif // defined(NDEBUG)
98 namespace {
99 class Buffer {
100 public:
101 // |buffer| is caller-allocated storage that SafeSPrintf() writes to. It
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
104 // the trailing NUL that will be added by the destructor. The buffer also
105 // must be smaller or equal to kSSizeMax in size.
106 Buffer(char* buffer, size_t size)
107 : buffer_(buffer),
108 size_(size - 1), // Account for trailing NUL byte
109 count_(0) {
110 // The following assertion does not build on Mac and Android. This is because
111 // static_assert only works with compile-time constants, but mac uses
112 // libstdc++4.2 and android uses stlport, which both don't mark
113 // numeric_limits::max() as constexp. Likewise, MSVS2013's standard library
114 // also doesn't mark max() as constexpr yet. cl.exe supports static_cast but
115 // doesn't really implement constexpr yet so it doesn't complain, but clang
116 // does.
117 #if __cplusplus >= 201103 && !defined(OS_ANDROID) && !defined(OS_MACOSX) && \
118 !defined(OS_IOS) && !(defined(__clang__) && defined(OS_WIN))
119 COMPILE_ASSERT(kSSizeMaxConst == \
120 static_cast<size_t>(std::numeric_limits<ssize_t>::max()),
121 kSSizeMax_is_the_max_value_of_an_ssize_t);
122 #endif
123 DEBUG_CHECK(size > 0);
124 DEBUG_CHECK(size <= kSSizeMax);
127 ~Buffer() {
128 // The code calling the constructor guaranteed that there was enough space
129 // to store a trailing NUL -- and in debug builds, we are actually
130 // verifying this with DEBUG_CHECK()s in the constructor. So, we can
131 // always unconditionally write the NUL byte in the destructor. We do not
132 // need to adjust the count_, as SafeSPrintf() copies snprintf() in not
133 // including the NUL byte in its return code.
134 *GetInsertionPoint() = '\000';
137 // Returns true, iff the buffer is filled all the way to |kSSizeMax-1|. The
138 // caller can now stop adding more data, as GetCount() has reached its
139 // maximum possible value.
140 inline bool OutOfAddressableSpace() const {
141 return count_ == static_cast<size_t>(kSSizeMax - 1);
144 // Returns the number of bytes that would have been emitted to |buffer_|
145 // if it was sized sufficiently large. This number can be larger than
146 // |size_|, if the caller provided an insufficiently large output buffer.
147 // But it will never be bigger than |kSSizeMax-1|.
148 inline ssize_t GetCount() const {
149 DEBUG_CHECK(count_ < kSSizeMax);
150 return static_cast<ssize_t>(count_);
153 // Emits one |ch| character into the |buffer_| and updates the |count_| of
154 // characters that are currently supposed to be in the buffer.
155 // Returns "false", iff the buffer was already full.
156 // N.B. |count_| increases even if no characters have been written. This is
157 // needed so that GetCount() can return the number of bytes that should
158 // have been allocated for the |buffer_|.
159 inline bool Out(char ch) {
160 if (size_ >= 1 && count_ < size_) {
161 buffer_[count_] = ch;
162 return IncrementCountByOne();
164 // |count_| still needs to be updated, even if the buffer has been
165 // filled completely. This allows SafeSPrintf() to return the number of
166 // bytes that should have been emitted.
167 IncrementCountByOne();
168 return false;
171 // Inserts |padding|-|len| bytes worth of padding into the |buffer_|.
172 // |count_| will also be incremented by the number of bytes that were meant
173 // to be emitted. The |pad| character is typically either a ' ' space
174 // or a '0' zero, but other non-NUL values are legal.
175 // Returns "false", iff the the |buffer_| filled up (i.e. |count_|
176 // overflowed |size_|) at any time during padding.
177 inline bool Pad(char pad, size_t padding, size_t len) {
178 DEBUG_CHECK(pad);
179 DEBUG_CHECK(padding <= kSSizeMax);
180 for (; padding > len; --padding) {
181 if (!Out(pad)) {
182 if (--padding) {
183 IncrementCount(padding-len);
185 return false;
188 return true;
191 // POSIX doesn't define any async-signal-safe function for converting
192 // an integer to ASCII. Define our own version.
194 // This also gives us the ability to make the function a little more
195 // powerful and have it deal with |padding|, with truncation, and with
196 // predicting the length of the untruncated output.
198 // IToASCII() converts an integer |i| to ASCII.
200 // Unlike similar functions in the standard C library, it never appends a
201 // NUL character. This is left for the caller to do.
203 // While the function signature takes a signed int64_t, the code decides at
204 // run-time whether to treat the argument as signed (int64_t) or as unsigned
205 // (uint64_t) based on the value of |sign|.
207 // It supports |base|s 2 through 16. Only a |base| of 10 is allowed to have
208 // a |sign|. Otherwise, |i| is treated as unsigned.
210 // For bases larger than 10, |upcase| decides whether lower-case or upper-
211 // case letters should be used to designate digits greater than 10.
213 // Padding can be done with either '0' zeros or ' ' spaces. Padding has to
214 // be positive and will always be applied to the left of the output.
216 // Prepends a |prefix| to the number (e.g. "0x"). This prefix goes to
217 // the left of |padding|, if |pad| is '0'; and to the right of |padding|
218 // if |pad| is ' '.
220 // Returns "false", if the |buffer_| overflowed at any time.
221 bool IToASCII(bool sign, bool upcase, int64_t i, int base,
222 char pad, size_t padding, const char* prefix);
224 private:
225 // Increments |count_| by |inc| unless this would cause |count_| to
226 // overflow |kSSizeMax-1|. Returns "false", iff an overflow was detected;
227 // it then clamps |count_| to |kSSizeMax-1|.
228 inline bool IncrementCount(size_t inc) {
229 // "inc" is either 1 or a "padding" value. Padding is clamped at
230 // run-time to at most kSSizeMax-1. So, we know that "inc" is always in
231 // the range 1..kSSizeMax-1.
232 // This allows us to compute "kSSizeMax - 1 - inc" without incurring any
233 // integer overflows.
234 DEBUG_CHECK(inc <= kSSizeMax - 1);
235 if (count_ > kSSizeMax - 1 - inc) {
236 count_ = kSSizeMax - 1;
237 return false;
238 } else {
239 count_ += inc;
240 return true;
244 // Convenience method for the common case of incrementing |count_| by one.
245 inline bool IncrementCountByOne() {
246 return IncrementCount(1);
249 // Return the current insertion point into the buffer. This is typically
250 // at |buffer_| + |count_|, but could be before that if truncation
251 // happened. It always points to one byte past the last byte that was
252 // successfully placed into the |buffer_|.
253 inline char* GetInsertionPoint() const {
254 size_t idx = count_;
255 if (idx > size_) {
256 idx = size_;
258 return buffer_ + idx;
261 // User-provided buffer that will receive the fully formatted output string.
262 char* buffer_;
264 // Number of bytes that are available in the buffer excluding the trailing
265 // NUL byte that will be added by the destructor.
266 const size_t size_;
268 // Number of bytes that would have been emitted to the buffer, if the buffer
269 // was sufficiently big. This number always excludes the trailing NUL byte
270 // and it is guaranteed to never grow bigger than kSSizeMax-1.
271 size_t count_;
273 DISALLOW_COPY_AND_ASSIGN(Buffer);
277 bool Buffer::IToASCII(bool sign, bool upcase, int64_t i, int base,
278 char pad, size_t padding, const char* prefix) {
279 // Sanity check for parameters. None of these should ever fail, but see
280 // above for the rationale why we can't call CHECK().
281 DEBUG_CHECK(base >= 2);
282 DEBUG_CHECK(base <= 16);
283 DEBUG_CHECK(!sign || base == 10);
284 DEBUG_CHECK(pad == '0' || pad == ' ');
285 DEBUG_CHECK(padding <= kSSizeMax);
286 DEBUG_CHECK(!(sign && prefix && *prefix));
288 // Handle negative numbers, if the caller indicated that |i| should be
289 // treated as a signed number; otherwise treat |i| as unsigned (even if the
290 // MSB is set!)
291 // Details are tricky, because of limited data-types, but equivalent pseudo-
292 // code would look like:
293 // if (sign && i < 0)
294 // prefix = "-";
295 // num = abs(i);
296 int minint = 0;
297 uint64_t num;
298 if (sign && i < 0) {
299 prefix = "-";
301 // Turn our number positive.
302 if (i == std::numeric_limits<int64_t>::min()) {
303 // The most negative integer needs special treatment.
304 minint = 1;
305 num = static_cast<uint64_t>(-(i + 1));
306 } else {
307 // "Normal" negative numbers are easy.
308 num = static_cast<uint64_t>(-i);
310 } else {
311 num = static_cast<uint64_t>(i);
314 // If padding with '0' zero, emit the prefix or '-' character now. Otherwise,
315 // make the prefix accessible in reverse order, so that we can later output
316 // it right between padding and the number.
317 // We cannot choose the easier approach of just reversing the number, as that
318 // fails in situations where we need to truncate numbers that have padding
319 // and/or prefixes.
320 const char* reverse_prefix = NULL;
321 if (prefix && *prefix) {
322 if (pad == '0') {
323 while (*prefix) {
324 if (padding) {
325 --padding;
327 Out(*prefix++);
329 prefix = NULL;
330 } else {
331 for (reverse_prefix = prefix; *reverse_prefix; ++reverse_prefix) {
334 } else
335 prefix = NULL;
336 const size_t prefix_length = reverse_prefix - prefix;
338 // Loop until we have converted the entire number. Output at least one
339 // character (i.e. '0').
340 size_t start = count_;
341 size_t discarded = 0;
342 bool started = false;
343 do {
344 // Make sure there is still enough space left in our output buffer.
345 if (count_ >= size_) {
346 if (start < size_) {
347 // It is rare that we need to output a partial number. But if asked
348 // to do so, we will still make sure we output the correct number of
349 // leading digits.
350 // Since we are generating the digits in reverse order, we actually
351 // have to discard digits in the order that we have already emitted
352 // them. This is essentially equivalent to:
353 // memmove(buffer_ + start, buffer_ + start + 1, size_ - start - 1)
354 for (char* move = buffer_ + start, *end = buffer_ + size_ - 1;
355 move < end;
356 ++move) {
357 *move = move[1];
359 ++discarded;
360 --count_;
361 } else if (count_ - size_ > 1) {
362 // Need to increment either |count_| or |discarded| to make progress.
363 // The latter is more efficient, as it eventually triggers fast
364 // handling of padding. But we have to ensure we don't accidentally
365 // change the overall state (i.e. switch the state-machine from
366 // discarding to non-discarding). |count_| needs to always stay
367 // bigger than |size_|.
368 --count_;
369 ++discarded;
373 // Output the next digit and (if necessary) compensate for the most
374 // negative integer needing special treatment. This works because,
375 // no matter the bit width of the integer, the lowest-most decimal
376 // integer always ends in 2, 4, 6, or 8.
377 if (!num && started) {
378 if (reverse_prefix > prefix) {
379 Out(*--reverse_prefix);
380 } else {
381 Out(pad);
383 } else {
384 started = true;
385 Out((upcase ? kUpCaseHexDigits : kDownCaseHexDigits)[num%base + minint]);
388 minint = 0;
389 num /= base;
391 // Add padding, if requested.
392 if (padding > 0) {
393 --padding;
395 // Performance optimization for when we are asked to output excessive
396 // padding, but our output buffer is limited in size. Even if we output
397 // a 64bit number in binary, we would never write more than 64 plus
398 // prefix non-padding characters. So, once this limit has been passed,
399 // any further state change can be computed arithmetically; we know that
400 // by this time, our entire final output consists of padding characters
401 // that have all already been output.
402 if (discarded > 8*sizeof(num) + prefix_length) {
403 IncrementCount(padding);
404 padding = 0;
407 } while (num || padding || (reverse_prefix > prefix));
409 // Conversion to ASCII actually resulted in the digits being in reverse
410 // order. We can't easily generate them in forward order, as we can't tell
411 // the number of characters needed until we are done converting.
412 // So, now, we reverse the string (except for the possible '-' sign).
413 char* front = buffer_ + start;
414 char* back = GetInsertionPoint();
415 while (--back > front) {
416 char ch = *back;
417 *back = *front;
418 *front++ = ch;
421 IncrementCount(discarded);
422 return !discarded;
425 } // anonymous namespace
427 namespace internal {
429 ssize_t SafeSNPrintf(char* buf, size_t sz, const char* fmt, const Arg* args,
430 const size_t max_args) {
431 // Make sure that at least one NUL byte can be written, and that the buffer
432 // never overflows kSSizeMax. Not only does that use up most or all of the
433 // address space, it also would result in a return code that cannot be
434 // represented.
435 if (static_cast<ssize_t>(sz) < 1) {
436 return -1;
437 } else if (sz > kSSizeMax) {
438 sz = kSSizeMax;
441 // Iterate over format string and interpret '%' arguments as they are
442 // encountered.
443 Buffer buffer(buf, sz);
444 size_t padding;
445 char pad;
446 for (unsigned int cur_arg = 0; *fmt && !buffer.OutOfAddressableSpace(); ) {
447 if (*fmt++ == '%') {
448 padding = 0;
449 pad = ' ';
450 char ch = *fmt++;
451 format_character_found:
452 switch (ch) {
453 case '0': case '1': case '2': case '3': case '4':
454 case '5': case '6': case '7': case '8': case '9':
455 // Found a width parameter. Convert to an integer value and store in
456 // "padding". If the leading digit is a zero, change the padding
457 // character from a space ' ' to a zero '0'.
458 pad = ch == '0' ? '0' : ' ';
459 for (;;) {
460 // The maximum allowed padding fills all the available address
461 // space and leaves just enough space to insert the trailing NUL.
462 const size_t max_padding = kSSizeMax - 1;
463 if (padding > max_padding/10 ||
464 10*padding > max_padding - (ch - '0')) {
465 DEBUG_CHECK(padding <= max_padding/10 &&
466 10*padding <= max_padding - (ch - '0'));
467 // Integer overflow detected. Skip the rest of the width until
468 // we find the format character, then do the normal error handling.
469 padding_overflow:
470 padding = max_padding;
471 while ((ch = *fmt++) >= '0' && ch <= '9') {
473 if (cur_arg < max_args) {
474 ++cur_arg;
476 goto fail_to_expand;
478 padding = 10*padding + ch - '0';
479 if (padding > max_padding) {
480 // This doesn't happen for "sane" values of kSSizeMax. But once
481 // kSSizeMax gets smaller than about 10, our earlier range checks
482 // are incomplete. Unittests do trigger this artificial corner
483 // case.
484 DEBUG_CHECK(padding <= max_padding);
485 goto padding_overflow;
487 ch = *fmt++;
488 if (ch < '0' || ch > '9') {
489 // Reached the end of the width parameter. This is where the format
490 // character is found.
491 goto format_character_found;
494 break;
495 case 'c': { // Output an ASCII character.
496 // Check that there are arguments left to be inserted.
497 if (cur_arg >= max_args) {
498 DEBUG_CHECK(cur_arg < max_args);
499 goto fail_to_expand;
502 // Check that the argument has the expected type.
503 const Arg& arg = args[cur_arg++];
504 if (arg.type != Arg::INT && arg.type != Arg::UINT) {
505 DEBUG_CHECK(arg.type == Arg::INT || arg.type == Arg::UINT);
506 goto fail_to_expand;
509 // Apply padding, if needed.
510 buffer.Pad(' ', padding, 1);
512 // Convert the argument to an ASCII character and output it.
513 char as_char = static_cast<char>(arg.integer.i);
514 if (!as_char) {
515 goto end_of_output_buffer;
517 buffer.Out(as_char);
518 break; }
519 case 'd': // Output a possibly signed decimal value.
520 case 'o': // Output an unsigned octal value.
521 case 'x': // Output an unsigned hexadecimal value.
522 case 'X':
523 case 'p': { // Output a pointer value.
524 // Check that there are arguments left to be inserted.
525 if (cur_arg >= max_args) {
526 DEBUG_CHECK(cur_arg < max_args);
527 goto fail_to_expand;
530 const Arg& arg = args[cur_arg++];
531 int64_t i;
532 const char* prefix = NULL;
533 if (ch != 'p') {
534 // Check that the argument has the expected type.
535 if (arg.type != Arg::INT && arg.type != Arg::UINT) {
536 DEBUG_CHECK(arg.type == Arg::INT || arg.type == Arg::UINT);
537 goto fail_to_expand;
539 i = arg.integer.i;
541 if (ch != 'd') {
542 // The Arg() constructor automatically performed sign expansion on
543 // signed parameters. This is great when outputting a %d decimal
544 // number, but can result in unexpected leading 0xFF bytes when
545 // outputting a %x hexadecimal number. Mask bits, if necessary.
546 // We have to do this here, instead of in the Arg() constructor, as
547 // the Arg() constructor cannot tell whether we will output a %d
548 // or a %x. Only the latter should experience masking.
549 if (arg.integer.width < sizeof(int64_t)) {
550 i &= (1LL << (8*arg.integer.width)) - 1;
553 } else {
554 // Pointer values require an actual pointer or a string.
555 if (arg.type == Arg::POINTER) {
556 i = reinterpret_cast<uintptr_t>(arg.ptr);
557 } else if (arg.type == Arg::STRING) {
558 i = reinterpret_cast<uintptr_t>(arg.str);
559 } else if (arg.type == Arg::INT &&
560 arg.integer.width == sizeof(NULL) &&
561 arg.integer.i == 0) { // Allow C++'s version of NULL
562 i = 0;
563 } else {
564 DEBUG_CHECK(arg.type == Arg::POINTER || arg.type == Arg::STRING);
565 goto fail_to_expand;
568 // Pointers always include the "0x" prefix.
569 prefix = "0x";
572 // Use IToASCII() to convert to ASCII representation. For decimal
573 // numbers, optionally print a sign. For hexadecimal numbers,
574 // distinguish between upper and lower case. %p addresses are always
575 // printed as upcase. Supports base 8, 10, and 16. Prints padding
576 // and/or prefixes, if so requested.
577 buffer.IToASCII(ch == 'd' && arg.type == Arg::INT,
578 ch != 'x', i,
579 ch == 'o' ? 8 : ch == 'd' ? 10 : 16,
580 pad, padding, prefix);
581 break; }
582 case 's': {
583 // Check that there are arguments left to be inserted.
584 if (cur_arg >= max_args) {
585 DEBUG_CHECK(cur_arg < max_args);
586 goto fail_to_expand;
589 // Check that the argument has the expected type.
590 const Arg& arg = args[cur_arg++];
591 const char *s;
592 if (arg.type == Arg::STRING) {
593 s = arg.str ? arg.str : "<NULL>";
594 } else if (arg.type == Arg::INT && arg.integer.width == sizeof(NULL) &&
595 arg.integer.i == 0) { // Allow C++'s version of NULL
596 s = "<NULL>";
597 } else {
598 DEBUG_CHECK(arg.type == Arg::STRING);
599 goto fail_to_expand;
602 // Apply padding, if needed. This requires us to first check the
603 // length of the string that we are outputting.
604 if (padding) {
605 size_t len = 0;
606 for (const char* src = s; *src++; ) {
607 ++len;
609 buffer.Pad(' ', padding, len);
612 // Printing a string involves nothing more than copying it into the
613 // output buffer and making sure we don't output more bytes than
614 // available space; Out() takes care of doing that.
615 for (const char* src = s; *src; ) {
616 buffer.Out(*src++);
618 break; }
619 case '%':
620 // Quoted percent '%' character.
621 goto copy_verbatim;
622 fail_to_expand:
623 // C++ gives us tools to do type checking -- something that snprintf()
624 // could never really do. So, whenever we see arguments that don't
625 // match up with the format string, we refuse to output them. But
626 // since we have to be extremely conservative about being async-
627 // signal-safe, we are limited in the type of error handling that we
628 // can do in production builds (in debug builds we can use
629 // DEBUG_CHECK() and hope for the best). So, all we do is pass the
630 // format string unchanged. That should eventually get the user's
631 // attention; and in the meantime, it hopefully doesn't lose too much
632 // data.
633 default:
634 // Unknown or unsupported format character. Just copy verbatim to
635 // output.
636 buffer.Out('%');
637 DEBUG_CHECK(ch);
638 if (!ch) {
639 goto end_of_format_string;
641 buffer.Out(ch);
642 break;
644 } else {
645 copy_verbatim:
646 buffer.Out(fmt[-1]);
649 end_of_format_string:
650 end_of_output_buffer:
651 return buffer.GetCount();
654 } // namespace internal
656 ssize_t SafeSNPrintf(char* buf, size_t sz, const char* fmt) {
657 // Make sure that at least one NUL byte can be written, and that the buffer
658 // never overflows kSSizeMax. Not only does that use up most or all of the
659 // address space, it also would result in a return code that cannot be
660 // represented.
661 if (static_cast<ssize_t>(sz) < 1) {
662 return -1;
663 } else if (sz > kSSizeMax) {
664 sz = kSSizeMax;
667 Buffer buffer(buf, sz);
669 // In the slow-path, we deal with errors by copying the contents of
670 // "fmt" unexpanded. This means, if there are no arguments passed, the
671 // SafeSPrintf() function always degenerates to a version of strncpy() that
672 // de-duplicates '%' characters.
673 const char* src = fmt;
674 for (; *src; ++src) {
675 buffer.Out(*src);
676 DEBUG_CHECK(src[0] != '%' || src[1] == '%');
677 if (src[0] == '%' && src[1] == '%') {
678 ++src;
681 return buffer.GetCount();
684 } // namespace strings
685 } // namespace base