View Issue Details

IDProjectCategoryView StatusLast Update
0004003OpenFOAMBugpublic2023-08-09 11:12
Reporterpetteri Assigned Tohenry  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionno change required 
PlatformGNU/LinuxOSUbuntuOS Version15.04
Product Versiondev 
Summary0004003: Undefined behaviour in LUDecompose
DescriptionIn scalarMatrices.C line 85:

label m = matrix.m();
scalar vv[m];

where vv should be an array of length m. The row count of the input matrix is, in general, not known compile time making the array creation UB. Modern compilers however seem to be doing the right thing here as 'LUDecompose' is called all over the place.
Steps To ReproduceCall Foam::LUDecompose( scalarSquareMatrix&, labelList&)
TagsNo tags attached.

Activities

henry

2023-08-03 09:59

manager   ~0013087

> Modern compilers however seem to be doing the right thing here

Yes this is defined functionality in modern C and C++, or do you have evidence to suggest otherwise? Can you provide a case which fails?

petteri

2023-08-03 10:21

reporter   ~0013088

I assume that if the compilers are not able to deduce the array size at compile time they will reserve some relatively large buffer for the array. To trigger issues, the matrix (and the array) would have to be relatively large. Here's a demonstration:

https://godbolt.org/z/xn7G66efz

henry

2023-08-03 12:44

manager   ~0013089

According to the current C standard

scalar vv[m];

is valid and I don't recall any particular limit specified for m. Maybe there is a bug in the compiler you are using.

We don't have any OpenFOAM examples for which the current implementation fails.

petteri

2023-08-03 13:26

reporter   ~0013091

It appears that the C99 standard added support for variable length arrays (VLA) but they were again removed in C11, however, some compilers still provide the support for it. The C++ standard has never supported VLAs but some vendors are kind enough to support old C features. That being said, the maximum limit of the created buffer is totally up to the compiler vendor and likely depends on the system you are running on.

 I am not exactly sure where the LUDecompose function is called and what is a typical size for the input matrix. The ODE solvers make calls to LUDecompose but typically the chemistry ODE system is quite small. Therefore, I'm not sure if trusting UB in this case is a problem in practice but issues may appear in the future if the compiler vendors decide to treat VLAs in C++ somehow different.

henry

2023-08-03 13:54

manager   ~0013092

I am not able to reproduce the problem with any OpenFOAM cases we have or with any C++ compiler we have. If you are having particular difficulties with very LU decomposition of very large matrices I suggest you use a more appropriate LU decomposer for your cases.

henry

2023-08-03 13:54

manager   ~0013093

User support

Issue History

Date Modified Username Field Change
2023-08-03 09:25 petteri New Issue
2023-08-03 09:59 henry Note Added: 0013087
2023-08-03 10:21 petteri Note Added: 0013088
2023-08-03 12:44 henry Note Added: 0013089
2023-08-03 13:26 petteri Note Added: 0013091
2023-08-03 13:54 henry Note Added: 0013092
2023-08-03 13:54 henry Assigned To => henry
2023-08-03 13:54 henry Status new => closed
2023-08-03 13:54 henry Resolution open => no change required
2023-08-03 13:54 henry Note Added: 0013093