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

Error: unhandled exception: value out of range [RangeDefect] #1121

Open
adokitkat opened this issue Aug 7, 2024 · 1 comment
Open

Error: unhandled exception: value out of range [RangeDefect] #1121

adokitkat opened this issue Aug 7, 2024 · 1 comment
Labels

Comments

@adokitkat
Copy link

Hi, I get this error message using both Nim v1.6 and v2 (I also got more smaller errors but I fixed them fairly easy, mostly syntax errors or arithmetics on variables with different types):

> nim c -d:debug -r main.nim
Hint: used config file '/home/x/.choosenim/toolchains/nim-1.6.20/config/nim.cfg' [Conf]
Hint: used config file '/home/x/.choosenim/toolchains/nim-1.6.20/config/config.nims' [Conf]
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
9935 lines; 0.015s; 10.68MiB peakmem; proj: /home/x/hobby/nim/kaitai/main.nim; out: /home/x/hobby/nim/kaitai/main [SuccessX]
Hint: /home/x/hobby/nim/kaitai/main  [Exec]
/home/x/hobby/nim/kaitai/main.nim(541) main
/home/x/hobby/nim/kaitai/main.nim(538) main
/home/x/hobby/nim/kaitai/main.nim(172) fromFile
/home/x/hobby/nim/kaitai/main.nim(152) read
/home/x/hobby/nim/kaitai/main.nim(453) read
/home/x/hobby/nim/kaitai/main.nim(267) read
/home/x/.choosenim/toolchains/nim-1.6.20/lib/system/fatal.nim(54) sysFatal
Error: unhandled exception: value out of range [RangeDefect]

Caused by this generated Nim code:

proc read*(_: typedesc[Nvs_EntryStateBitmapT], io: KaitaiStream, root: KaitaiStruct, parent: Nvs_PageT): Nvs_EntryStateBitmapT =
  template this: untyped = result
  this = new(Nvs_EntryStateBitmapT)
  let root = if root == nil: cast[Nvs](this) else: cast[Nvs](root)
  this.io = io
  this.root = root
  this.parent = parent

  for i in 0 ..< int(((4 * 32) - 2)):
    let it = Nvs_EntryState(this.io.readBitsIntLe(2)) # <---- crashes here
    this.entry.add(it)
  let unnamed1Expr = this.io.readBitsIntLe(4)
  this.unnamed1 = unnamed1Expr

The source .ksy file works with ksv visualizer. I compiled the Nim code with https://ide.kaitai.io/# website.

Files to replicate this in zip below:
kaitai_nim_doesnt_work.zip

@generalmimon
Copy link
Member

generalmimon commented Aug 7, 2024

@adokitkat:

I compiled the Nim code with https://ide.kaitai.io/# website.

This is actually significant - please use https://ide.kaitai.io/devel/ instead of https://ide.kaitai.io/, at least for now. It seems you've run into the same situation as in kaitai-io/kaitai_struct_webide#150 (comment), which has been fixed at https://ide.kaitai.io/devel/ (as part of kaitai-io/kaitai_struct_webide#165), but not in the "stable" version of the Web IDE yet.

The error message value out of range [RangeDefect] looks like it boils down to this exact problem - check the main.nim file you generated:

  Nvs_EntryState* = enum
    entry_state_empty = 0
  Nvs_NvsType* = enum

this.io.readBitsIntLe(2) apparently reads something other than 0, which Nim doesn't know how to convert into the Nvs_EntryState type.


You may also be interested in other recent improvements to the Web IDE: https://kaitai.io/news/2024/02/21/web-ide-improvements.html

@generalmimon generalmimon transferred this issue from kaitai-io/kaitai_struct_nim_runtime Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants