Bugfix: undefined behaviour in AbstractExternalFunctionNode::prepareForDerivation()

Input and output ranges should not overlap when calling std::set_union(),
otherwise the behaviour is undefined.

It seems that in this precise case the computation would still be
correct (though inefficient), because of the properties of std::set or because
of the specific implementation in libstdc++. But it’s better to be on the safe
side.
master
Sébastien Villemot 2023-03-02 15:55:51 +01:00
parent 41052ccb74
commit fe83933b1d
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
1 changed files with 9 additions and 5 deletions

View File

@ -6758,11 +6758,15 @@ AbstractExternalFunctionNode::prepareForDerivation()
non_null_derivatives = arguments.at(0)->non_null_derivatives;
for (int i = 1; i < static_cast<int>(arguments.size()); i++)
set_union(non_null_derivatives.begin(),
non_null_derivatives.end(),
arguments.at(i)->non_null_derivatives.begin(),
arguments.at(i)->non_null_derivatives.end(),
inserter(non_null_derivatives, non_null_derivatives.begin()));
{
set<int> non_null_derivatives_tmp;
set_union(non_null_derivatives.begin(),
non_null_derivatives.end(),
arguments.at(i)->non_null_derivatives.begin(),
arguments.at(i)->non_null_derivatives.end(),
inserter(non_null_derivatives_tmp, non_null_derivatives_tmp.begin()));
non_null_derivatives = move(non_null_derivatives_tmp);
}
preparedForDerivation = true;
}