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

Side by Side Diff: base/string_util.cc

Issue 6877008: Add a unittest case of ReplaceStringPlaceholders() (Closed)
Patch Set: Prevent overrun of string parsing Created 9 years, 6 months 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 | base/string_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/string_util.h" 5 #include "base/string_util.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <ctype.h> 9 #include <ctype.h>
10 #include <errno.h> 10 #include <errno.h>
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 } 808 }
809 809
810 string16 JoinString(const std::vector<string16>& parts, char16 sep) { 810 string16 JoinString(const std::vector<string16>& parts, char16 sep) {
811 return JoinStringT(parts, sep); 811 return JoinStringT(parts, sep);
812 } 812 }
813 813
814 template<class FormatStringType, class OutStringType> 814 template<class FormatStringType, class OutStringType>
815 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, 815 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string,
816 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { 816 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) {
817 size_t substitutions = subst.size(); 817 size_t substitutions = subst.size();
818 DCHECK(substitutions < 10);
819 818
820 size_t sub_length = 0; 819 size_t sub_length = 0;
821 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); 820 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin();
822 iter != subst.end(); ++iter) { 821 iter != subst.end(); ++iter) {
823 sub_length += iter->length(); 822 sub_length += iter->length();
824 } 823 }
825 824
826 OutStringType formatted; 825 OutStringType formatted;
827 formatted.reserve(format_string.length() + sub_length); 826 formatted.reserve(format_string.length() + sub_length);
828 827
829 std::vector<ReplacementOffset> r_offsets; 828 std::vector<ReplacementOffset> r_offsets;
830 for (typename FormatStringType::const_iterator i = format_string.begin(); 829 for (typename FormatStringType::const_iterator i = format_string.begin();
831 i != format_string.end(); ++i) { 830 i != format_string.end(); ++i) {
832 if ('$' == *i) { 831 if ('$' == *i) {
833 if (i + 1 != format_string.end()) { 832 if (i + 1 != format_string.end()) {
834 ++i; 833 ++i;
835 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i; 834 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i;
836 if ('$' == *i) { 835 if ('$' == *i) {
837 while (i != format_string.end() && '$' == *i) { 836 while (i != format_string.end() && '$' == *i) {
838 formatted.push_back('$'); 837 formatted.push_back('$');
839 ++i; 838 ++i;
840 } 839 }
841 --i; 840 --i;
842 } else { 841 } else {
843 uintptr_t index = *i - '1'; 842 uintptr_t index = 0;
843 while (i != format_string.end() && '0' <= *i && *i <= '9') {
844 index *= 10;
845 index += *i - '0';
846 ++i;
847 }
848 --i;
849 index -= 1;
844 if (offsets) { 850 if (offsets) {
845 ReplacementOffset r_offset(index, 851 ReplacementOffset r_offset(index,
846 static_cast<int>(formatted.size())); 852 static_cast<int>(formatted.size()));
847 r_offsets.insert(std::lower_bound(r_offsets.begin(), 853 r_offsets.insert(std::lower_bound(r_offsets.begin(),
848 r_offsets.end(), 854 r_offsets.end(),
849 r_offset, 855 r_offset,
850 &CompareParameter), 856 &CompareParameter),
851 r_offset); 857 r_offset);
852 } 858 }
853 if (index < substitutions) 859 if (index < substitutions)
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
1072 } 1078 }
1073 1079
1074 } // namespace 1080 } // namespace
1075 1081
1076 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { 1082 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) {
1077 return lcpyT<char>(dst, src, dst_size); 1083 return lcpyT<char>(dst, src, dst_size);
1078 } 1084 }
1079 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { 1085 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
1080 return lcpyT<wchar_t>(dst, src, dst_size); 1086 return lcpyT<wchar_t>(dst, src, dst_size);
1081 } 1087 }
OLDNEW
« no previous file with comments | « no previous file | base/string_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698