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

Side by Side Diff: src/runtime.cc

Issue 12326120: Fix overflow in WriteQuoteJsonString and SlowQuoteJsonString (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-latin-1.js » ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 5230 matching lines...) Expand 10 before | Expand all | Expand 10 after
5241 template <typename Char, typename StringType, bool comma> 5241 template <typename Char, typename StringType, bool comma>
5242 static MaybeObject* SlowQuoteJsonString(Isolate* isolate, 5242 static MaybeObject* SlowQuoteJsonString(Isolate* isolate,
5243 Vector<const Char> characters) { 5243 Vector<const Char> characters) {
5244 int length = characters.length(); 5244 int length = characters.length();
5245 const Char* read_cursor = characters.start(); 5245 const Char* read_cursor = characters.start();
5246 const Char* end = read_cursor + length; 5246 const Char* end = read_cursor + length;
5247 const int kSpaceForQuotes = 2 + (comma ? 1 :0); 5247 const int kSpaceForQuotes = 2 + (comma ? 1 :0);
5248 int quoted_length = kSpaceForQuotes; 5248 int quoted_length = kSpaceForQuotes;
5249 while (read_cursor < end) { 5249 while (read_cursor < end) {
5250 Char c = *(read_cursor++); 5250 Char c = *(read_cursor++);
5251 if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { 5251 if (static_cast<unsigned>(c) >= kQuoteTableLength) {
5252 quoted_length++; 5252 quoted_length++;
5253 } else { 5253 } else {
5254 quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; 5254 quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)];
5255 } 5255 }
5256 } 5256 }
5257 MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, 5257 MaybeObject* new_alloc = AllocateRawString<StringType>(isolate,
5258 quoted_length); 5258 quoted_length);
5259 Object* new_object; 5259 Object* new_object;
5260 if (!new_alloc->ToObject(&new_object)) { 5260 if (!new_alloc->ToObject(&new_object)) {
5261 return new_alloc; 5261 return new_alloc;
5262 } 5262 }
5263 StringType* new_string = StringType::cast(new_object); 5263 StringType* new_string = StringType::cast(new_object);
5264 5264
5265 Char* write_cursor = reinterpret_cast<Char*>( 5265 Char* write_cursor = reinterpret_cast<Char*>(
5266 new_string->address() + SeqString::kHeaderSize); 5266 new_string->address() + SeqString::kHeaderSize);
5267 if (comma) *(write_cursor++) = ','; 5267 if (comma) *(write_cursor++) = ',';
5268 *(write_cursor++) = '"'; 5268 *(write_cursor++) = '"';
5269 5269
5270 read_cursor = characters.start(); 5270 read_cursor = characters.start();
5271 while (read_cursor < end) { 5271 while (read_cursor < end) {
5272 Char c = *(read_cursor++); 5272 Char c = *(read_cursor++);
5273 if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { 5273 if (static_cast<unsigned>(c) >= kQuoteTableLength) {
5274 *(write_cursor++) = c; 5274 *(write_cursor++) = c;
5275 } else { 5275 } else {
5276 int len = JsonQuoteLengths[static_cast<unsigned>(c)]; 5276 int len = JsonQuoteLengths[static_cast<unsigned>(c)];
5277 const char* replacement = JsonQuotes + 5277 const char* replacement = JsonQuotes +
5278 static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; 5278 static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry;
5279 for (int i = 0; i < len; i++) { 5279 for (int i = 0; i < len; i++) {
5280 *write_cursor++ = *replacement++; 5280 *write_cursor++ = *replacement++;
5281 } 5281 }
5282 } 5282 }
5283 } 5283 }
5284 *(write_cursor++) = '"'; 5284 *(write_cursor++) = '"';
5285 return new_string; 5285 return new_string;
5286 } 5286 }
5287 5287
5288 5288
5289 template <typename SinkChar, typename SourceChar> 5289 template <typename SinkChar, typename SourceChar>
5290 static inline SinkChar* WriteQuoteJsonString( 5290 static inline SinkChar* WriteQuoteJsonString(
5291 Isolate* isolate, 5291 Isolate* isolate,
5292 SinkChar* write_cursor, 5292 SinkChar* write_cursor,
5293 Vector<const SourceChar> characters) { 5293 Vector<const SourceChar> characters) {
5294 // SinkChar is only char if SourceChar is guaranteed to be char. 5294 // SinkChar is only char if SourceChar is guaranteed to be char.
5295 ASSERT(sizeof(SinkChar) >= sizeof(SourceChar)); 5295 ASSERT(sizeof(SinkChar) >= sizeof(SourceChar));
5296 const SourceChar* read_cursor = characters.start(); 5296 const SourceChar* read_cursor = characters.start();
5297 const SourceChar* end = read_cursor + characters.length(); 5297 const SourceChar* end = read_cursor + characters.length();
5298 *(write_cursor++) = '"'; 5298 *(write_cursor++) = '"';
5299 while (read_cursor < end) { 5299 while (read_cursor < end) {
5300 SourceChar c = *(read_cursor++); 5300 SourceChar c = *(read_cursor++);
5301 if (sizeof(SourceChar) > 1u && 5301 if (static_cast<unsigned>(c) >= kQuoteTableLength) {
5302 static_cast<unsigned>(c) >= kQuoteTableLength) {
5303 *(write_cursor++) = static_cast<SinkChar>(c); 5302 *(write_cursor++) = static_cast<SinkChar>(c);
5304 } else { 5303 } else {
5305 int len = JsonQuoteLengths[static_cast<unsigned>(c)]; 5304 int len = JsonQuoteLengths[static_cast<unsigned>(c)];
5306 const char* replacement = JsonQuotes + 5305 const char* replacement = JsonQuotes +
5307 static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; 5306 static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry;
5308 write_cursor[0] = replacement[0]; 5307 write_cursor[0] = replacement[0];
5309 if (len > 1) { 5308 if (len > 1) {
5310 write_cursor[1] = replacement[1]; 5309 write_cursor[1] = replacement[1];
5311 if (len > 2) { 5310 if (len > 2) {
5312 ASSERT(len == 6); 5311 ASSERT(len == 6);
(...skipping 8023 matching lines...) Expand 10 before | Expand all | Expand 10 after
13336 // Handle last resort GC and make sure to allow future allocations 13335 // Handle last resort GC and make sure to allow future allocations
13337 // to grow the heap without causing GCs (if possible). 13336 // to grow the heap without causing GCs (if possible).
13338 isolate->counters()->gc_last_resort_from_js()->Increment(); 13337 isolate->counters()->gc_last_resort_from_js()->Increment();
13339 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, 13338 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags,
13340 "Runtime::PerformGC"); 13339 "Runtime::PerformGC");
13341 } 13340 }
13342 } 13341 }
13343 13342
13344 13343
13345 } } // namespace v8::internal 13344 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-latin-1.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698