From c8e12cf1ccb617dfd3e039b6224153a6df0749f1 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Thu, 30 Dec 2004 14:54:17 +0000 Subject: [PATCH] fix bug 1542 (crash wehn reading invalid equations) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_3_X@9413 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/ChangeLog | 9 ++++ src/mathed/math_hullinset.C | 24 +++++++-- src/mathed/math_hullinset.h | 2 + src/mathed/math_parser.C | 102 ++++++++++++++++++++++++++++-------- 4 files changed, 110 insertions(+), 27 deletions(-) diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index de26776e80..8324ffa47e 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,12 @@ +2004-12-22 Georg Baum + + * math_parser.C (addRow, addCol): new, try to add a row or column to + a MathGridInset + * math_parser.C (parse1): use addRow and addCol, fixes bug 1542 + * math_hullinset.[Ch] (rowChangeOK): new + * math_hullinset.C (addRow, delRow): check wether rows can be changed + * math_hullinset.C (delCol): don't delete if this is the only column + 2004-12-14 Angus Leeming * Makefile.am (INCLUDES): Remove trailing slash from -Ifoo/ diff --git a/src/mathed/math_hullinset.C b/src/mathed/math_hullinset.C index 81959c1ef7..2c8e570012 100644 --- a/src/mathed/math_hullinset.C +++ b/src/mathed/math_hullinset.C @@ -400,6 +400,16 @@ void MathHullInset::footer_write(WriteStream & os) const } +bool MathHullInset::rowChangeOK() const +{ + return + type_ == "eqnarray" || type_ == "align" || + type_ == "flalign" || type_ == "alignat" || + type_ == "xalignat" || type_ == "xxalignat" || + type_ == "gather" || type_ == "multline"; +} + + bool MathHullInset::colChangeOK() const { return @@ -410,6 +420,10 @@ bool MathHullInset::colChangeOK() const void MathHullInset::addRow(row_type row) { + if (!rowChangeOK()) { + lyxerr << "Can't change number of rows in '" << type_ << "'\n"; + return; + } nonum_.insert(nonum_.begin() + row + 1, !numberedType()); label_.insert(label_.begin() + row + 1, string()); MathGridInset::addRow(row); @@ -418,8 +432,10 @@ void MathHullInset::addRow(row_type row) void MathHullInset::delRow(row_type row) { - if (nrows() <= 1) + if (nrows() <= 1 || !rowChangeOK()) { + lyxerr << "Can't change number of rows in '" << type_ << "'\n"; return; + } MathGridInset::delRow(row); nonum_.erase(nonum_.begin() + row); label_.erase(label_.begin() + row); @@ -437,10 +453,10 @@ void MathHullInset::addCol(col_type col) void MathHullInset::delCol(col_type col) { - if (colChangeOK()) - MathGridInset::delCol(col); - else + if (ncols() <= 1 || !colChangeOK()) lyxerr << "Can't change number of columns in '" << type_ << "'\n"; + else + MathGridInset::delCol(col); } diff --git a/src/mathed/math_hullinset.h b/src/mathed/math_hullinset.h index 9430e5998c..2a71378bd8 100644 --- a/src/mathed/math_hullinset.h +++ b/src/mathed/math_hullinset.h @@ -110,6 +110,8 @@ private: char const * standardFont() const; /// consistency check void check() const; + /// can this change its number of rows? + bool rowChangeOK() const; /// can this change its number of cols? bool colChangeOK() const; diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index 0817041882..08a7372251 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -101,6 +101,65 @@ bool stared(string const & s) } +/*! + * Add the row \p cellrow to \p grid. + * \returns wether the row could be added. Adding a row can fail for + * environments like "equation" that have a fixed number of rows. + */ +bool addRow(MathGridInset & grid, MathGridInset::row_type & cellrow, + string const & vskip) +{ + ++cellrow; + if (cellrow == grid.nrows()) { + //lyxerr << "adding row " << cellrow << endl; + grid.addRow(cellrow - 1); + if (cellrow == grid.nrows()) { + // We can't add a row to this grid, so let's + // append the content of this cell to the previous + // one. + // This does not happen in well formed .lyx files, + // but LyX versions 1.3.x and older could create + // such files and tex2lyx can still do that. + --cellrow; + lyxerr << "ignoring extra row"; + if (!vskip.empty()) + lyxerr << " with extra space " << vskip; + lyxerr << '.' << endl; + return false; + } + } + grid.vcrskip(LyXLength(vskip), cellrow - 1); + return true; +} + + +/*! + * Add the column \p cellcol to \p grid. + * \returns wether the column could be added. Adding a column can fail for + * environments like "eqnarray" that have a fixed number of columns. + */ +bool addCol(MathGridInset & grid, MathGridInset::col_type & cellcol) +{ + ++cellcol; + if (cellcol == grid.ncols()) { + //lyxerr << "adding column " << cellcol << endl; + grid.addCol(cellcol - 1); + if (cellcol == grid.ncols()) { + // We can't add a column to this grid, so let's + // append the content of this cell to the previous + // one. + // This does not happen in well formed .lyx files, + // but LyX versions 1.3.x and older could create + // such files and tex2lyx can still do that. + --cellcol; + lyxerr << "ignoring extra column." << endl; + return false; + } + } + return true; +} + + // These are TeX's catcodes enum CatCode { catEscape, // 0 backslash @@ -722,13 +781,11 @@ void Parser::parse1(MathGridInset & grid, unsigned flags, } else if (t.cat() == catAlign) { - ++cellcol; - //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << "\n"; - if (cellcol == grid.ncols()) { - //lyxerr << "adding column " << cellcol << "\n"; - grid.addCol(cellcol - 1); - } - cell = &grid.cell(grid.index(cellrow, cellcol)); + //lyxerr << " column now " << (cellcol + 1) + // << " max: " << grid.ncols() << endl; + if (addCol(grid, cellcol)) + cell = &grid.cell(grid.index(cellrow, + cellcol)); } else if (t.cat() == catSuper || t.cat() == catSub) { @@ -876,14 +933,14 @@ void Parser::parse1(MathGridInset & grid, unsigned flags, } else if (t.cs() == "\\") { - grid.vcrskip(LyXLength(getArg('[', ']')), cellrow); - ++cellrow; - cellcol = 0; - if (cellrow == grid.nrows()) - grid.addRow(cellrow - 1); - if (grid.asHullInset()) - grid.asHullInset()->numbered(cellrow, numbered); - cell = &grid.cell(grid.index(cellrow, cellcol)); + if (addRow(grid, cellrow, getArg('[', ']'))) { + cellcol = 0; + if (grid.asHullInset()) + grid.asHullInset()->numbered( + cellrow, numbered); + cell = &grid.cell(grid.index(cellrow, + cellcol)); + } } #if 0 @@ -897,16 +954,15 @@ void Parser::parse1(MathGridInset & grid, unsigned flags, } // resize the table if necessary for (int i = 0; i < cols; ++i) { - ++cellcol; - if (cellcol == grid.ncols()) { - //lyxerr << "adding column " << cellcol << "\n"; - grid.addCol(cellcol - 1); + if (addCol(grid, cellcol)) { + cell = &grid.cell(grid.index( + cellrow, cellcol)); + // mark this as dummy + grid.cellinfo(grid.index( + cellrow, cellcol)).dummy_ = true; } - cell = &grid.cell(grid.index(cellrow, cellcol)); - // mark this as dummy - grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true; } - // the last cell is the real thng, not a dummy + // the last cell is the real thing, not a dummy grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false; // read special alignment