diff --git a/src/tests/test_utils/runner/TestSuite.cpp b/src/tests/test_utils/runner/TestSuite.cpp index d533148db..88ade2470 100644 --- a/src/tests/test_utils/runner/TestSuite.cpp +++ b/src/tests/test_utils/runner/TestSuite.cpp @@ -1196,15 +1196,8 @@ TestSuite::TestSuite(int *argc, char **argv, std::function registerTests exit(EXIT_FAILURE); } - uint32_t fileSize = 0; - if (!GetFileSize(mFilterFile.c_str(), &fileSize)) - { - printf("Error getting filter file size: %s\n", mFilterFile.c_str()); - exit(EXIT_FAILURE); - } - - std::vector fileContents(fileSize + 1, 0); - if (!ReadEntireFileToString(mFilterFile.c_str(), fileContents.data(), fileSize)) + std::string fileContents; + if (!ReadEntireFileToString(mFilterFile.c_str(), &fileContents)) { printf("Error loading filter file: %s\n", mFilterFile.c_str()); exit(EXIT_FAILURE); diff --git a/util/shader_utils.cpp b/util/shader_utils.cpp index 5c2875b20..7d60fe6f9 100644 --- a/util/shader_utils.cpp +++ b/util/shader_utils.cpp @@ -16,17 +16,6 @@ namespace { -bool ReadEntireFile(const std::string &filePath, std::string *contentsOut) -{ - constexpr uint32_t kMaxBufferSize = 2000; - char buffer[kMaxBufferSize] = {}; - if (!angle::ReadEntireFileToString(filePath.c_str(), buffer, kMaxBufferSize) || - strlen(buffer) == 0) - return false; - *contentsOut = buffer; - return true; -} - GLuint CompileProgramInternal(const char *vsSource, const char *tcsSource, const char *tesSource, @@ -200,7 +189,7 @@ GLuint CompileShader(GLenum type, const char *source) GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath) { std::string source; - if (!ReadEntireFile(sourcePath, &source)) + if (!angle::ReadEntireFileToString(sourcePath.c_str(), &source)) { std::cerr << "Error reading shader file: " << sourcePath << "\n"; return 0; @@ -318,14 +307,14 @@ GLuint CompileProgramWithTESS(const char *vsSource, GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath) { std::string vsSource; - if (!ReadEntireFile(vsPath, &vsSource)) + if (!angle::ReadEntireFileToString(vsPath.c_str(), &vsSource)) { std::cerr << "Error reading shader: " << vsPath << "\n"; return 0; } std::string fsSource; - if (!ReadEntireFile(fsPath, &fsSource)) + if (!angle::ReadEntireFileToString(fsPath.c_str(), &fsSource)) { std::cerr << "Error reading shader: " << fsPath << "\n"; return 0; diff --git a/util/test_utils.cpp b/util/test_utils.cpp index f0065bb3c..5057aa95f 100644 --- a/util/test_utils.cpp +++ b/util/test_utils.cpp @@ -37,7 +37,7 @@ bool GetFileSize(const char *filePath, uint32_t *sizeOut) return true; } -bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t maxLen) +bool ReadEntireFileToString(const char *filePath, std::string *contentsOut) { std::ifstream stream(filePath); if (!stream) @@ -45,15 +45,12 @@ bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t ma return false; } - std::string contents; - stream.seekg(0, std::ios::end); - contents.reserve(static_cast(stream.tellg())); + contentsOut->reserve(static_cast(stream.tellg())); stream.seekg(0, std::ios::beg); - contents.assign((std::istreambuf_iterator(stream)), std::istreambuf_iterator()); + contentsOut->assign((std::istreambuf_iterator(stream)), std::istreambuf_iterator()); - strncpy(contentsOut, contents.c_str(), maxLen); return true; } diff --git a/util/test_utils.h b/util/test_utils.h index fedff00f3..c67c93b45 100644 --- a/util/test_utils.h +++ b/util/test_utils.h @@ -51,8 +51,9 @@ bool CreateTemporaryFileInDir(const char *dir, char *tempFileNameOut, uint32_t m // Deletes a file or directory. bool DeleteSystemFile(const char *path); -// Reads a file contents into a string. -bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t maxLen); +// Reads a file contents into a string. Note: this method cannot be exported across a shared module +// boundary because it does memory allocation. +bool ReadEntireFileToString(const char *filePath, std::string *contentsOut); // Compute a file's size. bool GetFileSize(const char *filePath, uint32_t *sizeOut); diff --git a/util/test_utils_unittest.cpp b/util/test_utils_unittest.cpp index a128d9fcb..cc37637f0 100644 --- a/util/test_utils_unittest.cpp +++ b/util/test_utils_unittest.cpp @@ -93,9 +93,9 @@ TEST(TestUtils, MAYBE_CreateAndDeleteTemporaryFile) EXPECT_GE(retval, 0); // Test ReadEntireFileToString - char actualString[kMaxPath]; - EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath)); - EXPECT_EQ(strcmp(actualString, kOutputString), 0); + std::string actualString; + EXPECT_TRUE(ReadEntireFileToString(path, &actualString)); + EXPECT_EQ(strcmp(actualString.c_str(), kOutputString), 0); // Delete the temporary file. EXPECT_TRUE(angle::DeleteSystemFile(path)); @@ -121,9 +121,9 @@ TEST(TestUtils, MAYBE_CreateAndDeleteFileInTempDir) EXPECT_GE(retval, 0); // Test ReadEntireFileToString - char actualString[kMaxPath]; - EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath)); - EXPECT_EQ(strcmp(actualString, kOutputString), 0); + std::string actualString; + EXPECT_TRUE(ReadEntireFileToString(path, &actualString)); + EXPECT_EQ(strcmp(actualString.c_str(), kOutputString), 0); // Delete the temporary file. EXPECT_TRUE(angle::DeleteSystemFile(path));