Skip to content

Commit

Permalink
Avoid tampering with special commands while doing markdown parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
doxygen committed Dec 21, 2021
1 parent 2d1f4ed commit 511e7ae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/markdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ QCString Markdown::isBlockCommand(const char *data,int offset,int size)
{
TRACE(data);

using EndBlockFunc = QCString(*)(const std::string &blockName,bool openBracket,char nextChar);
using EndBlockFunc = QCString (*)(const std::string &blockName,bool openBracket,char nextChar);

static const auto getEndBlock = [](const std::string &blockName,bool,char) -> QCString
{
Expand Down Expand Up @@ -439,6 +439,52 @@ QCString Markdown::isBlockCommand(const char *data,int offset,int size)
return result;
}

int Markdown::isSpecialCommand(const char *data,int offset,int size)
{
TRACE(data);

using EndCmdFunc = int (*)(const std::string &cmdName,const char *data,int offset,int size);

auto endOfLine = [](const std::string &cmdName,const char *data,int offset,int size) -> int
{
// skip until the end of line (allowing line continuation characters)
char lc = 0;
char c;
while (offset<size && ((c=data[offset])!='\n' || lc=='\\'))
{
if (c=='\\') lc='\\'; // last character was a line continuation
else if (c!=' ') lc=0; // rest line continuation
offset++;
}
return offset;
};

static const std::unordered_map<std::string,EndCmdFunc> cmdNames =
{
{ "concept", endOfLine },
{ "idlexcept", endOfLine },
{ "interface", endOfLine },
{ "protocol", endOfLine },
{ "struct", endOfLine },
{ "union", endOfLine },
};

bool isEscaped = offset>0 && (data[-1]=='\\' || data[-1]=='@');
if (isEscaped) return 0;

int end=1;
while (end<size && (data[end]>='a' && data[end]<='z')) end++;
if (end==1) return 0;
std::string cmdName(data+1,end-1);
auto it = cmdNames.find(cmdName);
int result=0;
if (it!=cmdNames.end())
{
result = it->second(cmdName,data,end,size);
}
return result;
}

/** looks for the next emph char, skipping other constructs, and
* stopping when either it is found, or we are at the end of a paragraph.
*/
Expand Down Expand Up @@ -1376,6 +1422,12 @@ int Markdown::processSpecialCommand(const char *data, int offset, int size)
i++;
}
}
int endPos = isSpecialCommand(data,offset,size);
if (endPos>0)
{
m_out.addStr(data,endPos);
return endPos;
}
if (size>1 && data[0]=='\\') // escaped characters
{
char c=data[1];
Expand Down
1 change: 1 addition & 0 deletions src/markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Markdown
QCString processQuotations(const QCString &s,int refIndent);
QCString processBlocks(const QCString &s,int indent);
QCString isBlockCommand(const char *data,int offset,int size);
int isSpecialCommand(const char *data,int offset,int size);
void findEndOfLine(const char *data,int size,int &pi,int&i,int &end);
int processHtmlTagWrite(const char *data,int offset,int size,bool doWrite);
int processHtmlTag(const char *data,int offset,int size);
Expand Down

0 comments on commit 511e7ae

Please sign in to comment.