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

Created on 7 Jun 2018  Â·  30Comments  Â·  Source: nodejs/node

  • Version: v10.4.0
  • Platform: Linux pascal 4.17.0-1-ARCH #1 Mon Jun 4 20:39:17 UTC 2018 x86_64 GNU/Linux

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)
errors fs good first issue help wanted

All 30 comments

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

https://github.com/nodejs/node/blob/6dbd6f6c7d0fe5629c921a2fe618ce9c00544294/lib/internal/fs/utils.js#L293-L307

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

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? :-)

Thank you by the way!

@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

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.

@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().

As mentioned by @joyeecheung, this is just a matter of improving the current error messages in https://github.com/nodejs/node/blob/6dbd6f6c7d0fe5629c921a2fe618ce9c00544294/lib/internal/fs/utils.js#L293-L307 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.

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

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

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

@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.

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

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

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.

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".

@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.

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

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

  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?

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

@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.

@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

@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

@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.

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. :)

Ok I worked it out. Thanks anyways :smiley:

@AdityaSrivast you can amend the commit message.

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?

It should have. I suppose GitHub missed the event.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  Â·  3Comments

stevenvachon picture stevenvachon  Â·  3Comments

srl295 picture srl295  Â·  3Comments

willnwhite picture willnwhite  Â·  3Comments

cong88 picture cong88  Â·  3Comments