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 #include "base/strings/safe_sprintf.h" | 5 #include "base/strings/safe_sprintf.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #if !defined(NDEBUG) | 9 #if !defined(NDEBUG) |
10 // In debug builds, we use RAW_CHECK() to print useful error messages, if | 10 // In debug builds, we use RAW_CHECK() to print useful error messages, if |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 const Arg& arg = args[cur_arg++]; | 501 const Arg& arg = args[cur_arg++]; |
502 if (arg.type != Arg::INT && arg.type != Arg::UINT) { | 502 if (arg.type != Arg::INT && arg.type != Arg::UINT) { |
503 DEBUG_CHECK(arg.type == Arg::INT || arg.type == Arg::UINT); | 503 DEBUG_CHECK(arg.type == Arg::INT || arg.type == Arg::UINT); |
504 goto fail_to_expand; | 504 goto fail_to_expand; |
505 } | 505 } |
506 | 506 |
507 // Apply padding, if needed. | 507 // Apply padding, if needed. |
508 buffer.Pad(' ', padding, 1); | 508 buffer.Pad(' ', padding, 1); |
509 | 509 |
510 // Convert the argument to an ASCII character and output it. | 510 // Convert the argument to an ASCII character and output it. |
511 char ch = static_cast<char>(arg.i); | 511 char ch = static_cast<char>(arg.integer.i); |
512 if (!ch) { | 512 if (!ch) { |
513 goto end_of_output_buffer; | 513 goto end_of_output_buffer; |
514 } | 514 } |
515 buffer.Out(ch); | 515 buffer.Out(ch); |
516 break; } | 516 break; } |
517 case 'd': // Output a possibly signed decimal value. | 517 case 'd': // Output a possibly signed decimal value. |
518 case 'o': // Output an unsigned octal value. | 518 case 'o': // Output an unsigned octal value. |
519 case 'x': // Output an unsigned hexadecimal value. | 519 case 'x': // Output an unsigned hexadecimal value. |
520 case 'X': | 520 case 'X': |
521 case 'p': { // Output a pointer value. | 521 case 'p': { // Output a pointer value. |
522 // Check that there are arguments left to be inserted. | 522 // Check that there are arguments left to be inserted. |
523 if (cur_arg >= max_args) { | 523 if (cur_arg >= max_args) { |
524 DEBUG_CHECK(cur_arg < max_args); | 524 DEBUG_CHECK(cur_arg < max_args); |
525 goto fail_to_expand; | 525 goto fail_to_expand; |
526 } | 526 } |
527 | 527 |
528 const Arg& arg = args[cur_arg++]; | 528 const Arg& arg = args[cur_arg++]; |
529 int64_t i; | 529 int64_t i; |
530 const char* prefix = NULL; | 530 const char* prefix = NULL; |
531 if (ch != 'p') { | 531 if (ch != 'p') { |
532 // Check that the argument has the expected type. | 532 // Check that the argument has the expected type. |
533 if (arg.type != Arg::INT && arg.type != Arg::UINT) { | 533 if (arg.type != Arg::INT && arg.type != Arg::UINT) { |
534 DEBUG_CHECK(arg.type == Arg::INT || arg.type == Arg::UINT); | 534 DEBUG_CHECK(arg.type == Arg::INT || arg.type == Arg::UINT); |
535 goto fail_to_expand; | 535 goto fail_to_expand; |
536 } | 536 } |
537 i = arg.i; | 537 i = arg.integer.i; |
538 | 538 |
539 if (ch != 'd') { | 539 if (ch != 'd') { |
540 // The Arg() constructor automatically performed sign expansion on | 540 // The Arg() constructor automatically performed sign expansion on |
541 // signed parameters. This is great when outputting a %d decimal | 541 // signed parameters. This is great when outputting a %d decimal |
542 // number, but can result in unexpected leading 0xFF bytes when | 542 // number, but can result in unexpected leading 0xFF bytes when |
543 // outputting a %x hexadecimal number. Mask bits, if necessary. | 543 // outputting a %x hexadecimal number. Mask bits, if necessary. |
544 // We have to do this here, instead of in the Arg() constructor, as | 544 // We have to do this here, instead of in the Arg() constructor, as |
545 // the Arg() constructor cannot tell whether we will output a %d | 545 // the Arg() constructor cannot tell whether we will output a %d |
546 // or a %x. Only the latter should experience masking. | 546 // or a %x. Only the latter should experience masking. |
547 if (arg.width < sizeof(int64_t)) { | 547 if (arg.integer.width < sizeof(int64_t)) { |
548 i &= (1LL << (8*arg.width)) - 1; | 548 i &= (1LL << (8*arg.integer.width)) - 1; |
549 } | 549 } |
550 } | 550 } |
551 } else { | 551 } else { |
552 // Pointer values require an actual pointer or a string. | 552 // Pointer values require an actual pointer or a string. |
553 if (arg.type == Arg::POINTER) { | 553 if (arg.type == Arg::POINTER) { |
554 i = reinterpret_cast<uintptr_t>(arg.ptr); | 554 i = reinterpret_cast<uintptr_t>(arg.ptr); |
555 } else if (arg.type == Arg::STRING) { | 555 } else if (arg.type == Arg::STRING) { |
556 i = reinterpret_cast<uintptr_t>(arg.str); | 556 i = reinterpret_cast<uintptr_t>(arg.str); |
557 } else if (arg.type == Arg::INT && arg.width == sizeof(NULL) && | 557 } else if (arg.type == Arg::INT && |
558 arg.i == 0) { // Allow C++'s version of NULL | 558 arg.integer.width == sizeof(NULL) && |
| 559 arg.integer.i == 0) { // Allow C++'s version of NULL |
559 i = 0; | 560 i = 0; |
560 } else { | 561 } else { |
561 DEBUG_CHECK(arg.type == Arg::POINTER || arg.type == Arg::STRING); | 562 DEBUG_CHECK(arg.type == Arg::POINTER || arg.type == Arg::STRING); |
562 goto fail_to_expand; | 563 goto fail_to_expand; |
563 } | 564 } |
564 | 565 |
565 // Pointers always include the "0x" prefix. | 566 // Pointers always include the "0x" prefix. |
566 prefix = "0x"; | 567 prefix = "0x"; |
567 } | 568 } |
568 | 569 |
(...skipping 12 matching lines...) Expand all Loading... |
581 if (cur_arg >= max_args) { | 582 if (cur_arg >= max_args) { |
582 DEBUG_CHECK(cur_arg < max_args); | 583 DEBUG_CHECK(cur_arg < max_args); |
583 goto fail_to_expand; | 584 goto fail_to_expand; |
584 } | 585 } |
585 | 586 |
586 // Check that the argument has the expected type. | 587 // Check that the argument has the expected type. |
587 const Arg& arg = args[cur_arg++]; | 588 const Arg& arg = args[cur_arg++]; |
588 const char *s; | 589 const char *s; |
589 if (arg.type == Arg::STRING) { | 590 if (arg.type == Arg::STRING) { |
590 s = arg.str ? arg.str : "<NULL>"; | 591 s = arg.str ? arg.str : "<NULL>"; |
591 } else if (arg.type == Arg::INT && arg.width == sizeof(NULL) && | 592 } else if (arg.type == Arg::INT && arg.integer.width == sizeof(NULL) && |
592 arg.i == 0) { // Allow C++'s version of NULL | 593 arg.integer.i == 0) { // Allow C++'s version of NULL |
593 s = "<NULL>"; | 594 s = "<NULL>"; |
594 } else { | 595 } else { |
595 DEBUG_CHECK(arg.type == Arg::STRING); | 596 DEBUG_CHECK(arg.type == Arg::STRING); |
596 goto fail_to_expand; | 597 goto fail_to_expand; |
597 } | 598 } |
598 | 599 |
599 // Apply padding, if needed. This requires us to first check the | 600 // Apply padding, if needed. This requires us to first check the |
600 // length of the string that we are outputting. | 601 // length of the string that we are outputting. |
601 if (padding) { | 602 if (padding) { |
602 size_t len = 0; | 603 size_t len = 0; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 DEBUG_CHECK(src[0] != '%' || src[1] == '%'); | 674 DEBUG_CHECK(src[0] != '%' || src[1] == '%'); |
674 if (src[0] == '%' && src[1] == '%') { | 675 if (src[0] == '%' && src[1] == '%') { |
675 ++src; | 676 ++src; |
676 } | 677 } |
677 } | 678 } |
678 return buffer.GetCount(); | 679 return buffer.GetCount(); |
679 } | 680 } |
680 | 681 |
681 } // namespace strings | 682 } // namespace strings |
682 } // namespace base | 683 } // namespace base |
OLD | NEW |