]> git.mxchange.org Git - simgear.git/commitdiff
Detect the actual number of wchars required for the buffer and allocate it properly
authorErik Hofman <erik@ehofman.com>
Sun, 3 Jul 2016 07:34:19 +0000 (09:34 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 13 Aug 2016 08:21:16 +0000 (10:21 +0200)
simgear/misc/sg_path.cxx

index bc0fc450d2632a3a693d425e1e9e7f98336ac58e..0d5140eb94d2e3925484d2d6ae01c826b1222608 100644 (file)
@@ -987,17 +987,20 @@ std::string SGPath::join(const std::vector<SGPath>& paths, const std::string& jo
 std::wstring SGPath::wstr() const
 {
 #ifdef SG_WINDOWS
-    return std::wstring();
-#else
-    wchar_t wideBuf[2048];
-    size_t count = mbstowcs(wideBuf, path.c_str(), 2048);
-    if (count == -1) {
-        return std::wstring();
-    } else if (count == 2048) {
-        SG_LOG( SG_GENERAL, SG_ALERT, "SGPath::wstr: overflowed conversion buffer for " << *this );
-    }
-
-    return std::wstring(wideBuf, count);
+   size_t buflen = mbstowcs(NULL, path.c_str(), 0)+1;
+   wchar_t wideBuf = malloc(buflen * sizeof(int));
+   if (wideBuf) {
+       size_t count = mbstowcs(wideBuf, path.c_str(), buflen);
+       if (count == -1) {
+           return std::wstring();
+       }
+
+       std::wstring rv(wideBuf, count);
+       free(wideBuf);
+       return rv;
+   } else {
+       SG_LOG( SG_GENERAL, SG_ALERT, "SGPath::wstr: unable to allocate enough memory for " << *this );
+   }
 #endif
 }