Skip to content

Commit 2e29b76

Browse files
srl295MylesBorins
authored andcommitted
intl: 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. Fixes: #3111 Ref: #9008 PR-URL: #4253 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 2a66ddb commit 2e29b76

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/node.cc

+7
Original file line numberDiff line numberDiff line change
@@ -2858,6 +2858,13 @@ void SetupProcessObject(Environment* env,
28582858
READONLY_PROPERTY(versions,
28592859
"icu",
28602860
OneByteString(env->isolate(), U_ICU_VERSION));
2861+
2862+
if (icu_data_dir != nullptr) {
2863+
// Did the user attempt (via env var or parameter) to set an ICU path?
2864+
READONLY_PROPERTY(process,
2865+
"icu_data_dir",
2866+
OneByteString(env->isolate(), icu_data_dir));
2867+
}
28612868
#endif
28622869

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

src/node.js

+16
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
};
+15
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)