Display a "plugin not found" error message when a invalid command looks like a plugin call.

git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@6541 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
anders_k 2014-08-21 13:15:07 +00:00
parent 5ae69eb9cc
commit d42a4f16a6
5 changed files with 34 additions and 15 deletions

View file

@ -51,8 +51,9 @@ bool contains(const C& cntnr, const K& key)
template <class C, class K> template <class C, class K>
const typename C::const_iterator get_iterator(const C& cntnr, const K& key) const typename C::const_iterator get_iterator(const C& cntnr, const K& key)
{ {
assert( contains(cntnr, key) ); const typename C::const_iterator it = cntnr.find(key);
return cntnr.find(key); assert(cntnr.end() != it);
return it;
} }
template <class C, class K> template <class C, class K>
@ -245,6 +246,16 @@ bool Plugins::IsPluginCommand(const tstring& token) const
return contains(m_commands, token); return contains(m_commands, token);
} }
bool Plugins::IsKnownPlugin(const tstring& token) const
{
const tstring dllname = GetDllName(token);
return contains(m_dllname_to_path, dllname);
}
bool Plugins::IsPluginCallSyntax(const tstring& token)
{
const tstring dllname = GetDllName(token);
return dllname.length() + 2 < token.length();
}
#endif #endif

View file

@ -51,9 +51,11 @@ class Plugins
bool Initialize(const TCHAR*arcsubdir, bool displayInfo); bool Initialize(const TCHAR*arcsubdir, bool displayInfo);
void AddPluginsDir(const tstring& path, bool displayInfo); void AddPluginsDir(const tstring& path, bool displayInfo);
bool IsPluginCommand(const tstring& command) const; bool IsPluginCommand(const tstring& command) const;
bool IsKnownPlugin(const tstring& token) const;
bool GetCommandInfo(const tstring&command, tstring&canoniccmd, tstring&dllPath); bool GetCommandInfo(const tstring&command, tstring&canoniccmd, tstring&dllPath);
int GetDllDataHandle(bool uninst, const tstring& command) const; int GetDllDataHandle(bool uninst, const tstring& command) const;
void SetDllDataHandle(bool uninst, tstring&canoniccmd, int dataHandle); void SetDllDataHandle(bool uninst, tstring&canoniccmd, int dataHandle);
static bool IsPluginCallSyntax(const tstring& token);
private: // methods private: // methods
void GetExports(const tstring &pathToDll, bool displayInfo); void GetExports(const tstring &pathToDll, bool displayInfo);

View file

@ -177,7 +177,7 @@ class CEXEBuild {
bool is_ppbranch_token(TCHAR *s); bool is_ppbranch_token(TCHAR *s);
bool is_pp_token(int tkid); bool is_pp_token(int tkid);
bool is_unsafe_pp_token(int tkid); bool is_unsafe_pp_token(int tkid);
int get_commandtoken(TCHAR *s, int *np, int *op, int *pos); int get_commandtoken(const TCHAR *s, int *np, int *op, int *pos);
const TCHAR* get_commandtoken_name(int tok); const TCHAR* get_commandtoken_name(int tok);
/** /**
@ -186,7 +186,7 @@ class CEXEBuild {
* @return TP_FUNC, TP_SEC, TP_PAGEEX, TP_GLOBAL. * @return TP_FUNC, TP_SEC, TP_PAGEEX, TP_GLOBAL.
*/ */
int GetCurrentTokenPlace(); int GetCurrentTokenPlace();
int IsTokenPlacedRight(int pos, TCHAR *tok); int IsTokenPlacedRight(int pos, const TCHAR *tok);
// script.cpp // script.cpp
#ifdef NSIS_SUPPORT_STANDARD_PREDEFINES #ifdef NSIS_SUPPORT_STANDARD_PREDEFINES

View file

@ -385,23 +385,24 @@ int CEXEBuild::doParse(const TCHAR *str)
parse_again: parse_again:
if (line.getnumtokens() < 1) return PS_OK; if (line.getnumtokens() < 1) return PS_OK;
const TCHAR* const tokstr0 = line.gettoken_str(0);
int np,op,pos; int np,op,pos;
int tkid=get_commandtoken(line.gettoken_str(0),&np,&op,&pos); int tkid=get_commandtoken(tokstr0,&np,&op,&pos);
if (tkid == -1) if (tkid == -1)
{ {
TCHAR *p=line.gettoken_str(0); const TCHAR *p=tokstr0;
if (p[0] && p[_tcslen(p)-1]==_T(':')) if (p[0] && p[_tcslen(p)-1]==_T(':'))
{ {
if (p[0] == _T('!') || (p[0] >= _T('0') && p[0] <= _T('9')) || p[0] == _T('$') || p[0] == _T('-') || p[0] == _T('+')) if (p[0] == _T('!') || (p[0] >= _T('0') && p[0] <= _T('9')) || p[0] == _T('$') || p[0] == _T('-') || p[0] == _T('+'))
{ {
ERROR_MSG(_T("Invalid label: %") NPRIs _T(" (labels cannot begin with !, $, -, +, or 0-9)\n"),line.gettoken_str(0)); ERROR_MSG(_T("Invalid label: %") NPRIs _T(" (labels cannot begin with !, $, -, +, or 0-9)\n"),tokstr0);
return PS_ERROR; return PS_ERROR;
} }
extern FILE *g_output; extern FILE *g_output;
if (preprocessonly) if (preprocessonly)
_ftprintf(g_output,_T("%") NPRIs _T("\n"),line.gettoken_str(0)); _ftprintf(g_output,_T("%") NPRIs _T("\n"),tokstr0);
else else
if (add_label(line.gettoken_str(0))) return PS_ERROR; if (add_label(tokstr0)) return PS_ERROR;
line.eattoken(); line.eattoken();
goto parse_again; goto parse_again;
} }
@ -411,7 +412,7 @@ parse_again:
// We didn't recognise this command, could it be the name of a // We didn't recognise this command, could it be the name of a
// function exported from a dll? // function exported from a dll?
// Plugins cannot be called in global scope so there is no need to initialize the list first // Plugins cannot be called in global scope so there is no need to initialize the list first
if (m_pPlugins && m_pPlugins->IsPluginCommand(line.gettoken_str(0))) if (m_pPlugins && m_pPlugins->IsPluginCommand(tokstr0))
{ {
np = 0; // parameters are optional np = 0; // parameters are optional
op = -1; // unlimited number of optional parameters op = -1; // unlimited number of optional parameters
@ -421,18 +422,23 @@ parse_again:
else else
#endif #endif
{ {
ERROR_MSG(_T("Invalid command: %") NPRIs _T("\n"),line.gettoken_str(0)); #ifdef NSIS_CONFIG_PLUGIN_SUPPORT
if (Plugins::IsPluginCallSyntax(tokstr0) && (!m_pPlugins || !m_pPlugins->IsKnownPlugin(tokstr0)))
ERROR_MSG(_T("Plugin not found, cannot call %") NPRIs _T("\n"),tokstr0);
else
#endif
ERROR_MSG(_T("Invalid command: %") NPRIs _T("\n"),tokstr0);
return PS_ERROR; return PS_ERROR;
} }
} }
if (IsTokenPlacedRight(pos, line.gettoken_str(0)) != PS_OK) if (IsTokenPlacedRight(pos, tokstr0) != PS_OK)
return PS_ERROR; return PS_ERROR;
int v=line.getnumtokens()-(np+1); int v=line.getnumtokens()-(np+1);
if (v < 0 || (op >= 0 && v > op)) // opt_parms is -1 for unlimited if (v < 0 || (op >= 0 && v > op)) // opt_parms is -1 for unlimited
{ {
ERROR_MSG(_T("%") NPRIs _T(" expects %d"),line.gettoken_str(0),np); ERROR_MSG(_T("%") NPRIs _T(" expects %d"),tokstr0,np);
if (op < 0) ERROR_MSG(_T("+")); if (op < 0) ERROR_MSG(_T("+"));
if (op > 0) ERROR_MSG(_T("-%d"),op+np); if (op > 0) ERROR_MSG(_T("-%d"),op+np);
ERROR_MSG(_T(" parameters, got %d.\n"),line.getnumtokens()-1); ERROR_MSG(_T(" parameters, got %d.\n"),line.getnumtokens()-1);

View file

@ -360,7 +360,7 @@ bool CEXEBuild::is_unsafe_pp_token(int tkid)
return false; return false;
} }
int CEXEBuild::get_commandtoken(TCHAR *s, int *np, int *op, int *pos) int CEXEBuild::get_commandtoken(const TCHAR *s, int *np, int *op, int *pos)
{ {
for (int x = 0; x < TOK__LAST; x ++) for (int x = 0; x < TOK__LAST; x ++)
if (!_tcsicmp(tokenlist[x].name,s)) if (!_tcsicmp(tokenlist[x].name,s))
@ -384,7 +384,7 @@ int CEXEBuild::GetCurrentTokenPlace()
return TP_GLOBAL; return TP_GLOBAL;
} }
int CEXEBuild::IsTokenPlacedRight(int pos, TCHAR *tok) int CEXEBuild::IsTokenPlacedRight(int pos, const TCHAR *tok)
{ {
if (preprocessonly) if (preprocessonly)
return PS_OK; return PS_OK;