Still breaks... (changes specified below)
El 5 de desembre de 2011 14:03, Kjell Ahlstedt <[email protected]> ha escrit: > Three comments to the receiving code below. > > 2011-12-05 09:54, Glus Xof skrev: > >> (I prefer to use a Glib::ustring object instead of a std::string, >> here). The problem is that at the receiving endpoint, the messages >> with a multi-byte character doesn't appear (For example: Works fine >> with strings like "Hello..." or "everything else" but not with >> "Привет..."). >> >> gssize size = 0; >> gssize to_receive = 8; >> >> gchar longr_mess[to_receive]; > > 1. I'm surprised that this code compiles. The size of an array must be a > constant. to_receive is not a constant. Is a non-constant array size allowed > in c++11? gchar * longr_mess = g_new0 (gchar, to_receive+1); > >> try >> { >> size = client_socket->receive (longr_mess, to_receive); >> } >> catch (const Gio::Error& error) >> >> { >> return; >> } >> >> >> to_receive = atoi (longr_mess); > > 2. Gio::Socket::receive() does not add a terminating null. longr_mess is not > null-terminated in the call to atoi(). Dangerous! longr_mess [size] = '\0'; to_receive = atoi (longr_mess); >> >> if (to_receive == 0) >> return; >> >> gchar * r_mess = g_new0 (gchar, to_receive); >> >> try >> { >> size = client_socket->receive (r_mess, to_receive); >> } >> catch (const Gio::Error& error) >> >> { >> return false; >> } >> >> if (size == 0) >> return false; >> >> Glib::ustring mess (r_mess, size); > > 3. In Glib::ustring (const char* src, size_type n) n is not the number of > bytes, it's the number of UTF-8 characters. > You ought to add a trailing null byte, and use Glib::ustring (const char* > src). > Or use Glib::ustring (In pbegin, In pend): Glib::ustring mess(r_mess, > r_mess+size). r_mess [size] = '\0'; Glib::ustring mess (r_mess); >> g_free (r_mess); >> >> std::cout >> << Glib::ustring::compose ("# %1", mess) >> << std::endl; >> The problem, here, is that this code works fine with one-byte characters string but breaks with multi-byte characters strings ! _______________________________________________ gtkmm-list mailing list [email protected] http://mail.gnome.org/mailman/listinfo/gtkmm-list
