Skip to content

Commit

Permalink
fix crash on unknown timezone in tz helper
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Mar 17, 2017
1 parent d184883 commit 197648a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/node_cctz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ NAN_METHOD(load_time_zone) {
return;
}

std::unique_ptr<Nan::Persistent<v8::Object>> persist(new Nan::Persistent<v8::Object>(TimeZone::NewInstance(info[0])));
auto tz = TimeZone::NewInstance(info[0]);
if (tz.IsEmpty()) {
return;
}

std::unique_ptr<Nan::Persistent<v8::Object>> persist(new Nan::Persistent<v8::Object>(tz.ToLocalChecked()));
info.GetReturnValue().Set(Nan::New(*persist));
time_zone_map[name] = std::move(persist);
}
Expand Down
8 changes: 4 additions & 4 deletions src/timezone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ NAN_METHOD(TimeZone::New) {
info.GetReturnValue().Set(info.This());
}

v8::Local<v8::Object> TimeZone::NewInstance() {
v8::MaybeLocal<v8::Object> TimeZone::NewInstance() {
v8::Local<v8::Function> cons = Nan::New(constructor);
return Nan::NewInstance(cons).ToLocalChecked();
return Nan::NewInstance(cons);
}

v8::Local<v8::Object> TimeZone::NewInstance(v8::Local<v8::Value> arg) {
v8::MaybeLocal<v8::Object> TimeZone::NewInstance(v8::Local<v8::Value> arg) {
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { arg };
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);

return Nan::NewInstance(cons, argc, argv).ToLocalChecked();
return Nan::NewInstance(cons, argc, argv);
}
4 changes: 2 additions & 2 deletions src/timezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class TimeZone : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
static v8::Local<v8::Object> NewInstance();
static v8::Local<v8::Object> NewInstance(v8::Local<v8::Value> arg);
static v8::MaybeLocal<v8::Object> NewInstance();
static v8::MaybeLocal<v8::Object> NewInstance(v8::Local<v8::Value> arg);
static Nan::Persistent<v8::FunctionTemplate> prototype;

cctz::time_zone value;
Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ test('TimeZone has name getter', t => {
t.is(new TimeZone('UTC').name, 'UTC');
});

test('tz shortcut workd', t => {
t.throws(() => tz('unknown'), 'Failed to load time zone unknown');
t.is(tz('UTC').name, 'UTC');
});

test('now shortcut works', t => {
t.is(now(), Math.floor(Date.now() / 1000));
});

0 comments on commit 197648a

Please sign in to comment.