Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.createReadStream doesn't throw with options={start:NaN,end:NaN} #19715

Closed
anliting opened this issue Mar 31, 2018 · 6 comments
Closed

fs.createReadStream doesn't throw with options={start:NaN,end:NaN} #19715

anliting opened this issue Mar 31, 2018 · 6 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@anliting
Copy link

I think it would be better to throw in the following case. The following case also causes every newly created read stream to end immediately.

$ node --experimental-modules a.mjs with a.mjs:

import fs from 'fs'
function length(rs){
    return new Promise(res=>{
        let a=[]
        rs.on('data',[].push.bind(a))
        rs.on('end',e=>{
            res(Buffer.concat(a).length)
        })
    })
}
fs.writeFileSync('hello.txt','world')
;(async()=>{
    // print 5 0 0
    console.log(
        await length(fs.createReadStream('hello.txt')),
        await length(fs.createReadStream('hello.txt',{start:NaN,end:NaN})),
        await length(fs.createReadStream('hello.txt')),
    )
})()
@vsemozhetbyt vsemozhetbyt added the buffer Issues and PRs related to the buffer subsystem. label Mar 31, 2018
@ryzokuken
Copy link
Contributor

If I'm not wrong, this should just involve adding a simple check at https://github.com/nodejs/node/blob/master/lib/fs.js#L1976, something on the lines of:

if (Number.isNaN(options.start) || Number.isNaN(options.end)) {
  throw new Error(...) // Perhaps a RangeError would be more fitting?
}

Let me know if it sounds about right and I'll submit a PR promptly.

@anliting
Copy link
Author

anliting commented Apr 1, 2018

Similar checking can be found here (https://github.com/nodejs/node/blob/master/lib/fs.js#L2010-L2026), so I think it might be more appropriate to make a patch here, for the sake of maintainability.

start and end are integers (https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options), so I think it might be more appropriate to check it with Number.isInteger, avoiding there are similar cases like this issue we didn't find.

I would fix it like this: https://github.com/anliting/node-1/blob/master/lib/fs.js#L2010-L2030

@ryzokuken
Copy link
Contributor

@anliting I've made the change at the exact same place.

ryzokuken added a commit to ryzokuken/node that referenced this issue Apr 1, 2018
This test makes fs.ReadStream throw an error when either start or
end is NaN.

Fixes: nodejs#19715
@ghost
Copy link

ghost commented Apr 2, 2018

isNaN still allows infinite numbers (-Infinity, Infinity).
Infinity is currently used as the fallback value for end for backwards compatibility, but I am not so sure if it's a good choice for start.

@anliting
Copy link
Author

anliting commented Apr 2, 2018

@arboreal84 Check out #19732 if it solves your issue.

@ryzokuken
Copy link
Contributor

ryzokuken commented Apr 2, 2018

@arboreal84 I don't think Number.isInteger(Infinity) or Number.isInteger(-Infinity) return true though.

ryzokuken added a commit to ryzokuken/node that referenced this issue Apr 3, 2018
Make ReadStream (and thus createReadStream) throw a TypeError signalling
towards an invalid argument type when either options.start or
options.end (or obviously, both) are set to NaN.
Also add regression tests for the same.

Fixes: nodejs#19715
@lpinca lpinca closed this as completed in 38a6929 Apr 6, 2018
BridgeAR pushed a commit to BridgeAR/node that referenced this issue May 1, 2018
Make ReadStream (and thus createReadStream) throw a TypeError signalling
towards an invalid argument type when either options.start or
options.end (or obviously, both) are set to NaN.
Also add regression tests for the same.

PR-URL: nodejs#19775
Fixes: nodejs#19715
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
3 participants