Now uses MAX_LINE_BUFFER_SIZE constant for the array sizing of the buffer that hold lines read in
Now checks that the line being read in does not exceed MAX_LINE_BUFFER_SIZE length, avoiding a crash whenever a long line is read in from a .tex file. An error is now generated if the line is too long, and line line is truncated at MAX_LINE_BUFFER_SIZE characters. All error messages are now constructed with wxStrings rather than char arrays. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8989 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -410,13 +410,22 @@ bool read_a_line(char *buf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ch = -2;
|
int ch = -2;
|
||||||
int i = 0;
|
int bufIndex = 0;
|
||||||
buf[0] = 0;
|
buf[0] = 0;
|
||||||
|
|
||||||
while (ch != EOF && ch != 10)
|
while (ch != EOF && ch != 10)
|
||||||
{
|
{
|
||||||
if (((i == 14) && (strncmp(buf, "\\end{verbatim}", 14) == 0)) ||
|
if (bufIndex >= MAX_LINE_BUFFER_SIZE)
|
||||||
((i == 16) && (strncmp(buf, "\\end{toocomplex}", 16) == 0)))
|
{
|
||||||
|
wxString errBuf;
|
||||||
|
errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
|
||||||
|
LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
|
||||||
|
OnError((char *)errBuf.c_str());
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (((bufIndex == 14) && (strncmp(buf, "\\end{verbatim}", 14) == 0)) ||
|
||||||
|
((bufIndex == 16) && (strncmp(buf, "\\end{toocomplex}", 16) == 0)))
|
||||||
readInVerbatim = FALSE;
|
readInVerbatim = FALSE;
|
||||||
|
|
||||||
ch = getc(Inputs[CurrentInputIndex]);
|
ch = getc(Inputs[CurrentInputIndex]);
|
||||||
@@ -430,7 +439,7 @@ bool read_a_line(char *buf)
|
|||||||
if (rightCurly > leftCurly)
|
if (rightCurly > leftCurly)
|
||||||
{
|
{
|
||||||
wxString errBuf;
|
wxString errBuf;
|
||||||
errBuf.Printf("An extra right Curly brace ('}') was detected at line %lu inside file %s",LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str());
|
errBuf.Printf("An extra right curly brace ('}') was detected at line %lu inside file %s",LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str());
|
||||||
OnError((char *)errBuf.c_str());
|
OnError((char *)errBuf.c_str());
|
||||||
|
|
||||||
// Reduce the count of right curly braces, so the mismatched count
|
// Reduce the count of right curly braces, so the mismatched count
|
||||||
@@ -449,19 +458,38 @@ bool read_a_line(char *buf)
|
|||||||
if ((ch1 == 10) || (ch1 == 13))
|
if ((ch1 == 10) || (ch1 == 13))
|
||||||
{
|
{
|
||||||
// Eliminate newline (10) following DOS linefeed
|
// Eliminate newline (10) following DOS linefeed
|
||||||
if (ch1 == 13) ch1 = getc(Inputs[CurrentInputIndex]);
|
if (ch1 == 13)
|
||||||
buf[i] = 0;
|
ch1 = getc(Inputs[CurrentInputIndex]);
|
||||||
|
buf[bufIndex] = 0;
|
||||||
IncrementLineNumber();
|
IncrementLineNumber();
|
||||||
// strcat(buf, "\\par\n");
|
// strcat(buf, "\\par\n");
|
||||||
// i += 6;
|
// i += 6;
|
||||||
|
if (bufIndex+5 >= MAX_LINE_BUFFER_SIZE)
|
||||||
|
{
|
||||||
|
wxString errBuf;
|
||||||
|
errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
|
||||||
|
LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
|
||||||
|
OnError((char *)errBuf.c_str());
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
strcat(buf, "\\par");
|
strcat(buf, "\\par");
|
||||||
i += 5;
|
bufIndex += 5;
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ungetc(ch1, Inputs[CurrentInputIndex]);
|
ungetc(ch1, Inputs[CurrentInputIndex]);
|
||||||
buf[i] = ch;
|
if (bufIndex >= MAX_LINE_BUFFER_SIZE)
|
||||||
i ++;
|
{
|
||||||
|
wxString errBuf;
|
||||||
|
errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
|
||||||
|
LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
|
||||||
|
OnError((char *)errBuf.c_str());
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf[bufIndex] = ch;
|
||||||
|
bufIndex ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -476,37 +504,60 @@ bool read_a_line(char *buf)
|
|||||||
case 0xd6: // <20>
|
case 0xd6: // <20>
|
||||||
case 0xc4: // <20>
|
case 0xc4: // <20>
|
||||||
case 0xdc: // <20>
|
case 0xdc: // <20>
|
||||||
buf[i++]='\\';
|
if (bufIndex+5 >= MAX_LINE_BUFFER_SIZE)
|
||||||
buf[i++]='"';
|
{
|
||||||
buf[i++]='{';
|
wxString errBuf;
|
||||||
|
errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
|
||||||
|
LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
|
||||||
|
OnError((char *)errBuf.c_str());
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
buf[bufIndex++]='\\';
|
||||||
|
buf[bufIndex++]='"';
|
||||||
|
buf[bufIndex++]='{';
|
||||||
switch(ch)
|
switch(ch)
|
||||||
{
|
{
|
||||||
case 0xf6:buf[i++]='o';break; // <20>
|
case 0xf6:buf[bufIndex++]='o';break; // <20>
|
||||||
case 0xe4:buf[i++]='a';break; // <20>
|
case 0xe4:buf[bufIndex++]='a';break; // <20>
|
||||||
case 0xfc:buf[i++]='u';break; // <20>
|
case 0xfc:buf[bufIndex++]='u';break; // <20>
|
||||||
case 0xd6:buf[i++]='O';break; // <20>
|
case 0xd6:buf[bufIndex++]='O';break; // <20>
|
||||||
case 0xc4:buf[i++]='A';break; // <20>
|
case 0xc4:buf[bufIndex++]='A';break; // <20>
|
||||||
case 0xdc:buf[i++]='U';break; // <20>
|
case 0xdc:buf[bufIndex++]='U';break; // <20>
|
||||||
}
|
}
|
||||||
buf[i++]='}';
|
buf[bufIndex++]='}';
|
||||||
break;
|
break;
|
||||||
case 0xdf: // <20>
|
case 0xdf: // <20>
|
||||||
buf[i++]='\\';
|
if (bufIndex+5 >= MAX_LINE_BUFFER_SIZE)
|
||||||
buf[i++]='s';
|
{
|
||||||
buf[i++]='s';
|
wxString errBuf;
|
||||||
buf[i++]='\\';
|
errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
|
||||||
buf[i++]='/';
|
LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
|
||||||
|
OnError((char *)errBuf.c_str());
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
buf[bufIndex++]='\\';
|
||||||
|
buf[bufIndex++]='s';
|
||||||
|
buf[bufIndex++]='s';
|
||||||
|
buf[bufIndex++]='\\';
|
||||||
|
buf[bufIndex++]='/';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
buf[i++] = ch;
|
if (bufIndex >= MAX_LINE_BUFFER_SIZE)
|
||||||
|
{
|
||||||
|
wxString errBuf;
|
||||||
|
errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
|
||||||
|
LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
|
||||||
|
OnError((char *)errBuf.c_str());
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
buf[bufIndex++] = ch;
|
||||||
break;
|
break;
|
||||||
}
|
} // switch
|
||||||
|
} // else
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
buf[i] = 0;
|
buf[bufIndex] = 0;
|
||||||
fclose(Inputs[CurrentInputIndex]);
|
fclose(Inputs[CurrentInputIndex]);
|
||||||
Inputs[CurrentInputIndex] = NULL;
|
Inputs[CurrentInputIndex] = NULL;
|
||||||
if (CurrentInputIndex > 0)
|
if (CurrentInputIndex > 0)
|
||||||
@@ -533,7 +584,7 @@ bool read_a_line(char *buf)
|
|||||||
if (ch == 10)
|
if (ch == 10)
|
||||||
IncrementLineNumber();
|
IncrementLineNumber();
|
||||||
}
|
}
|
||||||
buf[i] = 0;
|
buf[bufIndex] = 0;
|
||||||
|
|
||||||
// Strip out comment environment
|
// Strip out comment environment
|
||||||
if (strncmp(buf, "\\begin{comment}", 15) == 0)
|
if (strncmp(buf, "\\begin{comment}", 15) == 0)
|
||||||
@@ -548,7 +599,7 @@ bool read_a_line(char *buf)
|
|||||||
int wordLen = 14;
|
int wordLen = 14;
|
||||||
char *fileName = buf + wordLen + 1;
|
char *fileName = buf + wordLen + 1;
|
||||||
|
|
||||||
int j = i - 1;
|
int j = bufIndex - 1;
|
||||||
buf[j] = 0;
|
buf[j] = 0;
|
||||||
|
|
||||||
// thing}\par -- eliminate the \par!
|
// thing}\par -- eliminate the \par!
|
||||||
@@ -558,16 +609,16 @@ bool read_a_line(char *buf)
|
|||||||
buf[j] = 0;
|
buf[j] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buf[j-1] == '}') buf[j-1] = 0; // Ignore final brace
|
if (buf[j-1] == '}')
|
||||||
|
buf[j-1] = 0; // Ignore final brace
|
||||||
|
|
||||||
wxString actualFile = TexPathList.FindValidPath(fileName);
|
wxString actualFile = TexPathList.FindValidPath(fileName);
|
||||||
currentFileName = actualFile;
|
currentFileName = actualFile;
|
||||||
if (actualFile == "")
|
if (actualFile == "")
|
||||||
{
|
{
|
||||||
char errBuf[300];
|
wxString errBuf;
|
||||||
strcpy(errBuf, "Could not find file: ");
|
errBuf.Printf("Could not find file: %s",fileName);
|
||||||
strncat(errBuf, fileName, 100);
|
OnError((char *)errBuf.c_str());
|
||||||
OnError(errBuf);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -575,6 +626,7 @@ bool read_a_line(char *buf)
|
|||||||
informStr.Printf("Processing: %s",actualFile.c_str());
|
informStr.Printf("Processing: %s",actualFile.c_str());
|
||||||
OnInform((char *)informStr.c_str());
|
OnInform((char *)informStr.c_str());
|
||||||
CurrentInputIndex ++;
|
CurrentInputIndex ++;
|
||||||
|
|
||||||
Inputs[CurrentInputIndex] = fopen(actualFile, "r");
|
Inputs[CurrentInputIndex] = fopen(actualFile, "r");
|
||||||
LineNumbers[CurrentInputIndex] = 1;
|
LineNumbers[CurrentInputIndex] = 1;
|
||||||
if (FileNames[CurrentInputIndex])
|
if (FileNames[CurrentInputIndex])
|
||||||
@@ -610,7 +662,7 @@ bool read_a_line(char *buf)
|
|||||||
|
|
||||||
char *fileName = buf + wordLen + 1;
|
char *fileName = buf + wordLen + 1;
|
||||||
|
|
||||||
int j = i - 1;
|
int j = bufIndex - 1;
|
||||||
buf[j] = 0;
|
buf[j] = 0;
|
||||||
|
|
||||||
// \input{thing}\par -- eliminate the \par!
|
// \input{thing}\par -- eliminate the \par!
|
||||||
@@ -622,7 +674,8 @@ bool read_a_line(char *buf)
|
|||||||
buf[j] = 0;
|
buf[j] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buf[j-1] == '}') buf[j-1] = 0; // Ignore final brace
|
if (buf[j-1] == '}')
|
||||||
|
buf[j-1] = 0; // Ignore final brace
|
||||||
|
|
||||||
// Remove backslashes from name
|
// Remove backslashes from name
|
||||||
wxString fileNameStr(fileName);
|
wxString fileNameStr(fileName);
|
||||||
@@ -645,10 +698,9 @@ bool read_a_line(char *buf)
|
|||||||
|
|
||||||
if (actualFile == "")
|
if (actualFile == "")
|
||||||
{
|
{
|
||||||
char errBuf[300];
|
wxString errBuf;
|
||||||
strcpy(errBuf, "Could not find file: ");
|
errBuf.Printf("Could not find file: %s",fileName);
|
||||||
strncat(errBuf, fileName, 100);
|
OnError((char *)errBuf.c_str());
|
||||||
OnError(errBuf);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -660,6 +712,7 @@ bool read_a_line(char *buf)
|
|||||||
informStr.Printf("Processing: %s",actualFile.c_str());
|
informStr.Printf("Processing: %s",actualFile.c_str());
|
||||||
OnInform((char *)informStr.c_str());
|
OnInform((char *)informStr.c_str());
|
||||||
CurrentInputIndex ++;
|
CurrentInputIndex ++;
|
||||||
|
|
||||||
Inputs[CurrentInputIndex] = fopen(actualFile, "r");
|
Inputs[CurrentInputIndex] = fopen(actualFile, "r");
|
||||||
LineNumbers[CurrentInputIndex] = 1;
|
LineNumbers[CurrentInputIndex] = 1;
|
||||||
if (FileNames[CurrentInputIndex])
|
if (FileNames[CurrentInputIndex])
|
||||||
@@ -668,10 +721,10 @@ bool read_a_line(char *buf)
|
|||||||
|
|
||||||
if (!Inputs[CurrentInputIndex])
|
if (!Inputs[CurrentInputIndex])
|
||||||
{
|
{
|
||||||
char errBuf[300];
|
wxString errBuf;
|
||||||
sprintf(errBuf, "Could not open include file %s", (const char*) actualFile);
|
errBuf.Printf("Could not open include file %s", (const char*) actualFile);
|
||||||
CurrentInputIndex --;
|
CurrentInputIndex --;
|
||||||
OnError(errBuf);
|
OnError((char *)errBuf.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool succ = read_a_line(buf);
|
bool succ = read_a_line(buf);
|
||||||
@@ -694,7 +747,7 @@ bool read_a_line(char *buf)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
return (ch == EOF);
|
return (ch == EOF);
|
||||||
}
|
} // read_a_line
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse newcommand
|
* Parse newcommand
|
||||||
@@ -771,7 +824,7 @@ bool ParseNewCommand(char *buffer, int *pos)
|
|||||||
|
|
||||||
void MacroError(char *buffer)
|
void MacroError(char *buffer)
|
||||||
{
|
{
|
||||||
char errBuf[300];
|
wxString errBuf;
|
||||||
char macroBuf[200];
|
char macroBuf[200];
|
||||||
macroBuf[0] = '\\';
|
macroBuf[0] = '\\';
|
||||||
int i = 1;
|
int i = 1;
|
||||||
@@ -785,9 +838,9 @@ void MacroError(char *buffer)
|
|||||||
if (i > 20)
|
if (i > 20)
|
||||||
macroBuf[20] = 0;
|
macroBuf[20] = 0;
|
||||||
|
|
||||||
sprintf(errBuf, "Could not find macro: %s at line %d, file %s",
|
errBuf.Printf("Could not find macro: %s at line %d, file %s",
|
||||||
macroBuf, (int)(LineNumbers[CurrentInputIndex]-1), FileNames[CurrentInputIndex]);
|
macroBuf, (int)(LineNumbers[CurrentInputIndex]-1), FileNames[CurrentInputIndex]);
|
||||||
OnError(errBuf);
|
OnError((char *)errBuf.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1492,11 +1545,8 @@ bool TexLoadFile(char *filename)
|
|||||||
|
|
||||||
if (line_buffer)
|
if (line_buffer)
|
||||||
delete line_buffer;
|
delete line_buffer;
|
||||||
#ifdef __WXMSW__
|
|
||||||
line_buffer = new char[600];
|
line_buffer = new char[MAX_LINE_BUFFER_SIZE];
|
||||||
#else
|
|
||||||
line_buffer = new char[11000];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Inputs[0] = fopen(filename, "r");
|
Inputs[0] = fopen(filename, "r");
|
||||||
LineNumbers[0] = 1;
|
LineNumbers[0] = 1;
|
||||||
@@ -3293,16 +3343,16 @@ bool DefaultOnArgument(int macroId, int arg_no, bool start)
|
|||||||
{
|
{
|
||||||
if (!ReadBib((char*) (const char*) actualFile))
|
if (!ReadBib((char*) (const char*) actualFile))
|
||||||
{
|
{
|
||||||
char buf[300];
|
wxString errBuf;
|
||||||
sprintf(buf, ".bib file %s not found or malformed", (const char*) actualFile);
|
errBuf.Printf(".bib file %s not found or malformed", (const char*) actualFile);
|
||||||
OnError(buf);
|
OnError((char *)errBuf.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char buf[300];
|
wxString errBuf;
|
||||||
sprintf(buf, ".bib file %s not found", fileBuf);
|
errBuf.Printf(".bib file %s not found", fileBuf);
|
||||||
OnError(buf);
|
OnError((char *)errBuf.c_str());
|
||||||
}
|
}
|
||||||
bibFile = ParseMultifieldString(allFiles, &pos);
|
bibFile = ParseMultifieldString(allFiles, &pos);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user