Commit 521c5483 authored by Ken Martin's avatar Ken Martin

ENH: added a limit to the getline method

parent 8f91a1d5
...@@ -3542,7 +3542,8 @@ kwsys_stl::string SystemTools::MakeCindentifier(const char* s) ...@@ -3542,7 +3542,8 @@ kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
// if any data were read before the end-of-file was reached. // if any data were read before the end-of-file was reached.
bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, bool SystemTools::GetLineFromStream(kwsys_ios::istream& is,
kwsys_stl::string& line, kwsys_stl::string& line,
bool* has_newline /* = 0 */) bool* has_newline /* = 0 */,
long sizeLimit /* = -1 */)
{ {
const int bufferSize = 1024; const int bufferSize = 1024;
char buffer[bufferSize]; char buffer[bufferSize];
...@@ -3552,9 +3553,12 @@ bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, ...@@ -3552,9 +3553,12 @@ bool SystemTools::GetLineFromStream(kwsys_ios::istream& is,
// Start with an empty line. // Start with an empty line.
line = ""; line = "";
long leftToRead = sizeLimit;
// If no characters are read from the stream, the end of file has // If no characters are read from the stream, the end of file has
// been reached. Clear the fail bit just before reading. // been reached. Clear the fail bit just before reading.
while(!haveNewline && while(!haveNewline &&
leftToRead != 0 &&
(is.clear(is.rdstate() & ~kwsys_ios::ios::failbit), (is.clear(is.rdstate() & ~kwsys_ios::ios::failbit),
is.getline(buffer, bufferSize), is.gcount() > 0)) is.getline(buffer, bufferSize), is.gcount() > 0))
{ {
...@@ -3575,8 +3579,23 @@ bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, ...@@ -3575,8 +3579,23 @@ bool SystemTools::GetLineFromStream(kwsys_ios::istream& is,
buffer[length-1] = 0; buffer[length-1] = 0;
} }
// if we read too much then truncate the buffer
if (leftToRead > 0)
{
if (length > leftToRead)
{
buffer[leftToRead-1] = 0;
leftToRead = 0;
}
else
{
leftToRead -= length;
}
}
// Append the data read to the line. // Append the data read to the line.
line.append(buffer); line.append(buffer);
sizeLimit -= length;
} }
// Return the results. // Return the results.
......
...@@ -434,7 +434,8 @@ public: ...@@ -434,7 +434,8 @@ public:
*/ */
static bool GetLineFromStream(kwsys_ios::istream& istr, static bool GetLineFromStream(kwsys_ios::istream& istr,
kwsys_stl::string& line, kwsys_stl::string& line,
bool* has_newline=0); bool* has_newline=0,
long sizeLimit=-1);
/** /**
* Get the parent directory of the directory or file * Get the parent directory of the directory or file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment