capocasa / limdb

Fast, in-process key-value store with a table-like interface persisted to disk using lmdb.
MIT License
33 stars 3 forks source link

Errors when key/value is object/tuple/array which contains string field #3

Open eienEika opened 1 year ago

eienEika commented 1 year ago

Following code:

import limdb
type Foo = object
  a: string
let db = initDatabase("db", (int, Foo))
db[1] = Foo(a: "a")
echo db[1]

runs without error. But in next run (without resetting value) when you try read db[1] program crashes with SIGSEGV. Some remarks:

Also for Foo as key there is another error. It doesn't compile because there are no compare procedure for string, string.

Nim version 1.6.12 and devel, no matter of --mm is refc or arc/orc.

capocasa commented 1 year ago

Thanks for letting me know!

For an immediate workaround, use a cstring. I will update the type checker to show an error for unsupported types.

It might be possible to support some heap types like string and seq as part of objects without degrading performance but I haven't had any great ideas so far.

winrid commented 10 months ago

@capocasa Confirmed still broken in Nim 2 with cstring:

import limdb
import std/strformat

type
  PersonCStr = object
    name: cstring
    age: Natural # Ensures the age is positive

let db = initDatabase("dbTestPersonCStr", (string, PersonCStr))

if "person" in db:
  echo "reading"
  echo db["person"]
else:
  db["person"] = PersonCStr(name: "John", age: 45)
  echo "saved"

Run this twice (once to save, once to read). Read on 2nd run fails at runtime:

reading
Traceback (most recent call last)
/home/winrid/CLionProjects/nimtest/main.nim(13) main
/home/winrid/.choosenim/toolchains/nim-2.0.0/lib/std/private/miscdollars.nim(31) $
/home/winrid/.choosenim/toolchains/nim-2.0.0/lib/system/iterators.nim(87) addQuoted
SIGSEGV: Illegal storage access. (Attempt to read from nil?)