From 1451a220fc6939e60feaad12b1ca819e63cef080 Mon Sep 17 00:00:00 2001
From: atraczyk <andreastraczyk@gmail.com>
Date: Mon, 27 Mar 2017 10:24:15 -0400
Subject: [PATCH] fix: crash during unpack due to CRLF line endings on UWP

- The std::ios_base::binary flag needs to be used when opening
  files for output of encoded data on windows, otherwise bytes with
  the value 0A (LF) will be replaced with 0D0A (CRLF).
  This will cause deserialization to behave incorrectly.

- This patch adds the binary flag where encoded data is saved for
  the name cache, id list, device list, node cache, contacts list,
  and incoming trust request list.

Change-Id: I0b1b671014af1cb42bc2f76a1064bc6008c70419
---
 src/ringdht/namedirectory.cpp |  2 +-
 src/ringdht/ringaccount.cpp   | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/ringdht/namedirectory.cpp b/src/ringdht/namedirectory.cpp
index f40b2539da..2541d7835c 100644
--- a/src/ringdht/namedirectory.cpp
+++ b/src/ringdht/namedirectory.cpp
@@ -319,7 +319,7 @@ void
 NameDirectory::saveCache()
 {
     fileutils::recursive_mkdir(fileutils::get_cache_dir()+DIR_SEPARATOR_STR+"namecache");
-    std::ofstream file(cachePath_, std::ios::trunc);
+    std::ofstream file(cachePath_, std::ios::trunc | std::ios::binary);
     msgpack::pack(file, nameCache_);
     RING_DBG("Saved %lu name-address mappings", (long unsigned)nameCache_.size());
 }
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index 1d66c11209..d8c98156d8 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -2578,7 +2578,7 @@ template<typename ID=dht::Value::Id>
 void
 saveIdList(const std::string& path, const std::set<ID>& ids)
 {
-    std::ofstream file(path, std::ios::trunc);
+    std::ofstream file(path, std::ios::trunc | std::ios::binary);
     if (!file.is_open()) {
         RING_ERR("Could not save to %s", path.c_str());
         return;
@@ -2645,7 +2645,7 @@ RingAccount::loadKnownDevices()
 void
 RingAccount::saveKnownDevices() const
 {
-    std::ofstream file(idPath_+DIR_SEPARATOR_STR "knownDevicesNames", std::ios::trunc);
+    std::ofstream file(idPath_+DIR_SEPARATOR_STR "knownDevicesNames", std::ios::trunc | std::ios::binary);
 
     std::map<dht::InfoHash, std::pair<std::string, uint64_t>> devices;
     for (const auto& id : knownDevices_)
@@ -2674,7 +2674,7 @@ RingAccount::saveNodes(const std::vector<dht::NodeExport>& nodes) const
     fileutils::check_dir(cachePath_.c_str());
     std::string nodesPath = cachePath_+DIR_SEPARATOR_STR "nodes";
     {
-        std::ofstream file(nodesPath, std::ios::trunc);
+        std::ofstream file(nodesPath, std::ios::trunc | std::ios::binary);
         if (!file.is_open()) {
             RING_ERR("Could not save nodes to %s", nodesPath.c_str());
             return;
@@ -2920,7 +2920,7 @@ RingAccount::loadContacts()
 void
 RingAccount::saveContacts() const
 {
-    std::ofstream file(idPath_+DIR_SEPARATOR_STR "contacts", std::ios::trunc);
+    std::ofstream file(idPath_+DIR_SEPARATOR_STR "contacts", std::ios::trunc | std::ios::binary);
     msgpack::pack(file, contacts_);
 }
 
@@ -3003,7 +3003,7 @@ RingAccount::sendTrustRequestConfirm(const dht::InfoHash& to)
 void
 RingAccount::saveTrustRequests() const
 {
-    std::ofstream file(idPath_+DIR_SEPARATOR_STR "incomingTrustRequests", std::ios::trunc);
+    std::ofstream file(idPath_+DIR_SEPARATOR_STR "incomingTrustRequests", std::ios::trunc | std::ios::binary);
     msgpack::pack(file, trustRequests_);
 }
 
-- 
GitLab