Skip to content

Commit 3f99517

Browse files
authored
Merge pull request webpack#6905 from xtuc/fix-handle-unknown-size
SizeFormatHelpers: defend against invalid sizes
2 parents aee2491 + 7b6f56e commit 3f99517

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/SizeFormatHelpers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
const SizeFormatHelpers = exports;
88

99
SizeFormatHelpers.formatSize = size => {
10+
if (typeof size !== "number" || Number.isNaN(size) === true) {
11+
return "unknown size";
12+
}
13+
1014
if (size <= 0) {
1115
return "0 bytes";
1216
}

test/SizeFormatHelpers.unittest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@ describe("SizeFormatHelpers", () => {
4343
"1.2 GiB"
4444
);
4545
});
46+
47+
it("should handle undefined/NaN", () => {
48+
should(SizeFormatHelpers.formatSize(undefined)).be.eql("unknown size");
49+
should(SizeFormatHelpers.formatSize(NaN)).be.eql("unknown size");
50+
});
4651
});
4752
});

0 commit comments

Comments
 (0)