commit e8947ae2de4f8b253eee22cda08dd35eefbbf017
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Tue Mar 4 15:32:44 2025 +0100

    Avoid hypothetical integer overflow
    
    This is to please Coverity Scan.
    
    Rewrite InsetTabular::rowFromY and columnFromX like Row::x2pos, in
    order to avoid to compute r-1 on an unsigned value r that Coverity Scan
    thinks might be already 0.
---
 src/insets/InsetTabular.cpp | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp
index c741e72e60..fdf462a3b9 100644
--- a/src/insets/InsetTabular.cpp
+++ b/src/insets/InsetTabular.cpp
@@ -5089,16 +5089,18 @@ void InsetTabular::read(Lexer & lex)
 }
 
 
-int InsetTabular::rowFromY(Cursor & cur, int y) const
+int InsetTabular::rowFromY(Cursor & cur, int const y) const
 {
        // top y coordinate of tabular
        int h = yo(cur.bv()) - tabular.rowAscent(0) + 
tabular.offsetVAlignment();
-       row_type r = 0;
-       for (; r < tabular.nrows() && y > h; ++r)
-               h += tabular.rowAscent(r) + tabular.rowDescent(r)
+       for (row_type r = 0; r < tabular.nrows(); ++r) {
+               int const rh = tabular.rowAscent(r) + tabular.rowDescent(r)
                        + tabular.interRowSpace(r);
-
-       return r - 1;
+               if (y <= h + rh)
+                       return r;
+               h += rh;
+       }
+       return tabular.nrows() - 1;
 }
 
 
@@ -5106,10 +5108,12 @@ int InsetTabular::columnFromX(Cursor & cur, int x) const
 {
        // left x coordinate of tabular
        int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
-       col_type c = 0;
-       for (; c < tabular.ncols() && x > w; ++c)
+       for (col_type c = 0; c < tabular.ncols(); ++c) {
+               if (x <= w + tabular.cellWidth(c))
+                       return c;
                w += tabular.cellWidth(c);
-       return c - 1;
+       }
+       return tabular.ncols() - 1;
 }
 
 
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to