From fa3e1096d4d97ed2ad1daa373bfecd209ae3ecc2 Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Wed, 7 Aug 2013 20:19:45 -0400 Subject: [PATCH] Bug 891110 - Detect attempts to pass undefined in declareFFI. r=yoric --- .../modules/osfile_shared_allthreads.jsm | 7 ++++++- .../tests/mochi/worker_test_osfile_unix.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/toolkit/components/osfile/modules/osfile_shared_allthreads.jsm b/toolkit/components/osfile/modules/osfile_shared_allthreads.jsm index fc64e94a540b..c9f7b6b219f3 100644 --- a/toolkit/components/osfile/modules/osfile_shared_allthreads.jsm +++ b/toolkit/components/osfile/modules/osfile_shared_allthreads.jsm @@ -885,7 +885,12 @@ let declareFFI = function declareFFI(lib, symbol, abi, } try { let fun = lib.declare.apply(lib, signature); - let result = function ffi(/*arguments*/) { + let result = function ffi(...args) { + for (let i = 0; i < args.length; i++) { + if (typeof args[i] == "undefined") { + throw new TypeError("Argument " + i + " of " + symbol + " is undefined"); + } + } let result = fun.apply(fun, arguments); return returnType.importFromC(result, symbol); }; diff --git a/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js b/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js index 356245e2a757..f6523a848d11 100644 --- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js +++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js @@ -12,6 +12,7 @@ self.onmessage = function(msg) { test_getcwd(); test_open_close(); test_create_file(); + test_passing_undefined(); test_access(); test_read_write(); finish(); @@ -36,6 +37,23 @@ function test_open_close() { is(ctypes.errno, OS.Constants.libc.ENOENT, "test_open_close: error is ENOENT"); } +function test_passing_undefined() +{ + info("Testing that an exception gets thrown when an FFI function is passed undefined"); + let exceptionRaised = false; + + try { + let file = OS.Unix.File.open(undefined, OS.Constants.libc.O_RDWR + | OS.Constants.libc.O_CREAT + | OS.Constants.libc.O_TRUNC, + OS.Constants.libc.S_IRWXU); + } catch(e if e instanceof TypeError) { + exceptionRaised = true; + } + + ok(exceptionRaised, "test_passing_undefined: exception gets thrown") +} + function test_create_file() { info("Starting test_create_file");