Skip to content

Commit

Permalink
Use std::getline to restore unlimited length driver strings
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Rift <[email protected]>
  • Loading branch information
riftEmber committed Nov 22, 2024
1 parent fe1dfd4 commit 9fc783f
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions compiler/util/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <unistd.h>

#include <cstring>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cstdlib>
Expand Down Expand Up @@ -209,22 +210,11 @@ void restoreDriverTmp(const char* tmpFilePath,
fileinfo* tmpFileDummy = openTmpFile(tmpFilePath, "a");
closefile(tmpFileDummy);

fileinfo* tmpFile = openTmpFile(tmpFilePath, "r");

char strBuf[4096];
while (fgets(strBuf, sizeof(strBuf), tmpFile->fptr)) {
// Note: Using strlen here (instead of strnlen) is safe because fgets
// guarantees null termination.
size_t len = strlen(strBuf);
// remove trailing newline, which fgets preserves unless buffer is exceeded
assert(strBuf[len - 1] == '\n' && "stored line exceeds maximum length");
strBuf[--len] = '\0';

// invoke restoring function
restoreSavedString(strBuf);
std::ifstream fileStream(genIntermediateFilename(tmpFilePath));
std::string line;
while (std::getline(fileStream, line)) {
restoreSavedString(line.c_str());
}

closefile(tmpFile);
}

void restoreDriverTmpMultiline(
Expand Down

0 comments on commit 9fc783f

Please sign in to comment.