status-im / nim-toml-serialization

Flexible TOML serialization [not] relying on run-time type information.
Apache License 2.0
36 stars 7 forks source link

Garbage reads with `readValue` for a custom type #63

Closed ZoomRmc closed 1 year ago

ZoomRmc commented 1 year ago

Not sure if I'm doing something wrong, but with the following code I get some garbage in the title field. Am I using readValue wrong? Surely we read from somewhere we shouldn't.

import strutils
import toml_serialization

type
  Item* = object
    id*: string
    txt*: string
  Items* = seq[Item]
  Group* = object
    title*: string
    items*: Items

const toml = """title = "GroupTitle"
items = """ & "\"\"\"" & "\nX;test1\nY;test2\nZ;test3" & "\"\"\""

proc parseItems(s: string): Items =
  for l in s.splitLines():
    if l != "":
      let parts = l.split(';')
      result.add Item(id: parts[0], txt: parts[1])

proc readValue*(r: var TomlReader, items: var Items)=
  let s = parseAsString(r)
  items = parseItems(s)

let group = Toml.decode(toml, Group)
echo group.title

For this data group.title is always Y test1+ a few garbage chars. In the real project I get Illegal storage access.

jangko commented 1 year ago

hmm, it's a nim orc bug. I suspect it's related to nim-faststreams, because it works when I change let to const, it works with simple buffer, but not with faststreams.

Thank you for reporting this, make it easier for me to isolate the source of the problem