@@ -352,6 +352,67 @@ describe('lib/decoder', () => {
352352 assert . deepStrictEqual ( decoder . decode ( 0 ) . value , tc . expected ) ;
353353 } ) ;
354354 }
355+
356+ function createBufferWithStringAtOffset (
357+ offset : number ,
358+ content : string
359+ ) : Buffer {
360+ const contentBuf = Buffer . from ( content ) ;
361+ const size = contentBuf . length ;
362+ const padding = Buffer . alloc ( offset ) ;
363+
364+ let header : Buffer ;
365+ if ( size <= 0x1f ) {
366+ header = Buffer . from ( [ 0x40 | size ] ) ;
367+ } else if ( size <= 0xffff ) {
368+ header = Buffer . from ( [ 0x40 | 0x1f , ( size >> 8 ) & 0xff , size & 0xff ] ) ;
369+ } else {
370+ header = Buffer . from ( [
371+ 0x5f ,
372+ ( size >> 24 ) & 0xff ,
373+ ( size >> 16 ) & 0xff ,
374+ ( size >> 8 ) & 0xff ,
375+ size & 0xff ,
376+ ] ) ;
377+ }
378+
379+ return Buffer . concat ( [ padding , header , contentBuf ] ) ;
380+ }
381+
382+ const MAX_INT_32 = 2147483648 ;
383+
384+ it ( 'should handle string just below 2^31 boundary' , function ( ) {
385+ this . timeout ( 15000 ) ;
386+ const offset = MAX_INT_32 - 16 ;
387+ const content = 'boundary test' ;
388+
389+ const buffer = createBufferWithStringAtOffset ( offset , content ) ;
390+
391+ const decoder = new Decoder ( buffer ) ;
392+
393+ const result = decoder . decode ( offset ) ;
394+ assert . strictEqual ( result . value , content ) ;
395+ assert (
396+ result . offset > offset ,
397+ `Offset ${ result . offset } should be > ${ offset } `
398+ ) ;
399+ } ) ;
400+
401+ it ( 'should handle when string at offset >= 2^31' , function ( ) {
402+ this . timeout ( 15000 ) ;
403+ const offset = MAX_INT_32 ;
404+ const content = 'test' ;
405+
406+ const buffer = createBufferWithStringAtOffset ( offset , content ) ;
407+ const decoder = new Decoder ( buffer ) ;
408+
409+ const result = decoder . decode ( offset ) ;
410+ assert . strictEqual ( result . value , content ) ;
411+ assert (
412+ result . offset > offset ,
413+ `Offset ${ result . offset } should be > ${ offset } `
414+ ) ;
415+ } ) ;
355416 } ) ;
356417
357418 describe ( 'decodeUint() - uint16' , ( ) => {
0 commit comments