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

Side by Side Diff: base/json/string_escape.cc

Issue 2903773003: Escape invalid code points (Closed)
Patch Set: Update documentation and signed/unsigned comparison Created 3 years, 7 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 | « base/json/string_escape.h ('k') | base/json/string_escape_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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/json/string_escape.h" 5 #include "base/json/string_escape.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 if (put_in_quotes) 84 if (put_in_quotes)
85 dest->push_back('"'); 85 dest->push_back('"');
86 86
87 // Casting is necessary because ICU uses int32_t. Try and do so safely. 87 // Casting is necessary because ICU uses int32_t. Try and do so safely.
88 CHECK_LE(str.length(), 88 CHECK_LE(str.length(),
89 static_cast<size_t>(std::numeric_limits<int32_t>::max())); 89 static_cast<size_t>(std::numeric_limits<int32_t>::max()));
90 const int32_t length = static_cast<int32_t>(str.length()); 90 const int32_t length = static_cast<int32_t>(str.length());
91 91
92 for (int32_t i = 0; i < length; ++i) { 92 for (int32_t i = 0; i < length; ++i) {
93 uint32_t code_point; 93 uint32_t code_point;
94 if (!ReadUnicodeCharacter(str.data(), length, &i, &code_point)) { 94 if (!ReadUnicodeCharacter(str.data(), length, &i, &code_point) ||
95 code_point == static_cast<decltype(code_point)>(CBU_SENTINEL) ||
96 !IsValidCharacter(code_point)) {
95 code_point = kReplacementCodePoint; 97 code_point = kReplacementCodePoint;
96 did_replacement = true; 98 did_replacement = true;
97 } 99 }
98 100
99 if (EscapeSpecialCodePoint(code_point, dest)) 101 if (EscapeSpecialCodePoint(code_point, dest))
100 continue; 102 continue;
101 103
102 // Escape non-printing characters. 104 // Escape non-printing characters.
103 if (code_point < 32) 105 if (code_point < 32)
104 base::StringAppendF(dest, kU16EscapeFormat, code_point); 106 base::StringAppendF(dest, kU16EscapeFormat, code_point);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 dest.push_back(*it); 160 dest.push_back(*it);
159 } 161 }
160 162
161 if (put_in_quotes) 163 if (put_in_quotes)
162 dest.push_back('"'); 164 dest.push_back('"');
163 165
164 return dest; 166 return dest;
165 } 167 }
166 168
167 } // namespace base 169 } // namespace base
OLDNEW
« no previous file with comments | « base/json/string_escape.h ('k') | base/json/string_escape_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698