Fix null dereference in helpers.getPreferredLineEnding when running in browser (platform unknown, process object not available) and input string does not contain line endings.

This commit is contained in:
David Anson 2022-05-04 22:09:11 -07:00
parent 580b7ed020
commit 295e481552
4 changed files with 51 additions and 3 deletions

View file

@ -25,7 +25,7 @@ module.exports = webpackEmptyContext;
/*!*****************************!*\
!*** ../helpers/helpers.js ***!
\*****************************/
/***/ ((module) => {
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
"use strict";
// @ts-check
@ -774,6 +774,17 @@ function emphasisMarkersInContent(params) {
return byLine;
}
module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
/**
* Gets the platform identifier from a string or process-like object.
*
* @param {string} platform Platform identifier (process.platform).
* @param {Object} process Node.js process object.
* @returns {string} Platform identifier.
*/
function getPlatformIdentifier(platform, process) {
return (platform || (process && process.platform) || "unknown");
}
module.exports.getPlatformIdentifier = getPlatformIdentifier;
/**
* Gets the most common line ending, falling back to the platform default.
*
@ -803,7 +814,10 @@ function getPreferredLineEnding(input, platform) {
var preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding =
((platform || process.platform) === "win32") ? "\r\n" : "\n";
// eslint-disable-next-line node/prefer-global/process
(getPlatformIdentifier(platform, __webpack_require__(/*! process */ "?4c74")) === "win32") ?
"\r\n" :
"\n";
}
else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n";
@ -1013,6 +1027,16 @@ module.exports = markdownit;
/***/ }),
/***/ "?4c74":
/*!*************************!*\
!*** process (ignored) ***!
\*************************/
/***/ (() => {
/* (ignored) */
/***/ }),
/***/ "?ec0a":
/*!********************!*\
!*** fs (ignored) ***!

View file

@ -45,6 +45,7 @@ function config(options) {
"fallback": {
"fs": false,
"path": false,
"process": false,
"util": false
}
}

View file

@ -789,6 +789,18 @@ function emphasisMarkersInContent(params) {
}
module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
/**
* Gets the platform identifier from a string or process-like object.
*
* @param {string} platform Platform identifier (process.platform).
* @param {Object} process Node.js process object.
* @returns {string} Platform identifier.
*/
function getPlatformIdentifier(platform, process) {
return (platform || (process && process.platform) || "unknown");
}
module.exports.getPlatformIdentifier = getPlatformIdentifier;
/**
* Gets the most common line ending, falling back to the platform default.
*
@ -818,7 +830,10 @@ function getPreferredLineEnding(input, platform) {
let preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding =
((platform || process.platform) === "win32") ? "\r\n" : "\n";
// eslint-disable-next-line node/prefer-global/process
(getPlatformIdentifier(platform, require("process")) === "win32") ?
"\r\n" :
"\n";
} else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n";
} else if (crlf >= cr) {

View file

@ -390,6 +390,14 @@ test("forEachInlineCodeSpan", (t) => {
});
});
test("getPlatformIdentifier", (t) => {
t.plan(4);
t.is(helpers.getPlatformIdentifier("custom", process), "custom");
t.is(helpers.getPlatformIdentifier("custom", null), "custom");
t.is(helpers.getPlatformIdentifier(null, process), process.platform);
t.is(helpers.getPlatformIdentifier(null, null), "unknown");
});
test("getPreferredLineEnding", (t) => {
t.plan(19);
const testCases = [