1'use strict';
2
3// This is a regression test for some scenarios in which node would pass
4// unsanitized user input to a printf-like formatting function when dlopen
5// fails, potentially crashing the process.
6
7const common = require('../common');
8const tmpdir = require('../common/tmpdir');
9tmpdir.refresh();
10
11const assert = require('assert');
12const fs = require('fs');
13
14// This error message should not be passed to a printf-like function.
15assert.throws(() => {
16  process.dlopen({ exports: {} }, 'foo-%s.node');
17}, ({ name, code, message }) => {
18  assert.strictEqual(name, 'Error');
19  assert.strictEqual(code, 'ERR_DLOPEN_FAILED');
20  if (!common.isAIX && !common.isIBMi) {
21    assert.match(message, /foo-%s\.node/);
22  }
23  return true;
24});
25
26const notBindingDir = 'test/addons/not-a-binding';
27const notBindingPath = `${notBindingDir}/build/Release/binding.node`;
28const strangeBindingPath = `${tmpdir.path}/binding-%s.node`;
29// Ensure that the addon directory exists, but skip the remainder of the test if
30// the addon has not been compiled.
31fs.accessSync(notBindingDir);
32try {
33  fs.copyFileSync(notBindingPath, strangeBindingPath);
34} catch (err) {
35  if (err.code !== 'ENOENT') throw err;
36  common.skip(`addon not found: ${notBindingPath}`);
37}
38
39// This error message should also not be passed to a printf-like function.
40assert.throws(() => {
41  process.dlopen({ exports: {} }, strangeBindingPath);
42}, {
43  name: 'Error',
44  code: 'ERR_DLOPEN_FAILED',
45  message: /^Module did not self-register: '.*binding-%s\.node'\.$/
46});
47