#150 Bug in it_ifile::low_level_read(std::string&)

release_3.99.x
closed
5
2012-09-15
2007-04-03
No

Hello,

I stumbled upon a bug in itfile.cpp concerning reading and writing std::string on 64bit platforms. I'm referring to version 3.99.1.

The problem is that in it_file::low_level_write(const std::string &str) on line 721,
s << str.size();
saves a 64bit value. However, in it_ifile::low_level_read(std::string &str) on line 283 we have
s >> i;
which reads a 32bit value. The effect is that the string that we read then starts with 4 0-bytes and the last 4 characters of the string are cut off. E.g., the string "12345678" would be read in as "\0\0\0\01234".

The easiest bugfix would probably be to replace line 279
int i, j;
with
size_t i, j;
as sizeof(size_t) is 4 on 32bit platforms and 8 on 64bit platforms.

There might be more similar bugs in the file, so maybe someone who knows the code should have a closer look...

Thanks!

Martin

Discussion

  • Adam Piątyszek

    Adam Piątyszek - 2007-04-03

    Logged In: YES
    user_id=1004597
    Originator: YES

    File Added: itfile-64bit_string.patch

     
  • Adam Piątyszek

    Adam Piątyszek - 2007-04-03

    Patch that should fix the issue from this bug report

     
  • Adam Piątyszek

    Adam Piątyszek - 2007-04-03

    Logged In: YES
    user_id=1004597
    Originator: YES

    One more comment:
    The patch I provided will break compatibility of it_file between 32- and 64-bit environments. Therefore, we should not use size_t for saving the size of string. I think that for backward compatibility (at least in 3.10.x branch) we should cast str.size() to int in it_file::low_level_write() and hope that users won't use strings longer than 2^32-1 ;-)

    In future we should rewrite the it_file and it_ifile classes to always use 64-bit unsigned int type (uint64_t for instance) for storing sizes of variables. This will produce in most cases identical files on both 32- and 64-bit platforms.

    So could you please try to change line 721 into something like this:
    s << static_cast<int>(str.size());
    and check if this works?

    BR,
    /ediap

     
  • Adam Piątyszek

    Adam Piątyszek - 2007-04-03

    Logged In: YES
    user_id=1004597
    Originator: YES

    This bug has been fixed by adding the missing cast to "int" (SVN rev. 980 and 981).

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks