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

Offset must be >= 0 && <= 0 Out of range 0 :-) #21193

Closed
amno1 opened this issue Jun 7, 2018 · 30 comments
Closed

Offset must be >= 0 && <= 0 Out of range 0 :-) #21193

amno1 opened this issue Jun 7, 2018 · 30 comments
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core. fs Issues and PRs related to the fs subsystem / file system. good first issue Issues that are suitable for first-time contributors. help wanted Issues that need assistance from volunteers or PRs that need help to proceed.

Comments

@amno1
Copy link

amno1 commented Jun 7, 2018

internal/fs/utils.js:353
throw err;
^

RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 && <= 0. Received 0
at Object.fs.readSync (fs.js:626:3)
at Object. (/home/arthur/serialkeys/dbtest/getkeys.js:11:14)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

I am trying to read a file and I am getting error when trying to read at beginning. As seen above, it says offset must be >= 0 and <= 0. It is very interesting error message i must say. Anyway I am using offset 0 and I get that error :-). I am just trying to read 10 bytes into an Uint8Array. Here is the call

let string = new Uint8Array();
let num = fs.readSync(keyfile, string, 0, 10, 0)
@joyeecheung joyeecheung added the fs Issues and PRs related to the fs subsystem / file system. label Jun 7, 2018
@joyeecheung
Copy link
Member

joyeecheung commented Jun 7, 2018

There should be an error for this because the buffer is empty and can't be written, although the error message does look pretty funny and this error (bufferLength === 0) should be special cased.

If anyone is interested, the enhancement should be added in

function validateOffsetLengthRead(offset, length, bufferLength) {
let err;
if (offset < 0 || offset >= bufferLength) {
err = new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${bufferLength}`, offset);
} else if (length < 0 || offset + length > bufferLength) {
err = new ERR_OUT_OF_RANGE('length',
`>= 0 && <= ${bufferLength - offset}`, length);
}
if (err !== undefined) {
Error.captureStackTrace(err, validateOffsetLengthRead);
throw err;
}
}

(Not sure if this count as a good first issue so I'll just add help wanted for new contributors)

@joyeecheung joyeecheung added the help wanted Issues that need assistance from volunteers or PRs that need help to proceed. label Jun 7, 2018
@amno1
Copy link
Author

amno1 commented Jun 7, 2018

Aha, so it was referring to my uint8array :-). Indeed changing constructor to make 10 bytes solved it.

let string = new Uint8Array(10);

However the message text is still funny, maybe renaming offset to "target_offset" or something might make it a bit easier for n00bs like me? :-)

@amno1
Copy link
Author

amno1 commented Jun 7, 2018

Thank you by the way!

@joyeecheung
Copy link
Member

joyeecheung commented Jun 7, 2018

@amno1 I think the better error should probably just be ERR_BUFFER_OUT_OF_BOUNDS or ERR_INVALID_BUFFER_SIZE with a twist to the message formatters since none of them look suitable right now. Or a generic ERR_INVALID_ARG_VALUE also works.

I could fix this myself if no new contributor want to pick this up. The internal error system is documented here: https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md

@joyeecheung joyeecheung added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Jun 7, 2018
@amno1
Copy link
Author

amno1 commented Jun 7, 2018

Just put a bit more clear message in error message and probably in documentation as well, what "offset" means. I was looking at docs, since I am really a C/C++ programmer, and wasn't really clear why there was both position and offset. The error message didn't help much :-). This is what docs says:

fs.readSync(fd, buffer, offset, length, position)

History

fd <integer>
buffer <Buffer> | <Uint8Array>
offset <integer>
length <integer>
position <integer>
Returns: <number>

Synchronous version of fs.read(). Returns the number of bytesRead.

Just make it a bit more clear in which ever way you prefer.

@joyeecheung
Copy link
Member

@amno1

The convention of the fs docs is: the actual explanation in under the async version of that API. In this case, fs.read() has a detailed explanation of what these arguments mean, and the sync version only mentions Synchronous version of fs.read().

Probably deserves another good first issue to make it more explicit, something like For detailed documentation, see fs.api().

@ryzokuken
Copy link
Contributor

ryzokuken commented Jun 7, 2018

As mentioned by @joyeecheung, this is just a matter of improving the current error messages in

function validateOffsetLengthRead(offset, length, bufferLength) {
let err;
if (offset < 0 || offset >= bufferLength) {
err = new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${bufferLength}`, offset);
} else if (length < 0 || offset + length > bufferLength) {
err = new ERR_OUT_OF_RANGE('length',
`>= 0 && <= ${bufferLength - offset}`, length);
}
if (err !== undefined) {
Error.captureStackTrace(err, validateOffsetLengthRead);
throw err;
}
}
replacing the current one with a ERR_INVALID_ARG_VALUE or a modified version of ERR_INVALID_BUFFER_SIZE maybe. PRs welcome, labelling as good-first-issue.

@AdityaSrivast
Copy link
Contributor

I want to give this a try. Will report back in two days.

@ryzokuken
Copy link
Contributor

@AdityaSrivast sure thing! Feel free to reach out if you face any issues setting everything up.

@AdityaSrivast
Copy link
Contributor

@ryzokuken Is there any way to test this error and know that I have fixed it?

@ryzokuken
Copy link
Contributor

@AdityaSrivast as mentioned by @amno1, the test case for this is:

let string = new Uint8Array();
let num = fs.readSync(keyfile, string, 0, 10, 0);

this should trigger the error, just make sure to test it works and changed and in the end, add a test that checks if the error is the correct one with assert.throws(...). Hope that helps.

@AdityaSrivast
Copy link
Contributor

@ryzokuken Where is this code(test case) to be typed?

@ryzokuken
Copy link
Contributor

@AdityaSrivast Make a test script, or try running it in the REPL.

@ryzokuken
Copy link
Contributor

P.S. You'd also need to set something to the keyfile variable? Maybe it's a PEM cert? It shouldn't matter though, any file should work.

@amno1
Copy link
Author

amno1 commented Jun 8, 2018

You don't need to hack the source. Just add to documentation what offset variable is referring to. That is probably the easiest thing to do.

Otherwise, the code I was using:

let fs = require ('fs')
let path = require ('path')

let file = process.argv.slice(2)

pt("opening file: "+file)

let keyfile = fs.openSync(file, 'r')
//let string = new Uint8Array(10)
let string = new Uint8Array()  // <-- this triggers the error
let num = fs.readSync(keyfile, string, 0, 10, 0)

pt("Red bytes" + num)
pt("string: " + string)

function pt(s){ console.log(s)}

"Testcodes" is just a textfile with strings of certain length, in this case lentgh was 10. Everything was just a small test to see how node api compares to C api since I am not JS developer.
Testcodes file:

QXFPEEPNAT
ZIEEVVEZCU
CUPUMUEUPR
ISMNOTJCYG
TBKEWVBWRH
GAXHUDAUAZ
QFYWDRFDVA
WJOPKBXPMK
RZAIQNZQZK
OIRPQKIQDU
OONZZNMZNH
TPBPBPKHPQ

Run it with node readcodes.js testcodes , assuming that you save code in file "readcodes.js" and test strings into file named "testcodes".

@AdityaSrivast
Copy link
Contributor

AdityaSrivast commented Jun 8, 2018

@amno1 from the given code I am not getting OUT_OF_RANGE error. Instead, the error message which I am getting is:
fs.js:675
return binding.read(fd, buffer, offset, length, position);
^

Error: Offset is out of bounds
at Object.fs.readSync (fs.js:675:18)
at Object. (/home/aditya/nodetest/readcodes.js:12:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3

I am using node -v8.11.2.

However I also want to test against current repository. Can anyone help me out ? It is unclear to me (i)where to type wrong code and (ii)how to record the henceforth coming error (while testing with 'repository'). Please correct me if I have understood wrong anywhere.
Thanks in advance.

@amno1
Copy link
Author

amno1 commented Jun 9, 2018

Obviously from my very first post I am using node 10.4.0, so you should update your node if you wish to have exact same message :-).

○ → node --version
v10.4.0

@AdityaSrivast
Copy link
Contributor

Oh! I missed that out. @amno1 Thanks a lot for correcting. I have upgraded my nodejs.

@jrasanen
Copy link
Contributor

jrasanen commented Jun 9, 2018

  if (offset < 0 || offset >= bufferLength) {
    err = new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${bufferLength}`, offset);
  } else if (length < 0 || offset + length > bufferLength) {
    err = new ERR_OUT_OF_RANGE('length',
                               `>= 0 && <= ${bufferLength - offset}`, length);
  }

should this just have a check bufferLength < 1 and then throw more sensible error message?

@amno1
Copy link
Author

amno1 commented Jun 9, 2018

Just emit more sensible error message and mention in docs what offset parameter refers to.

@AdityaSrivast
Copy link
Contributor

AdityaSrivast commented Jun 9, 2018

@ryzokuken Is there any way I can run the code using current node repository and record the error message? I have used assert.expectsError() in test/parallel/test-internal-errors.js but I don't know how to record the message from the error in the code(test case).
In short, I want to run the test case using node compiler of this repository. But I can't figure out how to do so.

@jrasanen
Copy link
Contributor

jrasanen commented Jun 9, 2018

@AdityaSrivast have you checked the contributing and running tests instructions?

These might help you,
https://github.com/nodejs/node/blob/master/doc/guides/contributing/pull-requests.md
https://github.com/nodejs/node/blob/master/CONTRIBUTING.md

Running these will run the tests against the current code you have in your repository:

./configure
make -j4 test

@AdityaSrivast
Copy link
Contributor

AdityaSrivast commented Jun 9, 2018

@jrasanen I have already tried it. It takes more than 1 hour and without any outcome, which is very long for running few lines of code

@jrasanen
Copy link
Contributor

@AdityaSrivast hm, you can update a single test only

python tools/test.py -J --mode=release parallel/test-stream2-transform

And if you change node's source, you can just run make -j4 again, and it should only compile the changed files, so should be faster. Depending on how many cores your computer has, you could try adding more compile jobs by increasing -j4 to -j8 or so.

@AdityaSrivast
Copy link
Contributor

AdityaSrivast commented Jun 11, 2018

Hey, I have made a commit which does not follow 72 column rule. What to do? Should I remove it? Also this is my first commit so it would be great if someone helps me out how to do it smoothly. :)

@AdityaSrivast
Copy link
Contributor

Ok I worked it out. Thanks anyways 😃

@ryzokuken
Copy link
Contributor

@AdityaSrivast you can amend the commit message.

AdityaSrivast added a commit to AdityaSrivast/node that referenced this issue Jun 11, 2018
ERR_INVALID_ARG_VALUE is an error for wrong arguments given to the
function. But calling a function without argument should also generate
a sensible error message. For no argument case, parameter 'value'
should be passed with zero value and the error message is generated.

In readSync(fd, buffer, offset, length, position), triggers
validateOffsetLengthRead() in lib/internal/fs/utils.js for validation.
When buffer is empty, a weird message is generated "The value of offset
is out of range.It must be >= 0 && <= 0. Received 0". There should be a
special case when buffer is empty or bufferLength is zero, which should
trigger ERR_INVALID_ARG_VALUE error.

Fixes: nodejs#21193
AdityaSrivast added a commit to AdityaSrivast/node that referenced this issue Jun 11, 2018
ERR_INVALID_ARG_VALUE is an error for wrong arguments given to the
function. But calling a function without argument should also generate
a sensible error message. For no argument case, parameter 'value'
should be passed with zero value and the error message is generated.

In readSync(fd, buffer, offset, length, position), triggers
validateOffsetLengthRead() in lib/internal/fs/utils.js for validation.
When buffer is empty, a weird message is generated "The value of offset
is out of range.It must be >= 0 && <= 0. Received 0". There should be a
special case when buffer is empty or bufferLength is zero, which should
trigger ERR_INVALID_ARG_VALUE error.

Fixes: nodejs#21193
AdityaSrivast added a commit to AdityaSrivast/node that referenced this issue Jun 11, 2018
In validateOffsetLengthRead(), an error message is generated if offset
or length are out of range. There should also be an error message if
buffer is empty, in which case it cannot write data in it.

Generally this validateOffsetLengthRead() is triggered with
fs.readSync(fd, buffer, offset, length, position), where if buffer is
empty, the case for zero bufferLength is triggered and the
corresponding message is thrown.

Fixes: nodejs#21193
AdityaSrivast added a commit to AdityaSrivast/node that referenced this issue Jun 12, 2018
In validateOffsetLengthRead(), an error message is generated if offset
or length are out of range. But there should also be an error message
if buffer is empty, when no data can be written on it.

Generally, validateOffsetLengthRead() is triggered with
fs.readSync(fd, buffer, offset, length, position), where if 'buffer' is
empty, the case for zero bufferLength should be triggered and the
error message for empty buffer should be thrown.

Fixes: nodejs#21193
AdityaSrivast added a commit to AdityaSrivast/node that referenced this issue Jun 16, 2018
While using read() or readSync() function, it should be verified that
the arguments of the function are in proper range and hold legitimate
values, for a successful read process.
For this validateOffsetLengthRead() function is called, which gives an
error message for offset and length passed by the user, if not in
order. But there should also be an error when an empty buffer is passed
as argument, when it cannot be written over.
The empty buffer case should be checked before calling
validateOffsetLengthRead() and ERR_INVALID_ARG_VALUE should be thrown
corresponding to the empty buffer argument in read and readSync
function.

Fixes: nodejs#21193
AdityaSrivast added a commit to AdityaSrivast/node that referenced this issue Jun 19, 2018
While using read() or readSync() function, it should be verified that
the arguments of the function are in proper range and hold legitimate
values, for a successful read process.
For this validateOffsetLengthRead() function is called, which gives an
error message for offset and length passed by the user, if not in
order. But there should also be an error when an empty buffer is passed
as argument, when it cannot be written over.
The empty buffer case should be checked before calling
validateOffsetLengthRead() and ERR_INVALID_ARG_VALUE should be thrown
corresponding to the empty buffer argument in read and readSync
function.

Fixes: nodejs#21193
@addaleax
Copy link
Member

I’m a bit confused … I updated the node-private master branch, and that closed this issue? Shouldn’t that have happened when we landed the original in the public repo?

@targos
Copy link
Member

targos commented Jul 27, 2018

It should have. I suppose GitHub missed the event.

@AdityaSrivast
Copy link
Contributor

AdityaSrivast commented Aug 9, 2018

@targos Hi, I just wanted to know if we have to add our names to contributors' list somewhere to become an official contributor, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core. fs Issues and PRs related to the fs subsystem / file system. good first issue Issues that are suitable for first-time contributors. help wanted Issues that need assistance from volunteers or PRs that need help to proceed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants