@@ -17,29 +17,29 @@ SlowBuffer.prototype.inspect = function () {
1717} ;
1818
1919
20- SlowBuffer . prototype . toString = function ( encoding , start , stop ) {
20+ SlowBuffer . prototype . toString = function ( encoding , start , end ) {
2121 encoding = String ( encoding || 'utf8' ) . toLowerCase ( ) ;
2222 start = + start || 0 ;
23- if ( typeof stop == "undefined" ) stop = this . length ;
23+ if ( typeof end == "undefined" ) end = this . length ;
2424
2525 // Fastpath empty strings
26- if ( + stop == start ) {
26+ if ( + end == start ) {
2727 return '' ;
2828 }
2929
3030 switch ( encoding ) {
3131 case 'utf8' :
3232 case 'utf-8' :
33- return this . utf8Slice ( start , stop ) ;
33+ return this . utf8Slice ( start , end ) ;
3434
3535 case 'ascii' :
36- return this . asciiSlice ( start , stop ) ;
36+ return this . asciiSlice ( start , end ) ;
3737
3838 case 'binary' :
39- return this . binarySlice ( start , stop ) ;
39+ return this . binarySlice ( start , end ) ;
4040
4141 case 'base64' :
42- return this . base64Slice ( start , stop ) ;
42+ return this . base64Slice ( start , end ) ;
4343
4444 default :
4545 throw new Error ( 'Unknown encoding' ) ;
@@ -212,14 +212,19 @@ Buffer.prototype.write = function write (string, offset, encoding) {
212212
213213
214214// toString(encoding, start=0, end=buffer.length)
215- Buffer . prototype . toString = function toString ( encoding , start , end ) {
216- encoding || ( encoding = 'utf8' ) ;
217- start || ( start = 0 ) ;
218- end || ( end = this . length ) ;
215+ Buffer . prototype . toString = function ( encoding , start , end ) {
216+ if ( typeof encoding == 'undefined' ) encoding = 'utf8' ;
219217
220- // Make sure we aren't oob
221- if ( end > this . length ) {
218+ if ( typeof start == 'undefined' || start < 0 ) {
219+ start = 0 ;
220+ } else if ( start > this . length ) {
221+ start = this . length ;
222+ }
223+
224+ if ( typeof end == "undefined" || end > this . length ) {
222225 end = this . length ;
226+ } else if ( end < 0 ) {
227+ end = 0 ;
223228 }
224229
225230 return this . parent . toString ( encoding , start + this . offset , end + this . offset ) ;
@@ -232,14 +237,37 @@ Buffer.byteLength = SlowBuffer.byteLength;
232237
233238// copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
234239Buffer . prototype . copy = function copy ( target , target_start , start , end ) {
240+ var source = this ;
235241 start || ( start = 0 ) ;
236242 end || ( end = this . length ) ;
237243
244+ if ( end < start ) throw new Error ( "sourceEnd < sourceStart" ) ;
245+
246+ // Copy 0 bytes; we're done
247+ if ( end === start ) return 0 ;
248+ if ( target . length == 0 || source . length == 0 ) return 0 ;
249+
250+ if ( target_start < 0 || target_start >= target . length ) {
251+ throw new Error ( "targetStart out of bounds" ) ;
252+ }
253+
254+ if ( start < 0 || start >= source . length ) {
255+ throw new Error ( "sourceStart out of bounds" ) ;
256+ }
257+
258+ if ( end < 0 || end > source . length ) {
259+ throw new Error ( "sourceEnd out of bounds" ) ;
260+ }
261+
238262 // Are we oob?
239263 if ( end > this . length ) {
240264 end = this . length ;
241265 }
242266
267+ if ( target . length - target_start < end - start ) {
268+ end = target . length - target_start + start ;
269+ }
270+
243271 return this . parent . copy ( target . parent ,
244272 target_start + target . offset ,
245273 start + this . offset ,
0 commit comments