Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix erroneous fallthrough in ParseEncoding() #7262

Merged
merged 2 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,15 @@ ADDONS_BINDING_GYPS := \
$(filter-out test/addons/??_*/binding.gyp, \
$(wildcard test/addons/*/binding.gyp))

ADDONS_BINDING_SOURCES := \
$(filter-out test/addons/??_*/*.cc, $(wildcard test/addons/*/*.cc)) \
$(filter-out test/addons/??_*/*.h, $(wildcard test/addons/*/*.h))

# Implicitly depends on $(NODE_EXE), see the build-addons rule for rationale.
# Depends on node-gyp package.json so that build-addons is (re)executed when
# node-gyp is updated as part of an npm update.
test/addons/.buildstamp: deps/npm/node_modules/node-gyp/package.json \
$(ADDONS_BINDING_GYPS) \
$(ADDONS_BINDING_GYPS) $(ADDONS_BINDING_SOURCES) \
deps/uv/include/*.h deps/v8/include/*.h \
src/node.h src/node_buffer.h src/node_object_wrap.h \
test/addons/.docbuildstamp
Expand Down
1 change: 1 addition & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ enum encoding ParseEncoding(const char* encoding,
if (strncmp(encoding + 2, "tin1", 4) == 0)
return LATIN1;
}
break;
case 'b':
// binary
if (encoding[1] == 'i') {
Expand Down
36 changes: 36 additions & 0 deletions test/addons/parse-encoding/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "node.h"
#include "v8.h"

namespace {

#define ENCODING_MAP(V) \
V(ASCII) \
V(BASE64) \
V(BUFFER) \
V(HEX) \
V(LATIN1) \
V(UCS2) \
V(UTF8) \

void ParseEncoding(const v8::FunctionCallbackInfo<v8::Value>& args) {
const node::encoding encoding =
node::ParseEncoding(args.GetIsolate(), args[0],
static_cast<node::encoding>(-1));
const char* encoding_name = "UNKNOWN";
#define V(name) if (encoding == node::name) encoding_name = #name;
ENCODING_MAP(V)
#undef V
auto encoding_string =
v8::String::NewFromUtf8(args.GetIsolate(), encoding_name,
v8::NewStringType::kNormal)
.ToLocalChecked();
args.GetReturnValue().Set(encoding_string);
}

void Initialize(v8::Local<v8::Object> target) {
NODE_SET_METHOD(target, "parseEncoding", ParseEncoding);
}

} // anonymous namespace

NODE_MODULE(binding, Initialize);
9 changes: 9 additions & 0 deletions test/addons/parse-encoding/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
'targets': [
{
'target_name': 'binding',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [ 'binding.cc' ]
}
]
}
19 changes: 19 additions & 0 deletions test/addons/parse-encoding/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

require('../../common');
const assert = require('assert');
const { parseEncoding } = require('./build/Release/binding');

assert.strictEqual(parseEncoding(''), 'UNKNOWN');

assert.strictEqual(parseEncoding('ascii'), 'ASCII');
assert.strictEqual(parseEncoding('base64'), 'BASE64');
assert.strictEqual(parseEncoding('binary'), 'LATIN1');
assert.strictEqual(parseEncoding('buffer'), 'BUFFER');
assert.strictEqual(parseEncoding('hex'), 'HEX');
assert.strictEqual(parseEncoding('latin1'), 'LATIN1');
assert.strictEqual(parseEncoding('ucs2'), 'UCS2');
assert.strictEqual(parseEncoding('utf8'), 'UTF8');

assert.strictEqual(parseEncoding('linary'), 'UNKNOWN');
assert.strictEqual(parseEncoding('luffer'), 'UNKNOWN');