@@ -140,3 +140,75 @@ assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
140140 assert . strictEqual ( buf . readIntLE ( 0 , 6 ) , 0x060504030201 ) ;
141141 assert . strictEqual ( buf . readIntBE ( 0 , 6 ) , 0x010203040506 ) ;
142142}
143+
144+ // https://github.com/nodejs/node/issues/8724 - specific to methods dealing
145+ // with floating point types because they call out to C++ code.
146+ {
147+ // ERR_INDEX_OUT_OF_RANGE is optional, exceptions from the binding layer
148+ // don't have it.
149+ const re = / ^ R a n g e E r r o r ( \[ E R R _ I N D E X _ O U T _ O F _ R A N G E \] ) ? : I n d e x o u t o f r a n g e $ / ;
150+ const buf = Buffer . alloc ( 0 ) ;
151+ assert . throws ( ( ) => buf . readFloatBE ( 0 ) , re ) ;
152+ assert . throws ( ( ) => buf . readFloatLE ( 0 ) , re ) ;
153+ assert . throws ( ( ) => buf . readDoubleBE ( 0 ) , re ) ;
154+ assert . throws ( ( ) => buf . readDoubleLE ( 0 ) , re ) ;
155+ for ( const noAssert of [ true , false ] ) {
156+ assert . throws ( ( ) => buf . readFloatBE ( 0 , noAssert ) , re ) ;
157+ assert . throws ( ( ) => buf . readFloatLE ( 0 , noAssert ) , re ) ;
158+ assert . throws ( ( ) => buf . readDoubleBE ( 0 , noAssert ) , re ) ;
159+ assert . throws ( ( ) => buf . readDoubleLE ( 0 , noAssert ) , re ) ;
160+ }
161+ }
162+
163+ {
164+ const { readFloatBE, readFloatLE, readDoubleBE, readDoubleLE } =
165+ Buffer . prototype ;
166+ const re = / ^ T y p e E r r o r : a r g u m e n t s h o u l d b e a B u f f e r $ / ;
167+ assert . throws ( ( ) => readFloatBE ( 0 , true ) , re ) ;
168+ assert . throws ( ( ) => readFloatLE ( 0 , true ) , re ) ;
169+ assert . throws ( ( ) => readDoubleBE ( 0 , true ) , re ) ;
170+ assert . throws ( ( ) => readDoubleLE ( 0 , true ) , re ) ;
171+ }
172+
173+ {
174+ const { readFloatBE, readFloatLE, readDoubleBE, readDoubleLE } =
175+ Buffer . prototype ;
176+ const re = / ^ T y p e E r r o r : C a n n o t r e a d p r o p e r t y ' l e n g t h ' o f u n d e f i n e d $ / ;
177+ assert . throws ( ( ) => readFloatBE ( 0 ) , re ) ;
178+ assert . throws ( ( ) => readFloatLE ( 0 ) , re ) ;
179+ assert . throws ( ( ) => readDoubleBE ( 0 ) , re ) ;
180+ assert . throws ( ( ) => readDoubleLE ( 0 ) , re ) ;
181+ assert . throws ( ( ) => readFloatBE ( 0 , false ) , re ) ;
182+ assert . throws ( ( ) => readFloatLE ( 0 , false ) , re ) ;
183+ assert . throws ( ( ) => readDoubleBE ( 0 , false ) , re ) ;
184+ assert . throws ( ( ) => readDoubleLE ( 0 , false ) , re ) ;
185+ }
186+
187+ {
188+ const re =
189+ new RegExp ( '^TypeError: Method get TypedArray\\.prototype\\.length ' +
190+ 'called on incompatible receiver \\[object Object\\]$' ) ;
191+ assert . throws ( ( ) => Buffer . prototype . readFloatBE ( 0 ) , re ) ;
192+ assert . throws ( ( ) => Buffer . prototype . readFloatLE ( 0 ) , re ) ;
193+ assert . throws ( ( ) => Buffer . prototype . readDoubleBE ( 0 ) , re ) ;
194+ assert . throws ( ( ) => Buffer . prototype . readDoubleLE ( 0 ) , re ) ;
195+ assert . throws ( ( ) => Buffer . prototype . readFloatBE ( 0 , false ) , re ) ;
196+ assert . throws ( ( ) => Buffer . prototype . readFloatLE ( 0 , false ) , re ) ;
197+ assert . throws ( ( ) => Buffer . prototype . readDoubleBE ( 0 , false ) , re ) ;
198+ assert . throws ( ( ) => Buffer . prototype . readDoubleLE ( 0 , false ) , re ) ;
199+ }
200+
201+ for ( const noAssert of [ true , false ] ) {
202+ const re = / ^ T y p e E r r o r : a r g u m e n t s h o u l d b e a B u f f e r $ / ;
203+ // Wrong receiver.
204+ assert . throws ( ( ) => Buffer . prototype . readFloatBE . call ( { } , 0 , noAssert ) , re ) ;
205+ assert . throws ( ( ) => Buffer . prototype . readFloatLE . call ( { } , 0 , noAssert ) , re ) ;
206+ assert . throws ( ( ) => Buffer . prototype . readDoubleBE . call ( { } , 0 , noAssert ) , re ) ;
207+ assert . throws ( ( ) => Buffer . prototype . readDoubleLE . call ( { } , 0 , noAssert ) , re ) ;
208+ if ( noAssert === false ) continue ; // noAssert=false is tested above.
209+ // Non-method call.
210+ assert . throws ( ( ) => Buffer . prototype . readFloatBE ( 0 , noAssert ) , re ) ;
211+ assert . throws ( ( ) => Buffer . prototype . readFloatLE ( 0 , noAssert ) , re ) ;
212+ assert . throws ( ( ) => Buffer . prototype . readDoubleBE ( 0 , noAssert ) , re ) ;
213+ assert . throws ( ( ) => Buffer . prototype . readDoubleLE ( 0 , noAssert ) , re ) ;
214+ }
0 commit comments