From 54a21aa3059455aff408fc3b2fc90a80483959af Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Tue, 28 Dec 2004 15:43:29 +0000 Subject: [PATCH] fix bug 1542 (crash wehn reading invalid equations) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9408 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/ChangeLog | 6 +++ src/mathed/math_parser.C | 102 ++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 23 deletions(-) diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index 635825daa6..d9a21c774e 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,9 @@ +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 + 2004-12-14 Angus Leeming * Makefile.am (AM_CPPFLAGS): Remove trailing slash from -Ifoo/ diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index f16092f441..bb2730ea3d 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -102,6 +102,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 @@ -696,13 +755,11 @@ void Parser::parse1(MathGridInset & grid, unsigned flags, } else if (t.cat() == catAlign) { - ++cellcol; - //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << endl; - if (cellcol == grid.ncols()) { - //lyxerr << "adding column " << cellcol << endl; - 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) { @@ -860,14 +917,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 @@ -881,16 +938,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 << endl; - 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 -- 2.39.2