Skip to content

Commit ab3766a

Browse files
committed
intl: (v4.x) Don't crash if v8BreakIterator not available
If the undocumented v8BreakIterator does not have data available, monkeypatch it to throw an error instead of crashing. Backport of #3111 c99f231
1 parent d959279 commit ab3766a

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/node.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,13 @@ void SetupProcessObject(Environment* env,
28552855
READONLY_PROPERTY(versions,
28562856
"icu",
28572857
OneByteString(env->isolate(), U_ICU_VERSION));
2858+
2859+
if (icu_data_dir != nullptr) {
2860+
// Did the user attempt (via env var or parameter) to set an ICU path?
2861+
READONLY_PROPERTY(process,
2862+
"icu_data_dir",
2863+
OneByteString(env->isolate(), icu_data_dir));
2864+
}
28582865
#endif
28592866

28602867
const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);

src/node.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,22 @@
266266
process.config = JSON.parse(config, function(key, value) {
267267
if (value === 'true') return true;
268268
if (value === 'false') return false;
269+
270+
// Intl.v8BreakIterator() would crash w/ fatal error, so throw instead.
271+
if (value.icu_small &&
272+
global.hasOwnProperty('Intl') &&
273+
Intl.hasOwnProperty('v8BreakIterator') &&
274+
!process.icu_data_dir) {
275+
const des = Object.getOwnPropertyDescriptor(Intl, 'v8BreakIterator');
276+
des.value = function v8BreakIterator() {
277+
throw new Error('v8BreakIterator: full ICU data not installed. ' +
278+
'See https://github.com/nodejs/node/wiki/Intl');
279+
};
280+
Object.defineProperty(Intl, 'v8BreakIterator', des);
281+
}
282+
// Don’t let icu_data_dir leak through.
283+
delete process.icu_data_dir;
284+
269285
return value;
270286
});
271287
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
if (global.Intl === undefined || Intl.v8BreakIterator === undefined) {
6+
return console.log('1..0 # Skipped: no Intl');
7+
}
8+
9+
try {
10+
new Intl.v8BreakIterator();
11+
// May succeed if data is available - OK
12+
} catch (e) {
13+
// May throw this error if ICU data is not available - OK
14+
assert.throws(() => new Intl.v8BreakIterator(), /ICU data/);
15+
}

0 commit comments

Comments
 (0)