path: fix normalize paths ending with two dots · nodejs/node@d1bf8ce
@@ -11,10 +11,10 @@ function assertPath(path) {
1111// Resolves . and .. elements in a path with directory names
1212function normalizeStringWin32(path, allowAboveRoot) {
1313var res = '';
14+var lastSegmentLength = 0;
1415var lastSlash = -1;
1516var dots = 0;
1617var code;
17-var isAboveRoot = false;
1818for (var i = 0; i <= path.length; ++i) {
1919if (i < path.length)
2020code = path.charCodeAt(i);
@@ -26,7 +26,7 @@ function normalizeStringWin32(path, allowAboveRoot) {
2626if (lastSlash === i - 1 || dots === 1) {
2727// NOOP
2828} else if (lastSlash !== i - 1 && dots === 2) {
29-if (res.length < 2 || !isAboveRoot ||
29+if (res.length < 2 || lastSegmentLength !== 2 ||
3030res.charCodeAt(res.length - 1) !== 46/*.*/ ||
3131res.charCodeAt(res.length - 2) !== 46/*.*/) {
3232if (res.length > 2) {
@@ -37,20 +37,22 @@ function normalizeStringWin32(path, allowAboveRoot) {
3737break;
3838}
3939if (j !== start) {
40-if (j === -1)
40+if (j === -1) {
4141res = '';
42-else
42+lastSegmentLength = 0;
43+} else {
4344res = res.slice(0, j);
45+lastSegmentLength = j;
46+}
4447lastSlash = i;
4548dots = 0;
46-isAboveRoot = false;
4749continue;
4850}
4951} else if (res.length === 2 || res.length === 1) {
5052res = '';
53+lastSegmentLength = 0;
5154lastSlash = i;
5255dots = 0;
53-isAboveRoot = false;
5456continue;
5557}
5658}
@@ -59,14 +61,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
5961res += '\\..';
6062else
6163res = '..';
62-isAboveRoot = true;
64+lastSegmentLength = 2;
6365}
6466} else {
6567if (res.length > 0)
6668res += '\\' + path.slice(lastSlash + 1, i);
6769else
6870res = path.slice(lastSlash + 1, i);
69-isAboveRoot = false;
71+lastSegmentLength = i - lastSlash - 1;
7072}
7173lastSlash = i;
7274dots = 0;
@@ -82,10 +84,10 @@ function normalizeStringWin32(path, allowAboveRoot) {
8284// Resolves . and .. elements in a path with directory names
8385function normalizeStringPosix(path, allowAboveRoot) {
8486var res = '';
87+var lastSegmentLength = 0;
8588var lastSlash = -1;
8689var dots = 0;
8790var code;
88-var isAboveRoot = false;
8991for (var i = 0; i <= path.length; ++i) {
9092if (i < path.length)
9193code = path.charCodeAt(i);
@@ -97,7 +99,7 @@ function normalizeStringPosix(path, allowAboveRoot) {
9799if (lastSlash === i - 1 || dots === 1) {
98100// NOOP
99101} else if (lastSlash !== i - 1 && dots === 2) {
100-if (res.length < 2 || !isAboveRoot ||
102+if (res.length < 2 || lastSegmentLength !== 2 ||
101103res.charCodeAt(res.length - 1) !== 46/*.*/ ||
102104res.charCodeAt(res.length - 2) !== 46/*.*/) {
103105if (res.length > 2) {
@@ -108,20 +110,22 @@ function normalizeStringPosix(path, allowAboveRoot) {
108110break;
109111}
110112if (j !== start) {
111-if (j === -1)
113+if (j === -1) {
112114res = '';
113-else
115+lastSegmentLength = 0;
116+} else {
114117res = res.slice(0, j);
118+lastSegmentLength = j;
119+}
115120lastSlash = i;
116121dots = 0;
117-isAboveRoot = false;
118122continue;
119123}
120124} else if (res.length === 2 || res.length === 1) {
121125res = '';
126+lastSegmentLength = 0;
122127lastSlash = i;
123128dots = 0;
124-isAboveRoot = false;
125129continue;
126130}
127131}
@@ -130,14 +134,14 @@ function normalizeStringPosix(path, allowAboveRoot) {
130134res += '/..';
131135else
132136res = '..';
133-isAboveRoot = true;
137+lastSegmentLength = 2;
134138}
135139} else {
136140if (res.length > 0)
137141res += '/' + path.slice(lastSlash + 1, i);
138142else
139143res = path.slice(lastSlash + 1, i);
140-isAboveRoot = false;
144+lastSegmentLength = i - lastSlash - 1;
141145}
142146lastSlash = i;
143147dots = 0;