From ed204fe20027a70dd3dc7628e46f07ea00da04c6 Mon Sep 17 00:00:00 2001 From: Alec Grieser Date: Fri, 23 Jun 2017 10:14:28 -0700 Subject: [PATCH] added explicit float casting to node C++ extension to avoid undefined behavior --- bindings/nodejs/binding.gyp | 2 +- bindings/nodejs/lib/tuple.js | 5 ++-- bindings/nodejs/src/FdbUtil.cpp | 43 ++++++++++++++++++++++++++++ bindings/nodejs/src/FdbUtil.h | 29 +++++++++++++++++++ bindings/nodejs/src/FdbV8Wrapper.cpp | 2 ++ bindings/nodejs/tests/tuple_test.js | 25 +++++++++++++++- 6 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 bindings/nodejs/src/FdbUtil.cpp create mode 100644 bindings/nodejs/src/FdbUtil.h diff --git a/bindings/nodejs/binding.gyp b/bindings/nodejs/binding.gyp index d24034df76..6771942577 100644 --- a/bindings/nodejs/binding.gyp +++ b/bindings/nodejs/binding.gyp @@ -2,7 +2,7 @@ 'targets': [ { 'target_name': 'fdblib', - 'sources': [ 'src/FdbV8Wrapper.cpp', 'src/Database.cpp', 'src/Transaction.cpp', 'src/Cluster.cpp', 'src/FdbError.cpp', 'src/FdbOptions.cpp', 'src/FdbOptions.g.cpp' ], + 'sources': [ 'src/FdbV8Wrapper.cpp', 'src/Database.cpp', 'src/Transaction.cpp', 'src/Cluster.cpp', 'src/FdbError.cpp', 'src/FdbOptions.cpp', 'src/FdbOptions.g.cpp', 'src/FdbUtil.cpp' ], 'include_dirs': ['../c'], 'conditions': [ ['OS=="linux"', { diff --git a/bindings/nodejs/lib/tuple.js b/bindings/nodejs/lib/tuple.js index 1fe1d1dcc5..5758791407 100644 --- a/bindings/nodejs/lib/tuple.js +++ b/bindings/nodejs/lib/tuple.js @@ -24,6 +24,7 @@ var assert = require('assert'); var buffer = require('./bufferConversion'); var fdbUtil = require('./fdbUtil'); +var fdb = require('./fdbModule'); var FDBError = require('./error'); var sizeLimits = new Array(8); @@ -83,7 +84,7 @@ function Float(value) { return this.rawData; } else { var buf = new Buffer(4); - buf.writeFloatBE(this.value, 0); + buf.writeFloatBE(fdb.toFloat(this.value), 0); return buf; } }; @@ -230,7 +231,7 @@ function encode(item, buf, pos) { if (isNaN(item.value) && item.rawData !== undefined) { item.rawData.copy(outBuf, 1, 0, 4); } else { - outBuf.writeFloatBE(item.value, 1); + outBuf.writeFloatBE(fdb.toFloat(item.value), 1); } adjustFloat(outBuf, 1, true); return outBuf; diff --git a/bindings/nodejs/src/FdbUtil.cpp b/bindings/nodejs/src/FdbUtil.cpp new file mode 100644 index 0000000000..d95910a9e7 --- /dev/null +++ b/bindings/nodejs/src/FdbUtil.cpp @@ -0,0 +1,43 @@ +/* + * FdbUtil.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include "FdbUtil.h" + +using namespace v8; + +Handle ToFloat(const Arguments &args) { + HandleScope scope; + + if (args.Length() != 1) { + return ThrowException(Exception::TypeError(String::NewSymbol("Wrong number of arguments (must be exactly 1)"))); + } + + if (!args[0]->IsNumber()) { + return ThrowException(Exception::TypeError(String::NewSymbol("Argument is not a Number"))); + } + + float value = (float)args[0]->NumberValue(); + Handle jsValue = Number::New(value); + + return scope.Close(jsValue); +} diff --git a/bindings/nodejs/src/FdbUtil.h b/bindings/nodejs/src/FdbUtil.h new file mode 100644 index 0000000000..a8ad05a01a --- /dev/null +++ b/bindings/nodejs/src/FdbUtil.h @@ -0,0 +1,29 @@ +/* + * FdbUtil.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_FDB_UTIL_H +#define FDB_NODE_FDB_UTIL_H + +#include + +v8::Handle ToFloat(const v8::Arguments &args); + +#endif diff --git a/bindings/nodejs/src/FdbV8Wrapper.cpp b/bindings/nodejs/src/FdbV8Wrapper.cpp index 4b7eea8371..659b26a0a9 100644 --- a/bindings/nodejs/src/FdbV8Wrapper.cpp +++ b/bindings/nodejs/src/FdbV8Wrapper.cpp @@ -33,6 +33,7 @@ #include "Version.h" #include "FdbError.h" #include "FdbOptions.h" +#include "FdbUtil.h" uv_thread_t fdbThread; @@ -140,6 +141,7 @@ void init(Handle target){ target->Set(String::NewSymbol("options"), FdbOptions::CreateOptions(FdbOptions::NetworkOption)); target->Set(String::NewSymbol("streamingMode"), FdbOptions::CreateEnum(FdbOptions::StreamingMode)); target->Set(String::NewSymbol("atomic"), FdbOptions::CreateOptions(FdbOptions::MutationType)); + target->Set(String::NewSymbol("toFloat"), FunctionTemplate::New(ToFloat)->GetFunction()); } #if NODE_VERSION_AT_LEAST(0, 8, 0) diff --git a/bindings/nodejs/tests/tuple_test.js b/bindings/nodejs/tests/tuple_test.js index e430c964c4..56efde8d78 100755 --- a/bindings/nodejs/tests/tuple_test.js +++ b/bindings/nodejs/tests/tuple_test.js @@ -18,7 +18,8 @@ * limitations under the License. */ -var fdb = require('../lib/fdb.js').apiVersion(200); +var fdb = require('../lib/fdb.js').apiVersion(500); +var fdbModule = require('../lib/fdbModule.js'); console.log(fdb.tuple.pack([-Math.pow(2,53)])); console.log(fdb.tuple.pack([-Math.pow(2,53)+1])); @@ -76,3 +77,25 @@ tuples = [ ]; tuples.sort(fdb.tuple.compare); console.log(tuples); + +// Float overruns. +const floats = [ 2.037036e90, -2.037036e90, 4.9090935e-91, -4.9090935e-91, 2.345624805922133125e14, -2.345624805922133125e14 ]; +for (var i = 0; i < floats.length; i++) { + var f = floats[i]; + console.log(f + " -> " + fdb.tuple.Float.fromBytes((new fdb.tuple.Float(f)).toBytes()).value); +} + +// Float type errors. +try { + console.log((new fdb.tuple.Float("asdf")).toBytes()); +} catch (e) { + console.log("Caught!"); + console.log(e); +} + +try { + console.log(fdbModule.toFloat(3.14, 2.718)); +} catch (e) { + console.log("Caught!"); + console.log(e); +}