added explicit float casting to node C++ extension to avoid undefined behavior

This commit is contained in:
Alec Grieser 2017-06-23 10:14:28 -07:00
parent cefaa2391d
commit ed204fe200
6 changed files with 102 additions and 4 deletions

View File

@ -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"', {

View File

@ -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;

View File

@ -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 <node.h>
#include <node_buffer.h>
#include "FdbUtil.h"
using namespace v8;
Handle<Value> 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<Value> jsValue = Number::New(value);
return scope.Close(jsValue);
}

View File

@ -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 <node.h>
v8::Handle<v8::Value> ToFloat(const v8::Arguments &args);
#endif

View File

@ -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<Object> 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)

View File

@ -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);
}