Skip to content

Commit

Permalink
intl: Don't crash if v8BreakIterator not available
Browse files Browse the repository at this point in the history
If the undocumented v8BreakIterator does not have data available,
monkeypatch it to throw an error instead of crashing.

Fixes: #3111
Ref: #9008
PR-URL: #4253
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
srl295 authored and rvagg committed Oct 18, 2016
1 parent 9c30a0f commit d5d69fd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2858,6 +2858,13 @@ void SetupProcessObject(Environment* env,
READONLY_PROPERTY(versions,
"icu",
OneByteString(env->isolate(), U_ICU_VERSION));

if (icu_data_dir != nullptr) {
// Did the user attempt (via env var or parameter) to set an ICU path?
READONLY_PROPERTY(process,
"icu_data_dir",
OneByteString(env->isolate(), icu_data_dir));
}
#endif

const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
Expand Down
16 changes: 16 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,22 @@
process.config = JSON.parse(config, function(key, value) {
if (value === 'true') return true;
if (value === 'false') return false;

// Intl.v8BreakIterator() would crash w/ fatal error, so throw instead.
if (value.icu_small &&
global.hasOwnProperty('Intl') &&
Intl.hasOwnProperty('v8BreakIterator') &&
!process.icu_data_dir) {
const des = Object.getOwnPropertyDescriptor(Intl, 'v8BreakIterator');
des.value = function v8BreakIterator() {
throw new Error('v8BreakIterator: full ICU data not installed. ' +
'See https://github.com/nodejs/node/wiki/Intl');
};
Object.defineProperty(Intl, 'v8BreakIterator', des);
}
// Don’t let icu_data_dir leak through.
delete process.icu_data_dir;

return value;
});
};
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-intl-v8BreakIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
require('../common');
const assert = require('assert');

if (global.Intl === undefined || Intl.v8BreakIterator === undefined) {
return console.log('1..0 # Skipped: no Intl');
}

try {
new Intl.v8BreakIterator();
// May succeed if data is available - OK
} catch (e) {
// May throw this error if ICU data is not available - OK
assert.throws(() => new Intl.v8BreakIterator(), /ICU data/);
}

0 comments on commit d5d69fd

Please sign in to comment.