Fix two potential crashes due to invalid static_casts.

An example of a fatal function call is "gotoInset(this, NOTE_CODE, true)". Luckily we don't check for the contents in LFUN_NOTE_NEXT.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35860 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Vincent van Ravesteijn 2010-10-27 00:42:43 +00:00
parent 48a8d24328
commit faab3618ae

View File

@ -107,10 +107,7 @@ T * getInsetByCode(Cursor const & cur, InsetCode code)
return 0; return 0;
} }
/// Note that comparing contents can only be used for InsetCommand
bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
bool same_content);
bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes, bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
docstring const & contents) docstring const & contents)
{ {
@ -118,15 +115,17 @@ bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
while (tmpdit) { while (tmpdit) {
Inset const * inset = tmpdit.nextInset(); Inset const * inset = tmpdit.nextInset();
if (inset if (inset) {
&& std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() bool const valid_code = std::find(codes.begin(), codes.end(),
&& (contents.empty() || inset->lyxCode()) != codes.end();
//FIXME: This static_cast seems very dangerous. Does this InsetCommand const * ic = inset->asInsetCommand();
// mean that if contents is not empty, we must only be bool const same_or_no_contents = contents.empty()
// looking for InsetCommand's ?? || (ic && (ic->getFirstNonOptParam() == contents));
static_cast<InsetCommand const *>(inset)->getFirstNonOptParam() == contents)) {
dit = tmpdit; if (valid_code && same_or_no_contents) {
return true; dit = tmpdit;
return true;
}
} }
tmpdit.forwardInset(); tmpdit.forwardInset();
} }
@ -136,6 +135,7 @@ bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
/// Looks for next inset with one of the given codes. /// Looks for next inset with one of the given codes.
/// Note that same_content can only be used for InsetCommand
bool findInset(DocIterator & dit, vector<InsetCode> const & codes, bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
bool same_content) bool same_content)
{ {
@ -145,14 +145,14 @@ bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
if (!tmpdit) if (!tmpdit)
return false; return false;
if (same_content) { Inset const * inset = tmpdit.nextInset();
Inset const * inset = tmpdit.nextInset(); if (same_content && inset) {
if (inset InsetCommand const * ic = inset->asInsetCommand();
&& std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) { if (ic) {
//FIXME: This static_cast seems very dangerous. Does this bool const valid_code = std::find(codes.begin(), codes.end(),
// mean that if contents is not empty, we must only be ic->lyxCode()) != codes.end();
// looking for InsetCommand's ?? if (valid_code)
contents = static_cast<InsetCommand const *>(inset)->getFirstNonOptParam(); contents = ic->getFirstNonOptParam();
} }
} }