From: Jean-Marc Lasgouttes Date: Tue, 1 Oct 2024 09:45:36 +0000 (+0200) Subject: Do not pass a vector by value to getArgInset X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=bc624cc7809cef758939aa7fa01550ccb9d0616a;p=lyx.git Do not pass a vector by value to getArgInset Instead of passing the vector by value so that stuff can be added to it in the function, add required elements of arguments to a second vector. While we are at it, simplify the code a bit by using a range-for and auto keyword. Spotted by Coverity scan --- diff --git a/src/output_latex.cpp b/src/output_latex.cpp index 478c99698d..9fdc901727 100644 --- a/src/output_latex.cpp +++ b/src/output_latex.cpp @@ -501,13 +501,10 @@ void TeXEnvironment(Buffer const & buf, Text const & text, } -// FIXME: pass the \c required vector by reference and add the stuff -// from \c latexargs to a different vector. This avoids a copy and -// (more importantly?) a coverity defect. void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs, map const & ilist, - vector required, string const & prefix) + vector const & required, string const & prefix) { size_t const argnr = latexargs.size(); if (argnr == 0) @@ -515,24 +512,24 @@ void getArgInsets(otexstream & os, OutputParams const & runparams, // Default and preset args are always output, so if they require // other arguments, consider this. + vector required_args; for (auto const & larg : latexargs) { Layout::latexarg const & arg = larg.second; if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.required.empty()) { - vector req = getVectorFromString(arg.required); - required.insert(required.end(), req.begin(), req.end()); - } + vector const req = getVectorFromString(arg.required); + required_args.insert(required_args.end(), req.begin(), req.end()); + } } for (size_t i = 1; i <= argnr; ++i) { - map::const_iterator lit = ilist.find(i); + auto const lit = ilist.find(i); bool inserted = false; if (lit != ilist.end()) { InsetArgument const * ins = lit->second; if (ins) { - Layout::LaTeXArgMap::const_iterator const lait = - latexargs.find(ins->name()); + auto const lait = latexargs.find(ins->name()); if (lait != latexargs.end()) { - Layout::latexarg arg = lait->second; + Layout::latexarg const arg = lait->second; docstring ldelim; docstring rdelim; if (!arg.nodelims) { @@ -553,12 +550,10 @@ void getArgInsets(otexstream & os, OutputParams const & runparams, } } if (!inserted) { - Layout::LaTeXArgMap::const_iterator lait = latexargs.begin(); - Layout::LaTeXArgMap::const_iterator const laend = latexargs.end(); - for (; lait != laend; ++lait) { + for (auto const & la_p : latexargs) { string const name = prefix + convert(i); - if ((*lait).first == name) { - Layout::latexarg arg = (*lait).second; + if (la_p.first == name) { + Layout::latexarg const arg = la_p.second; docstring preset = arg.presetarg; if (!arg.defaultarg.empty()) { if (!preset.empty()) @@ -578,7 +573,9 @@ void getArgInsets(otexstream & os, OutputParams const & runparams, from_ascii("]") : arg.rdelim; os << ldelim << preset << rdelim; } else if (find(required.begin(), required.end(), - (*lait).first) != required.end()) { + la_p.first) != required.end() + || find(required_args.begin(), required_args.end(), + la_p.first) != required_args.end()) { docstring ldelim = arg.ldelim.empty() ? from_ascii("[") : arg.ldelim; docstring rdelim = arg.rdelim.empty() ?