commit f4137463710fe806c15964cd6647a030e9231412
Author: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date:   Thu Feb 20 00:06:24 2025 +0100

    Use while() loops instead of fighting against for()
    
    In a loop where elements may be removed from a vector, it is dangerous
    and weird to decrease the counter so that it can be incremented by the
    for() ++i statement later without change. I suspect that things can go wrong
    when i == 0, and at least Coverity Scan agrees.
    
    Use while() instead.
---
 src/insets/InsetInclude.cpp  | 19 +++++++++++--------
 src/insets/InsetListings.cpp | 14 ++++++++------
 2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp
index f4ecaae8a0..4e68b9add5 100644
--- a/src/insets/InsetInclude.cpp
+++ b/src/insets/InsetInclude.cpp
@@ -695,34 +695,37 @@ void InsetInclude::latex(otexstream & os, OutputParams 
const & runparams) const
                char_type comma = replaceCommaInBraces(parameters);
                // Get float placement, language, caption, and
                // label, then remove the relative options if minted.
-               vector<docstring> opts =
-                       getVectorFromString(parameters, from_ascii(","), false);
+               vector<docstring> opts = getVectorFromString(parameters, 
from_ascii(","), false);
                vector<docstring> latexed_opts;
-               for (size_t i = 0; i < opts.size(); ++i) {
+               size_t i = 0;
+               while (i < opts.size()) {
                        // Restore replaced commas
                        opts[i] = subst(opts[i], comma, ',');
+                       // remove options that we support
                        if (use_minted && prefixIs(opts[i], 
from_ascii("float"))) {
                                if (prefixIs(opts[i], from_ascii("float=")))
                                        placement = opts[i].substr(6);
-                               opts.erase(opts.begin() + i--);
+                               opts.erase(opts.begin() + i);
                        } else if (use_minted && prefixIs(opts[i], 
from_ascii("language="))) {
                                language = opts[i].substr(9);
-                               opts.erase(opts.begin() + i--);
+                               opts.erase(opts.begin() + i);
                        } else if (prefixIs(opts[i], from_ascii("caption="))) {
                                caption = opts[i].substr(8);
                                caption = params().prepareCommand(runparams, 
stripOuterBraces(caption),
                                                                  
ParamInfo::HANDLING_LATEXIFY);
-                               opts.erase(opts.begin() + i--);
+                               opts.erase(opts.begin() + i);
                                if (!use_minted)
                                        
latexed_opts.push_back(from_ascii("caption={") + caption + "}");
                        } else if (prefixIs(opts[i], from_ascii("label="))) {
                                label = opts[i].substr(6);
                                label = params().prepareCommand(runparams, 
stripOuterBraces(label),
                                                                
ParamInfo::HANDLING_ESCAPE);
-                               opts.erase(opts.begin() + i--);
+                               opts.erase(opts.begin() + i);
                                if (!use_minted)
                                        
latexed_opts.push_back(from_ascii("label={") + label + "}");
-                       }
+                       } else
+                               ++i;
+
                        if (use_minted && !label.empty()) {
                                if (isfloat || !caption.empty())
                                        label = stripOuterBraces(label);
diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp
index 89b71e7e4d..1a1a547c39 100644
--- a/src/insets/InsetListings.cpp
+++ b/src/insets/InsetListings.cpp
@@ -207,18 +207,20 @@ void InsetListings::latex(otexstream & os, OutputParams 
const & runparams) const
        if (use_minted && (isfloat || contains(param_string, "language="))) {
                // Get float placement and/or language of the code,
                // then remove the relative options.
-               vector<string> opts =
-                       getVectorFromString(param_string, ",", false);
-               for (size_t i = 0; i < opts.size(); ++i) {
+               vector<string> opts = getVectorFromString(param_string, ",", 
false);
+               size_t i = 0;
+               // remove options that we handle
+               while (i < opts.size()) {
                        if (prefixIs(opts[i], "float")) {
                                if (prefixIs(opts[i], "float="))
                                        float_placement = opts[i].substr(6);
-                               opts.erase(opts.begin() + int(i--));
+                               opts.erase(opts.begin() + i);
                        }
                        else if (prefixIs(opts[i], "language=")) {
                                minted_language = opts[i].substr(9);
-                               opts.erase(opts.begin() + int(i--));
-                       }
+                               opts.erase(opts.begin() + i);
+                       } else
+                               ++i;
                }
                param_string = getStringFromVector(opts, ",");
        }
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to