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