Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 7fc85eb

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Fix undefined behaviour in codegen of array reduction
There was a condition variable that was never assigned to that caused the C-compiler to either assume the reduction always failed, always succeeded or at runtime get random memory that made the choice. The variable and superfluous check have been removed. Belonging to [master]: - #2402
1 parent 5a7ce94 commit 7fc85eb

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Compiler/Template/CodegenCFunctions.tpl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6407,7 +6407,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
64076407
let &rangeExpPre = buffer ""
64086408
let arrayTypeResult = expTypeFromExpArray(r)
64096409
let arrIndex = match ri.path case IDENT(name="array") then tempDecl("int",&tmpVarDecls)
6410-
let foundFirst = if not ri.defaultValue then tempDecl("int",&tmpVarDecls)
6410+
let foundFirst = match ri.path case IDENT(name="array") then "" else (if not ri.defaultValue then tempDecl("int",&tmpVarDecls))
64116411
let resType = expTypeArrayIf(typeof(exp))
64126412
let res = contextCref(makeUntypedCrefIdent(ri.resultName), context, &auxFunction)
64136413
let &tmpVarDecls += '<%resType%> <%res%>;<%\n%>'
@@ -6449,7 +6449,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
64496449
else match ri.foldExp case SOME(fExp) then
64506450
let &foldExpPre = buffer ""
64516451
let fExpStr = daeExp(fExp, context, &bodyExpPre, &tmpVarDecls, &auxFunction)
6452-
if not ri.defaultValue then
6452+
if foundFirst then
64536453
<<
64546454
if(<%foundFirst%>)
64556455
{
@@ -6607,15 +6607,17 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
66076607
else
66086608
'simple_alloc_1d_<%arrayTypeResult%>(&<%res%>,<%length%>);'%>
66096609
>>
6610-
else if ri.defaultValue then
6611-
<<
6612-
<%&preDefault%>
6613-
<%res%> = <%defaultValue%>; /* defaultValue */
6614-
>>
66156610
else
6616-
<<
6617-
<%foundFirst%> = 0; /* <%dotPath(ri.path)%> lacks default-value */
6618-
>>)
6611+
(if foundFirst then
6612+
<<
6613+
<%foundFirst%> = 0; /* <%dotPath(ri.path)%> lacks default-value */
6614+
>>
6615+
else
6616+
<<
6617+
<%&preDefault%>
6618+
<%res%> = <%defaultValue%>; /* defaultValue */
6619+
>>)
6620+
)
66196621
let loop =
66206622
<<
66216623
while(1) {
@@ -6641,7 +6643,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
66416643
<%firstValue%>
66426644
<% if resTail then '<%resTail%> = &<%res%>;' %>
66436645
<%loop%>
6644-
<% if not ri.defaultValue then 'if (!<%foundFirst%>) <%generateThrow()%>;' %>
6646+
<% if foundFirst then 'if (!<%foundFirst%>) <%generateThrow()%>;' %>
66456647
<% if resTail then '*<%resTail%> = mmc_mk_nil();' %>
66466648
<% resTmp %> = <% res %>;
66476649
}<%\n%>

0 commit comments

Comments
 (0)