danieltdt/unqlite-ruby — pull request #4 by djurczak (2013-07-19 11:40:17+00:00)
Title: Fix valuestring corruption (issue #3)

As described in [issue 3] I have encountered that any value-string of a given k/v pair that is longer than 7 characters ended up being corrupted:

irb(main):015:0> db.fetch("key2")
=> "1234567"
irb(main):016:0> db.store("key2", "12345678")
=> true
irb(main):017:0> db.fetch("key2")
=> "12345678\xA0\x97\v\x02"

This wasn't caught by the test suite because each of them uses shorter strings (``wabba``).

What I think caused the problem was the fact that the code, originally, tried to determine the length of a dynamic cstring by using ``sizeof(string)``. While this works might work for statically allocated buffers (e.g. ``char zData[34]``) it doesn't work for dynamically allocated ones. Instead it returns the size of the pointer which in my case is 8 bytes.

I changed the code to use ``RSTRING_LEN + 1`` instead. Moreover, I increased the requested space in the calloc call to make sure there is enough space to include the zero-terminator (``\0``) in each string.

Finally, I added an additional test case which uses a longer value-string (``wabbawabba``).

cheers,

Daniel

[issue 3]: https://github.com/danieltdt/unqlite-ruby/issues/3