Add explicit null checks after string conversions in the shells.
This fixes issue 224. Review URL: http://codereview.chromium.org/20081 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1233 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
b0e3ee6274
commit
05a56bf1ea
@ -859,7 +859,7 @@ class EXPORT String : public Primitive {
|
||||
public:
|
||||
explicit Utf8Value(Handle<v8::Value> obj);
|
||||
~Utf8Value();
|
||||
char* operator*() { return str_; }
|
||||
char* operator*() const { return str_; }
|
||||
int length() { return length_; }
|
||||
private:
|
||||
char* str_;
|
||||
@ -878,7 +878,7 @@ class EXPORT String : public Primitive {
|
||||
public:
|
||||
explicit AsciiValue(Handle<v8::Value> obj);
|
||||
~AsciiValue();
|
||||
char* operator*() { return str_; }
|
||||
char* operator*() const { return str_; }
|
||||
int length() { return length_; }
|
||||
private:
|
||||
char* str_;
|
||||
@ -896,7 +896,7 @@ class EXPORT String : public Primitive {
|
||||
public:
|
||||
explicit Value(Handle<v8::Value> obj);
|
||||
~Value();
|
||||
uint16_t* operator*() { return str_; }
|
||||
uint16_t* operator*() const { return str_; }
|
||||
int length() { return length_; }
|
||||
private:
|
||||
uint16_t* str_;
|
||||
|
@ -99,6 +99,12 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
|
||||
// Extracts a C string from a V8 Utf8Value.
|
||||
const char* ToCString(const v8::String::Utf8Value& value) {
|
||||
return *value ? *value : "<string conversion failed>";
|
||||
}
|
||||
|
||||
|
||||
// The callback that is invoked by v8 whenever the JavaScript 'print'
|
||||
// function is called. Prints its arguments on stdout separated by
|
||||
// spaces and ending with a newline.
|
||||
@ -112,7 +118,8 @@ v8::Handle<v8::Value> Print(const v8::Arguments& args) {
|
||||
printf(" ");
|
||||
}
|
||||
v8::String::Utf8Value str(args[i]);
|
||||
printf("%s", *str);
|
||||
const char* cstr = ToCString(str);
|
||||
printf("%s", cstr);
|
||||
}
|
||||
printf("\n");
|
||||
return v8::Undefined();
|
||||
@ -126,6 +133,9 @@ v8::Handle<v8::Value> Load(const v8::Arguments& args) {
|
||||
for (int i = 0; i < args.Length(); i++) {
|
||||
v8::HandleScope handle_scope;
|
||||
v8::String::Utf8Value file(args[i]);
|
||||
if (*file == NULL) {
|
||||
return v8::ThrowException(v8::String::New("Error loading file"));
|
||||
}
|
||||
v8::Handle<v8::String> source = ReadFile(*file);
|
||||
if (source.IsEmpty()) {
|
||||
return v8::ThrowException(v8::String::New("Error loading file"));
|
||||
@ -220,7 +230,8 @@ bool ExecuteString(v8::Handle<v8::String> source,
|
||||
// If all went well and the result wasn't undefined then print
|
||||
// the returned value.
|
||||
v8::String::Utf8Value str(result);
|
||||
printf("%s\n", *str);
|
||||
const char* cstr = ToCString(str);
|
||||
printf("%s\n", cstr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -231,19 +242,22 @@ bool ExecuteString(v8::Handle<v8::String> source,
|
||||
void ReportException(v8::TryCatch* try_catch) {
|
||||
v8::HandleScope handle_scope;
|
||||
v8::String::Utf8Value exception(try_catch->Exception());
|
||||
const char* exception_string = ToCString(exception);
|
||||
v8::Handle<v8::Message> message = try_catch->Message();
|
||||
if (message.IsEmpty()) {
|
||||
// V8 didn't provide any extra information about this error; just
|
||||
// print the exception.
|
||||
printf("%s\n", *exception);
|
||||
printf("%s\n", exception_string);
|
||||
} else {
|
||||
// Print (filename):(line number): (message).
|
||||
v8::String::Utf8Value filename(message->GetScriptResourceName());
|
||||
const char* filename_string = ToCString(filename);
|
||||
int linenum = message->GetLineNumber();
|
||||
printf("%s:%i: %s\n", *filename, linenum, *exception);
|
||||
printf("%s:%i: %s\n", filename_string, linenum, exception_string);
|
||||
// Print line of source code.
|
||||
v8::String::Utf8Value sourceline(message->GetSourceLine());
|
||||
printf("%s\n", *sourceline);
|
||||
const char* sourceline_string = ToCString(sourceline);
|
||||
printf("%s\n", sourceline_string);
|
||||
// Print wavy underline (GetUnderline is deprecated).
|
||||
int start = message->GetStartColumn();
|
||||
for (int i = 0; i < start; i++) {
|
||||
|
34
src/d8.cc
34
src/d8.cc
@ -93,6 +93,12 @@ Persistent<Context> Shell::utility_context_;
|
||||
Persistent<Context> Shell::evaluation_context_;
|
||||
|
||||
|
||||
// Converts a V8 value to a C string.
|
||||
const char* ToCString(const v8::String::Utf8Value& value) {
|
||||
return *value ? *value : "<string conversion failed>";
|
||||
}
|
||||
|
||||
|
||||
// Executes a string within the current v8 context.
|
||||
bool Shell::ExecuteString(Handle<String> source,
|
||||
Handle<Value> name,
|
||||
@ -121,8 +127,9 @@ bool Shell::ExecuteString(Handle<String> source,
|
||||
if (print_result && !result->IsUndefined()) {
|
||||
// If all went well and the result wasn't undefined then print
|
||||
// the returned value.
|
||||
String::Utf8Value str(result);
|
||||
printf("%s\n", *str);
|
||||
v8::String::Utf8Value str(result);
|
||||
const char* cstr = ToCString(str);
|
||||
printf("%s\n", cstr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -139,8 +146,9 @@ Handle<Value> Shell::Print(const Arguments& args) {
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
String::Utf8Value str(args[i]);
|
||||
printf("%s", *str);
|
||||
v8::String::Utf8Value str(args[i]);
|
||||
const char* cstr = ToCString(str);
|
||||
printf("%s", cstr);
|
||||
}
|
||||
printf("\n");
|
||||
return Undefined();
|
||||
@ -151,6 +159,9 @@ Handle<Value> Shell::Load(const Arguments& args) {
|
||||
for (int i = 0; i < args.Length(); i++) {
|
||||
HandleScope handle_scope;
|
||||
String::Utf8Value file(args[i]);
|
||||
if (*file == NULL) {
|
||||
return ThrowException(String::New("Error loading file"));
|
||||
}
|
||||
Handle<String> source = ReadFile(*file);
|
||||
if (source.IsEmpty()) {
|
||||
return ThrowException(String::New("Error loading file"));
|
||||
@ -178,20 +189,23 @@ Handle<Value> Shell::Version(const Arguments& args) {
|
||||
|
||||
void Shell::ReportException(v8::TryCatch* try_catch) {
|
||||
HandleScope handle_scope;
|
||||
String::Utf8Value exception(try_catch->Exception());
|
||||
v8::String::Utf8Value exception(try_catch->Exception());
|
||||
const char* exception_string = ToCString(exception);
|
||||
Handle<Message> message = try_catch->Message();
|
||||
if (message.IsEmpty()) {
|
||||
// V8 didn't provide any extra information about this error; just
|
||||
// print the exception.
|
||||
printf("%s\n", *exception);
|
||||
printf("%s\n", exception_string);
|
||||
} else {
|
||||
// Print (filename):(line number): (message).
|
||||
String::Utf8Value filename(message->GetScriptResourceName());
|
||||
v8::String::Utf8Value filename(message->GetScriptResourceName());
|
||||
const char* filename_string = ToCString(filename);
|
||||
int linenum = message->GetLineNumber();
|
||||
printf("%s:%i: %s\n", *filename, linenum, *exception);
|
||||
printf("%s:%i: %s\n", filename_string, linenum, exception_string);
|
||||
// Print line of source code.
|
||||
String::Utf8Value sourceline(message->GetSourceLine());
|
||||
printf("%s\n", *sourceline);
|
||||
v8::String::Utf8Value sourceline(message->GetSourceLine());
|
||||
const char* sourceline_string = ToCString(sourceline);
|
||||
printf("%s\n", sourceline_string);
|
||||
// Print wavy underline (GetUnderline is deprecated).
|
||||
int start = message->GetStartColumn();
|
||||
for (int i = 0; i < start; i++) {
|
||||
|
Loading…
Reference in New Issue
Block a user