kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
4.04k stars 199 forks source link

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

Open adokitkat opened 3 months ago

adokitkat commented 3 months ago

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 commented 3 months ago

@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 https://github.com/kaitai-io/kaitai_struct_webide/issues/150#issuecomment-1307819199, which has been fixed at https://ide.kaitai.io/devel/ (as part of https://github.com/kaitai-io/kaitai_struct_webide/issues/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