Here comes an early christmas gift: fix bug 1542.
The problem is that the math parser may try to add too many rows or columns
to equation types that don't support them, e.g the following two equations
do not work:
\begin{multline}
1&2\\
\end{multline}
\begin{equation}
1\\[2mm]
2\\
3
\end{equation}
Unfortunately LyX allowed to create such beasts until I fixed that a month
ago. The following patch prevents a crash when reading such files. OK to
apply? If yes, should I put it in 1.3, too?
Georg
Index: src/mathed/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/ChangeLog,v
retrieving revision 1.459
diff -u -r1.459 ChangeLog
--- src/mathed/ChangeLog 14 Dec 2004 10:41:08 -0000 1.459
+++ src/mathed/ChangeLog 22 Dec 2004 21:03:13 -0000
@@ -1,3 +1,9 @@
+2004-12-22 Georg Baum <[EMAIL PROTECTED]>
+
+ * 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 <[EMAIL PROTECTED]>
* Makefile.am (AM_CPPFLAGS): Remove trailing slash from -Ifoo/
Index: src/mathed/math_parser.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_parser.C,v
retrieving revision 1.306
diff -u -r1.306 math_parser.C
--- src/mathed/math_parser.C 7 Nov 2004 13:27:20 -0000 1.306
+++ src/mathed/math_parser.C 22 Dec 2004 21:07:00 -0000
@@ -102,6 +102,65 @@
}
+/*!
+ * 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 @@
}
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 @@
}
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,14 +938,13 @@
}
// 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
grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;