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

Side by Side Diff: src/d8.cc

Issue 20081: Add explicit null checks after string conversions in the shells. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 10 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 | « samples/shell.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 87
88 Shell::CounterMap Shell::counter_map_; 88 Shell::CounterMap Shell::counter_map_;
89 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; 89 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
90 CounterCollection Shell::local_counters_; 90 CounterCollection Shell::local_counters_;
91 CounterCollection* Shell::counters_ = &local_counters_; 91 CounterCollection* Shell::counters_ = &local_counters_;
92 Persistent<Context> Shell::utility_context_; 92 Persistent<Context> Shell::utility_context_;
93 Persistent<Context> Shell::evaluation_context_; 93 Persistent<Context> Shell::evaluation_context_;
94 94
95 95
96 // Converts a V8 value to a C string.
97 const char* ToCString(const v8::String::Utf8Value& value) {
98 return *value ? *value : "<string conversion failed>";
99 }
100
101
96 // Executes a string within the current v8 context. 102 // Executes a string within the current v8 context.
97 bool Shell::ExecuteString(Handle<String> source, 103 bool Shell::ExecuteString(Handle<String> source,
98 Handle<Value> name, 104 Handle<Value> name,
99 bool print_result, 105 bool print_result,
100 bool report_exceptions) { 106 bool report_exceptions) {
101 HandleScope handle_scope; 107 HandleScope handle_scope;
102 TryCatch try_catch; 108 TryCatch try_catch;
103 if (i::FLAG_debugger) { 109 if (i::FLAG_debugger) {
104 // When debugging make exceptions appear to be uncaught. 110 // When debugging make exceptions appear to be uncaught.
105 try_catch.SetVerbose(true); 111 try_catch.SetVerbose(true);
106 } 112 }
107 Handle<Script> script = Script::Compile(source, name); 113 Handle<Script> script = Script::Compile(source, name);
108 if (script.IsEmpty()) { 114 if (script.IsEmpty()) {
109 // Print errors that happened during compilation. 115 // Print errors that happened during compilation.
110 if (report_exceptions && !i::FLAG_debugger) 116 if (report_exceptions && !i::FLAG_debugger)
111 ReportException(&try_catch); 117 ReportException(&try_catch);
112 return false; 118 return false;
113 } else { 119 } else {
114 Handle<Value> result = script->Run(); 120 Handle<Value> result = script->Run();
115 if (result.IsEmpty()) { 121 if (result.IsEmpty()) {
116 // Print errors that happened during execution. 122 // Print errors that happened during execution.
117 if (report_exceptions && !i::FLAG_debugger) 123 if (report_exceptions && !i::FLAG_debugger)
118 ReportException(&try_catch); 124 ReportException(&try_catch);
119 return false; 125 return false;
120 } else { 126 } else {
121 if (print_result && !result->IsUndefined()) { 127 if (print_result && !result->IsUndefined()) {
122 // If all went well and the result wasn't undefined then print 128 // If all went well and the result wasn't undefined then print
123 // the returned value. 129 // the returned value.
124 String::Utf8Value str(result); 130 v8::String::Utf8Value str(result);
125 printf("%s\n", *str); 131 const char* cstr = ToCString(str);
132 printf("%s\n", cstr);
126 } 133 }
127 return true; 134 return true;
128 } 135 }
129 } 136 }
130 } 137 }
131 138
132 139
133 Handle<Value> Shell::Print(const Arguments& args) { 140 Handle<Value> Shell::Print(const Arguments& args) {
134 bool first = true; 141 bool first = true;
135 for (int i = 0; i < args.Length(); i++) { 142 for (int i = 0; i < args.Length(); i++) {
136 HandleScope handle_scope; 143 HandleScope handle_scope;
137 if (first) { 144 if (first) {
138 first = false; 145 first = false;
139 } else { 146 } else {
140 printf(" "); 147 printf(" ");
141 } 148 }
142 String::Utf8Value str(args[i]); 149 v8::String::Utf8Value str(args[i]);
143 printf("%s", *str); 150 const char* cstr = ToCString(str);
151 printf("%s", cstr);
144 } 152 }
145 printf("\n"); 153 printf("\n");
146 return Undefined(); 154 return Undefined();
147 } 155 }
148 156
149 157
150 Handle<Value> Shell::Load(const Arguments& args) { 158 Handle<Value> Shell::Load(const Arguments& args) {
151 for (int i = 0; i < args.Length(); i++) { 159 for (int i = 0; i < args.Length(); i++) {
152 HandleScope handle_scope; 160 HandleScope handle_scope;
153 String::Utf8Value file(args[i]); 161 String::Utf8Value file(args[i]);
162 if (*file == NULL) {
163 return ThrowException(String::New("Error loading file"));
164 }
154 Handle<String> source = ReadFile(*file); 165 Handle<String> source = ReadFile(*file);
155 if (source.IsEmpty()) { 166 if (source.IsEmpty()) {
156 return ThrowException(String::New("Error loading file")); 167 return ThrowException(String::New("Error loading file"));
157 } 168 }
158 if (!ExecuteString(source, String::New(*file), false, false)) { 169 if (!ExecuteString(source, String::New(*file), false, false)) {
159 return ThrowException(String::New("Error executing file")); 170 return ThrowException(String::New("Error executing file"));
160 } 171 }
161 } 172 }
162 return Undefined(); 173 return Undefined();
163 } 174 }
164 175
165 176
166 Handle<Value> Shell::Quit(const Arguments& args) { 177 Handle<Value> Shell::Quit(const Arguments& args) {
167 int exit_code = args[0]->Int32Value(); 178 int exit_code = args[0]->Int32Value();
168 OnExit(); 179 OnExit();
169 exit(exit_code); 180 exit(exit_code);
170 return Undefined(); 181 return Undefined();
171 } 182 }
172 183
173 184
174 Handle<Value> Shell::Version(const Arguments& args) { 185 Handle<Value> Shell::Version(const Arguments& args) {
175 return String::New(V8::GetVersion()); 186 return String::New(V8::GetVersion());
176 } 187 }
177 188
178 189
179 void Shell::ReportException(v8::TryCatch* try_catch) { 190 void Shell::ReportException(v8::TryCatch* try_catch) {
180 HandleScope handle_scope; 191 HandleScope handle_scope;
181 String::Utf8Value exception(try_catch->Exception()); 192 v8::String::Utf8Value exception(try_catch->Exception());
193 const char* exception_string = ToCString(exception);
182 Handle<Message> message = try_catch->Message(); 194 Handle<Message> message = try_catch->Message();
183 if (message.IsEmpty()) { 195 if (message.IsEmpty()) {
184 // V8 didn't provide any extra information about this error; just 196 // V8 didn't provide any extra information about this error; just
185 // print the exception. 197 // print the exception.
186 printf("%s\n", *exception); 198 printf("%s\n", exception_string);
187 } else { 199 } else {
188 // Print (filename):(line number): (message). 200 // Print (filename):(line number): (message).
189 String::Utf8Value filename(message->GetScriptResourceName()); 201 v8::String::Utf8Value filename(message->GetScriptResourceName());
202 const char* filename_string = ToCString(filename);
190 int linenum = message->GetLineNumber(); 203 int linenum = message->GetLineNumber();
191 printf("%s:%i: %s\n", *filename, linenum, *exception); 204 printf("%s:%i: %s\n", filename_string, linenum, exception_string);
192 // Print line of source code. 205 // Print line of source code.
193 String::Utf8Value sourceline(message->GetSourceLine()); 206 v8::String::Utf8Value sourceline(message->GetSourceLine());
194 printf("%s\n", *sourceline); 207 const char* sourceline_string = ToCString(sourceline);
208 printf("%s\n", sourceline_string);
195 // Print wavy underline (GetUnderline is deprecated). 209 // Print wavy underline (GetUnderline is deprecated).
196 int start = message->GetStartColumn(); 210 int start = message->GetStartColumn();
197 for (int i = 0; i < start; i++) { 211 for (int i = 0; i < start; i++) {
198 printf(" "); 212 printf(" ");
199 } 213 }
200 int end = message->GetEndColumn(); 214 int end = message->GetEndColumn();
201 for (int i = start; i < end; i++) { 215 for (int i = start; i < end; i++) {
202 printf("^"); 216 printf("^");
203 } 217 }
204 printf("\n"); 218 printf("\n");
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 return 0; 569 return 0;
556 } 570 }
557 571
558 572
559 } // namespace v8 573 } // namespace v8
560 574
561 575
562 int main(int argc, char* argv[]) { 576 int main(int argc, char* argv[]) {
563 return v8::Shell::Main(argc, argv); 577 return v8::Shell::Main(argc, argv);
564 } 578 }
OLDNEW
« no previous file with comments | « samples/shell.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698