GeezerVison is showing github.com — buffer: combine checking range of sourceStart in `buf.copy` · nodejs/node@e457d89
File treeExpand 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-8Lines changed: 2 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -222,8 +222,8 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) { | 222 | 222 | sourceStart = 0; | 223 | 223 | } else { | 224 | 224 | sourceStart = toInteger(sourceStart, 0); | 225 | | - if (sourceStart < 0) | 226 | | - throw new ERR_OUT_OF_RANGE('sourceStart', '>= 0', sourceStart); | | 225 | + if (sourceStart < 0 || sourceStart > source.length) | | 226 | + throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.length}`, sourceStart); | 227 | 227 | } | 228 | 228 |
| 229 | 229 | if (sourceEnd === undefined) { | @@ -237,12 +237,6 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) { | 237 | 237 | if (targetStart >= target.length || sourceStart >= sourceEnd) | 238 | 238 | return 0; | 239 | 239 |
| 240 | | - if (sourceStart > source.length) { | 241 | | - throw new ERR_OUT_OF_RANGE('sourceStart', | 242 | | - `<= ${source.length}`, | 243 | | - sourceStart); | 244 | | - } | 245 | | - | 246 | 240 | return _copyActual(source, target, targetStart, sourceStart, sourceEnd); | 247 | 241 | } | 248 | 242 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-alloc.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -124,7 +124,7 @@ b.copy(Buffer.alloc(0), 1, 1, 1); | 124 | 124 | b.copy(Buffer.alloc(1), 1, 1, 1); | 125 | 125 |
| 126 | 126 | // Try to copy 0 bytes from past the end of the source buffer | 127 | | -b.copy(Buffer.alloc(1), 0, 2048, 2048); | | 127 | +b.copy(Buffer.alloc(1), 0, 1024, 1024); | 128 | 128 |
| 129 | 129 | // Testing for smart defaults and ability to pass string values as offset | 130 | 130 | { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-copy.js +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -155,8 +155,15 @@ assert.throws( | 155 | 155 | { | 156 | 156 | code: 'ERR_OUT_OF_RANGE', | 157 | 157 | name: 'RangeError', | 158 | | - message: 'The value of "sourceStart" is out of range. ' + | 159 | | - 'It must be >= 0. Received -1' | | 158 | + } | | 159 | +); | | 160 | + | | 161 | +// Copy throws if sourceStart is greater than length of source | | 162 | +assert.throws( | | 163 | + () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, 100), | | 164 | + { | | 165 | + code: 'ERR_OUT_OF_RANGE', | | 166 | + name: 'RangeError', | 160 | 167 | } | 161 | 168 | ); | 162 | 169 |
| |
|