achille-roussel / nanomsgxx

Nanomsg binding for C++11
MIT License
115 stars 35 forks source link

bugfix : xsputn // taking into consideration the buffer size when copying #6

Closed steven-martins closed 8 years ago

steven-martins commented 8 years ago

Hello,

I've got a memory corruption when I try to write more than 1000 bytes at once.

vagrant@gitlab:~$ ./a.out
*** Error in `./a.out': free(): invalid next size (normal): 0x0000000001677400 ***

A snippet of code which highlights the bug:

#include <iostream>                  
#include <nnxx/message.h>            
#include <nnxx/message_ostream.h>    

int main()                           
{                                    
  std::string str(2000, 'A');        
  nnxx::message_ostream os;          

  os << str;                         

}                                    

It turned out that the copy made by message_streambuf::xsputn does not take into account the available buffer size. Thereby the writes with a size higher than 1000 are not handled correctly.

I follow the libstd++'s implementation of steambuf::xsputn to provide this patch.

Test results:

execution summary
  tests that pass 14/14
    /home/vagrant/nanomsgxx/build/tests/test_readme
    /home/vagrant/nanomsgxx/build/tests/test_reqrep
    /home/vagrant/nanomsgxx/build/tests/test_pair
    /home/vagrant/nanomsgxx/build/tests/test_message_ostream
    /home/vagrant/nanomsgxx/build/tests/test_pipeline
    /home/vagrant/nanomsgxx/build/tests/test_survey
    /home/vagrant/nanomsgxx/build/tests/test_message
    /home/vagrant/nanomsgxx/build/tests/test_reqrep_multi
    /home/vagrant/nanomsgxx/build/tests/test_poll
    /home/vagrant/nanomsgxx/build/tests/test_nnxx_ext
    /home/vagrant/nanomsgxx/build/tests/test_socket
    /home/vagrant/nanomsgxx/build/tests/test_pubsub
    /home/vagrant/nanomsgxx/build/tests/test_message_istream
    /home/vagrant/nanomsgxx/build/tests/test_timeout
  tests that fail 0/14
achille-roussel commented 8 years ago

Thanks for figuring this out, I'll merge and make a patch release, this looks pretty important to fix.