Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Verify that the argument passed to vm.runInContext() is a context obj…

…ect.

Fixes #558.
  • Loading branch information...
commit 06c9a4ec754430962141276ac4c7ec517ca64161 1 parent c0d3f1f
Ben Noordhuis authored July 05, 2011
10  src/node_script.cc
@@ -50,6 +50,7 @@ class WrappedContext : ObjectWrap {
50 50
 
51 51
   Persistent<Context> GetV8Context();
52 52
   static Local<Object> NewInstance();
  53
+  static bool InstanceOf(Handle<Value> value);
53 54
 
54 55
  protected:
55 56
 
@@ -110,6 +111,11 @@ void WrappedContext::Initialize(Handle<Object> target) {
110 111
 }
111 112
 
112 113
 
  114
+bool WrappedContext::InstanceOf(Handle<Value> value) {
  115
+  return !value.IsEmpty() && constructor_template->HasInstance(value);
  116
+}
  117
+
  118
+
113 119
 Handle<Value> WrappedContext::New(const Arguments& args) {
114 120
   HandleScope scope;
115 121
 
@@ -282,7 +288,9 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) {
282 288
   }
283 289
 
284 290
   const int sandbox_index = input_flag == compileCode ? 1 : 0;
285  
-  if (context_flag == userContext && args.Length() < (sandbox_index + 1)) {
  291
+  if (context_flag == userContext
  292
+    && !WrappedContext::InstanceOf(args[sandbox_index]))
  293
+  {
286 294
     return ThrowException(Exception::TypeError(
287 295
           String::New("needs a 'context' argument.")));
288 296
   }
13  test/simple/test-script-context.js
@@ -22,7 +22,8 @@
22 22
 var common = require('../common');
23 23
 var assert = require('assert');
24 24
 
25  
-var Script = require('vm').Script;
  25
+var vm = require('vm');
  26
+var Script = vm.Script;
26 27
 var script = new Script('"passed";');
27 28
 
28 29
 common.debug('run in a new empty context');
@@ -44,3 +45,13 @@ assert.equal('lala', context.thing);
44 45
 
45 46
 // Issue GH-227:
46 47
 Script.runInNewContext('', null, 'some.js');
  48
+
  49
+// GH-558, non-context argument segfaults / raises assertion
  50
+function isTypeError(o) {
  51
+  return o instanceof TypeError;
  52
+}
  53
+
  54
+[undefined, null, 0, 0.0, '', {}, []].forEach(function(e) {
  55
+  assert.throws(function() { script.runInContext(e); }, isTypeError);
  56
+  assert.throws(function() { vm.runInContext('', e); }, isTypeError);
  57
+});

0 notes on commit 06c9a4e

Please sign in to comment.
Something went wrong with that request. Please try again.