Fix config.xml corruption when it contains multi-byte UTF8 characters.
authorCarl Hetherington <cth@carlh.net>
Wed, 24 Feb 2021 17:02:49 +0000 (18:02 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 24 Feb 2021 17:02:49 +0000 (18:02 +0100)
src/lib/config.cc
src/lib/dcp_video.cc
src/lib/encode_server.cc
test/config_test.cc
test/data

index d38256673a8685cf169a24d643bdbaf0e084289b..cc9bd6ffdc177d619f810fffe61cc3ff45f0c619 100644 (file)
@@ -984,7 +984,7 @@ Config::write_config () const
                if (!f) {
                        throw FileError (_("Could not open file for writing"), tmp);
                }
-               checked_fwrite (s.c_str(), s.length(), f, tmp);
+               checked_fwrite (s.c_str(), s.bytes(), f, tmp);
                fclose (f);
                boost::filesystem::remove (config_file());
                boost::filesystem::rename (tmp, config_file());
index 153f4997ed7dbdbaf14afe693a76ae0476fa7c1a..ed68299bb45319d611606fb8a5b6451558be06e4 100644 (file)
@@ -226,7 +226,7 @@ DCPVideo::encode_remotely (EncodeServerDescription serv, int timeout)
                /* Send XML metadata */
                auto xml = doc.write_to_string ("UTF-8");
                socket->write (xml.length() + 1);
-               socket->write ((uint8_t *) xml.c_str(), xml.length() + 1);
+               socket->write ((uint8_t *) xml.c_str(), xml.bytes() + 1);
 
                /* Send binary data */
                LOG_TIMING("start-remote-send thread=%1", thread_id ());
index 92589c573d31429073efa0d530c005195c36f0e6..891b8caacae2d375f36f7a3e61499a5676a4f3c9 100644 (file)
@@ -295,8 +295,8 @@ EncodeServer::broadcast_received ()
                try {
                        auto socket = make_shared<Socket>();
                        socket->connect (boost::asio::ip::tcp::endpoint (_broadcast.send_endpoint.address(), MAIN_SERVER_PRESENCE_PORT));
-                       socket->write (xml.length() + 1);
-                       socket->write ((uint8_t *) xml.c_str(), xml.length() + 1);
+                       socket->write (xml.bytes() + 1);
+                       socket->write ((uint8_t *) xml.c_str(), xml.bytes() + 1);
                } catch (...) {
 
                }
@@ -304,8 +304,8 @@ EncodeServer::broadcast_received ()
                try {
                        auto socket = make_shared<Socket>();
                        socket->connect (boost::asio::ip::tcp::endpoint (_broadcast.send_endpoint.address(), BATCH_SERVER_PRESENCE_PORT));
-                       socket->write (xml.length() + 1);
-                       socket->write ((uint8_t *) xml.c_str(), xml.length() + 1);
+                       socket->write (xml.bytes() + 1);
+                       socket->write ((uint8_t *) xml.c_str(), xml.bytes() + 1);
                } catch (...) {
 
                }
index ec67a200a3462a556e666733b109bf1bea0dd4ad..4ca3ba07ffb3b9168b48969ca972b5eef7c5e92a 100644 (file)
@@ -91,3 +91,16 @@ BOOST_AUTO_TEST_CASE (config_backup_test)
        */
        setup_test_config ();
 }
+
+
+BOOST_AUTO_TEST_CASE (config_write_utf8_test)
+{
+       boost::filesystem::remove_all ("build/test/config.xml");
+       boost::filesystem::copy_file ("test/data/utf8_config.xml", "build/test/config.xml");
+       Config::override_path = "build/test";
+       Config::drop ();
+       Config::instance()->write();
+
+       check_file ("test/data/utf8_config.xml", "build/test/config.xml");
+}
+
index 3eb05f5989dcb601bde60c4badc95b66cf46a0cb..4c70bad1c502a43d2db17d6d03476e904eb757ce 160000 (submodule)
--- a/test/data
+++ b/test/data
@@ -1 +1 @@
-Subproject commit 3eb05f5989dcb601bde60c4badc95b66cf46a0cb
+Subproject commit 4c70bad1c502a43d2db17d6d03476e904eb757ce