Ruby My first script and Ruby bug

I decided to rewrite one of my perl scripts (a pop3 server) in Ruby, just to get my hands on the language ... kaboom

I have a small mail server here in perl and decided to rewrite it in Ruby just see how does it feel writing code. It was going relatively ok, lots of hiccups of course, but progressing good. Then ... the problems started for some reason...

What really surprised me was this particular one. The following code works quite fine:

h = {'marin'=>22}

File.open( 'bug', 'w+' ) do |f|
f <<Marshal.dump(h)
end

Marshal.load(File.read('bug'))

Pretty simple - initializing a hash called h, dumping it using Marshal to a file, and then reading the same file. Now that's fine of course, and when you run the script, it just does what it does and exits quietly.

The thing is however ... the following script produces an error !

h = {'marin'=>21}

File.open( 'bug', 'w+' ) do |f|
f <<Marshal.dump(h)
end

Marshal.load(File.read('bug'))

Found the difference ? The value of the key called 'marin' is 21 instead of 22... Wow ...

It seems to me Marshal has a problem with serializing the value '21' ... Honestly ... I tried all kind of integer values - 1, 2, 5, 19, 20, 22, 399, 1212121212, 2121, 816453820863 - I guarantee these are ok. Then I tried some string values including '21' - that's fine too.

My best guess is just Marshal uses the byte value 21 to divide the values inside the serialized data. The error message makes me think so :

bug.rb:7:in `load': marshal data too short (ArgumentError)
from bug.rb:7

So ... marshal data too short must be read : A value in marshal data is too short (read : broken by a value separator inside the value )

This somehow reminds me about a bug in MySQL I found some years ago ... The column type set was using the Cyrillic letter "я" as a separator for the set values ... Guess if you want to have a set of words in Bulgarian or Russian ... Not possible if you have "я" inside some word ...

Bad thinking ... I don't know how these situations happen ...

Did somebody really though when writing Marshal : "Well it seems nobody will ever want to save the integer 21 into a Marshalled file ... " ... Or somebody at Mysql though "Well it seems nobody will ever want to use words in a set with this particular Cyrillic letter ..."

Software rocks :)

Marin

Leave a Reply 777 Views