Hopefully fix file_to_string() on Windows with text files.
authorCarl Hetherington <cth@carlh.net>
Wed, 4 Nov 2015 21:25:36 +0000 (21:25 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 4 Nov 2015 21:25:36 +0000 (21:25 +0000)
src/util.cc

index 5a5db98d87e215702c344508081f0ceacba1fb7b..856bc9bd0001d0d958cbae4334df0824b6cc0a25 100644 (file)
@@ -408,12 +408,12 @@ dcp::file_to_string (boost::filesystem::path p, uintmax_t max_length)
                throw FileError ("could not open file", p, errno);
        }
 
-       char* c = new char[len + 1];
-       fread (c, 1, len, f);
+       char* c = new char[len];
+       /* This may read less than `len' if we are on Windows and we have CRLF in the file */
+       int const N = fread (c, 1, len, f);
        fclose (f);
-       c[len] = '\0';
 
-       string s (c);
+       string s (c, N);
        delete[] c;
 
        return s;