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

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

Issue 2903773003: Escape invalid code points (Closed)
Patch Set: 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 | « no previous file | 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 < 0 || !IsValidCharacter(code_point)) {
Mark Mentovai 2017/05/24 19:02:48 How can code_point, a uint32_t, ever be < 0?
Mark Mentovai 2017/05/24 19:02:48 More importantly, you’re confusing characters with
sabbakumov 2017/05/25 03:26:14 I've updated the documentation. IsValidCharacter()
sabbakumov 2017/05/25 03:26:14 Thank you. I've mistakenly copy-pasted it from her
sabbakumov 2017/05/25 03:26:14 Done.
sabbakumov 2017/05/25 03:26:14 Done.
95 code_point = kReplacementCodePoint; 96 code_point = kReplacementCodePoint;
96 did_replacement = true; 97 did_replacement = true;
97 } 98 }
98 99
99 if (EscapeSpecialCodePoint(code_point, dest)) 100 if (EscapeSpecialCodePoint(code_point, dest))
100 continue; 101 continue;
101 102
102 // Escape non-printing characters. 103 // Escape non-printing characters.
103 if (code_point < 32) 104 if (code_point < 32)
104 base::StringAppendF(dest, kU16EscapeFormat, code_point); 105 base::StringAppendF(dest, kU16EscapeFormat, code_point);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 dest.push_back(*it); 159 dest.push_back(*it);
159 } 160 }
160 161
161 if (put_in_quotes) 162 if (put_in_quotes)
162 dest.push_back('"'); 163 dest.push_back('"');
163 164
164 return dest; 165 return dest;
165 } 166 }
166 167
167 } // namespace base 168 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/json/string_escape_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698