GeezerVison is showing github.com — buffer: fix out of range for toString · nodejs/node@1fb67af
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/buffer.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -843,12 +843,12 @@ Buffer.prototype.toString = function toString(encoding, start, end) { |
843 | 843 | else if (start >= len) |
844 | 844 | return ''; |
845 | 845 | else |
846 | | - start |= 0; |
| 846 | + start = MathTrunc(start) || 0; |
847 | 847 |
|
848 | 848 | if (end === undefined || end > len) |
849 | 849 | end = len; |
850 | 850 | else |
851 | | - end |= 0; |
| 851 | + end = MathTrunc(end) || 0; |
852 | 852 |
|
853 | 853 | if (end <= start) |
854 | 854 | return ''; |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-buffer-tostring-range.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -require('../common'); |
| 3 | +const common = require('../common'); |
4 | 4 | const assert = require('assert'); |
5 | 5 |
|
6 | 6 | const rangeBuffer = Buffer.from('abc'); |
@@ -98,3 +98,11 @@ assert.throws(() => { |
98 | 98 | name: 'TypeError', |
99 | 99 | message: 'Unknown encoding: null' |
100 | 100 | }); |
| 101 | + |
| 102 | +// Must not throw when start and end are within kMaxLength |
| 103 | +// Cannot test on 32bit machine as we are testing the case |
| 104 | +// when start and end are above the threshold |
| 105 | +common.skipIf32Bits(); |
| 106 | +const threshold = 0xFFFFFFFF; |
| 107 | +const largeBuffer = Buffer.alloc(threshold + 20); |
| 108 | +largeBuffer.toString('utf8', threshold, threshold + 20); |