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"
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
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
28 // errno = 13 (Access denied)
29 // In most of the anticipated use cases, that's probably the preferred
31 #include "base/logging.h"
32 #define DEBUG_CHECK RAW_CHECK
34 #define DEBUG_CHECK(x) do { if (x) { } } while (0)
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
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
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-
60 // http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html
63 const size_t kSSizeMaxConst
= ((size_t)(ssize_t
)-1) >> 1;
65 const char kUpCaseHexDigits
[] = "0123456789ABCDEF";
66 const char kDownCaseHexDigits
[] = "0123456789abcdef";
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.
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.
84 static size_t kSSizeMax
= kSSizeMaxConst
;
88 void SetSafeSPrintfSSizeMaxForTest(size_t max
) {
92 size_t GetSafeSPrintfSSizeMaxForTest() {
96 #endif // defined(NDEBUG)
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
)
108 size_(size
- 1), // Account for trailing NUL byte
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
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
);
123 DEBUG_CHECK(size
> 0);
124 DEBUG_CHECK(size
<= kSSizeMax
);
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();
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
) {
179 DEBUG_CHECK(padding
<= kSSizeMax
);
180 for (; padding
> len
; --padding
) {
183 IncrementCount(padding
-len
);
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|
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
);
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;
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 {
258 return buffer_
+ idx
;
261 // User-provided buffer that will receive the fully formatted output string.
264 // Number of bytes that are available in the buffer excluding the trailing
265 // NUL byte that will be added by the destructor.
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.
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
291 // Details are tricky, because of limited data-types, but equivalent pseudo-
292 // code would look like:
293 // if (sign && i < 0)
301 // Turn our number positive.
302 if (i
== std::numeric_limits
<int64_t>::min()) {
303 // The most negative integer needs special treatment.
305 num
= static_cast<uint64_t>(-(i
+ 1));
307 // "Normal" negative numbers are easy.
308 num
= static_cast<uint64_t>(-i
);
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
320 const char* reverse_prefix
= NULL
;
321 if (prefix
&& *prefix
) {
331 for (reverse_prefix
= prefix
; *reverse_prefix
; ++reverse_prefix
) {
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;
344 // Make sure there is still enough space left in our output buffer.
345 if (count_
>= 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
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;
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_|.
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
);
385 Out((upcase
? kUpCaseHexDigits
: kDownCaseHexDigits
)[num
%base
+ minint
]);
391 // Add padding, if requested.
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
);
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
) {
421 IncrementCount(discarded
);
425 } // anonymous namespace
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
435 if (static_cast<ssize_t
>(sz
) < 1) {
437 } else if (sz
> kSSizeMax
) {
441 // Iterate over format string and interpret '%' arguments as they are
443 Buffer
buffer(buf
, sz
);
446 for (unsigned int cur_arg
= 0; *fmt
&& !buffer
.OutOfAddressableSpace(); ) {
451 format_character_found
:
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' : ' ';
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.
470 padding
= max_padding
;
471 while ((ch
= *fmt
++) >= '0' && ch
<= '9') {
473 if (cur_arg
< max_args
) {
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
484 DEBUG_CHECK(padding
<= max_padding
);
485 goto padding_overflow
;
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
;
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
);
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
);
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
);
515 goto end_of_output_buffer
;
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.
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
);
530 const Arg
& arg
= args
[cur_arg
++];
532 const char* prefix
= NULL
;
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
);
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;
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
564 DEBUG_CHECK(arg
.type
== Arg::POINTER
|| arg
.type
== Arg::STRING
);
568 // Pointers always include the "0x" prefix.
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
,
579 ch
== 'o' ? 8 : ch
== 'd' ? 10 : 16,
580 pad
, padding
, prefix
);
583 // Check that there are arguments left to be inserted.
584 if (cur_arg
>= max_args
) {
585 DEBUG_CHECK(cur_arg
< max_args
);
589 // Check that the argument has the expected type.
590 const Arg
& arg
= args
[cur_arg
++];
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
598 DEBUG_CHECK(arg
.type
== Arg::STRING
);
602 // Apply padding, if needed. This requires us to first check the
603 // length of the string that we are outputting.
606 for (const char* src
= s
; *src
++; ) {
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
; ) {
620 // Quoted percent '%' character.
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
634 // Unknown or unsupported format character. Just copy verbatim to
639 goto end_of_format_string
;
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
661 if (static_cast<ssize_t
>(sz
) < 1) {
663 } else if (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
) {
676 DEBUG_CHECK(src
[0] != '%' || src
[1] == '%');
677 if (src
[0] == '%' && src
[1] == '%') {
681 return buffer
.GetCount();
684 } // namespace strings