@@ -8,13 +8,20 @@ const filepath = fixtures.path('x.txt');
8
8
9
9
const expected = Buffer . from ( 'xyz\n' ) ;
10
10
11
- function runTest ( defaultBuffer , options ) {
11
+ function runTest ( defaultBuffer , options , errorCode = false ) {
12
12
let fd ;
13
13
try {
14
14
fd = fs . openSync ( filepath , 'r' ) ;
15
- const result = fs . readSync ( fd , defaultBuffer , options ) ;
16
- assert . strictEqual ( result , expected . length ) ;
17
- assert . deepStrictEqual ( defaultBuffer , expected ) ;
15
+ if ( errorCode ) {
16
+ assert . throws (
17
+ ( ) => fs . readSync ( fd , defaultBuffer , options ) ,
18
+ { code : errorCode }
19
+ ) ;
20
+ } else {
21
+ const result = fs . readSync ( fd , defaultBuffer , options ) ;
22
+ assert . strictEqual ( result , expected . length ) ;
23
+ assert . deepStrictEqual ( defaultBuffer , expected ) ;
24
+ }
18
25
} finally {
19
26
if ( fd != null ) fs . closeSync ( fd ) ;
20
27
}
@@ -31,7 +38,6 @@ for (const options of [
31
38
{ length : expected . length , position : 0 } ,
32
39
{ offset : 0 , length : expected . length , position : 0 } ,
33
40
34
- { offset : null } ,
35
41
{ position : null } ,
36
42
{ position : - 1 } ,
37
43
{ position : 0n } ,
@@ -41,17 +47,27 @@ for (const options of [
41
47
null ,
42
48
undefined ,
43
49
44
- // Test if bad params are interpreted as default (not mandatory)
50
+ // Test malicious corner case: it works as {length: 4} but not intentionally
51
+ new String ( '4444' ) ,
52
+ ] ) {
53
+ runTest ( Buffer . allocUnsafe ( expected . length ) , options ) ;
54
+ }
55
+
56
+ for ( const options of [
57
+
58
+ // Test various invalid options
45
59
false ,
46
60
true ,
47
61
Infinity ,
48
62
42n ,
49
63
Symbol ( ) ,
64
+ 'amString' ,
65
+ [ ] ,
66
+ ( ) => { } ,
50
67
51
- // Test even more malicious corner cases
68
+ // Test if arbitrary entity with expected .length is not mistaken for options
52
69
'4' . repeat ( expected . length ) ,
53
- new String ( '4444' ) ,
54
70
[ 4 , 4 , 4 , 4 ] ,
55
71
] ) {
56
- runTest ( Buffer . allocUnsafe ( expected . length ) , options ) ;
72
+ runTest ( Buffer . allocUnsafe ( expected . length ) , options , 'ERR_INVALID_ARG_TYPE' ) ;
57
73
}
0 commit comments