Skip to content

Commit

Permalink
Fix undefined behaviour in codegen of array reduction
Browse files Browse the repository at this point in the history
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]:
  - OpenModelica/OMCompiler#2402
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed May 2, 2018
1 parent 5a7ce94 commit 7fc85eb
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -6407,7 +6407,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
let &rangeExpPre = buffer ""
let arrayTypeResult = expTypeFromExpArray(r)
let arrIndex = match ri.path case IDENT(name="array") then tempDecl("int",&tmpVarDecls)
let foundFirst = if not ri.defaultValue then tempDecl("int",&tmpVarDecls)
let foundFirst = match ri.path case IDENT(name="array") then "" else (if not ri.defaultValue then tempDecl("int",&tmpVarDecls))
let resType = expTypeArrayIf(typeof(exp))
let res = contextCref(makeUntypedCrefIdent(ri.resultName), context, &auxFunction)
let &tmpVarDecls += '<%resType%> <%res%>;<%\n%>'
Expand Down Expand Up @@ -6449,7 +6449,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
else match ri.foldExp case SOME(fExp) then
let &foldExpPre = buffer ""
let fExpStr = daeExp(fExp, context, &bodyExpPre, &tmpVarDecls, &auxFunction)
if not ri.defaultValue then
if foundFirst then
<<
if(<%foundFirst%>)
{
Expand Down Expand Up @@ -6607,15 +6607,17 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
else
'simple_alloc_1d_<%arrayTypeResult%>(&<%res%>,<%length%>);'%>
>>
else if ri.defaultValue then
<<
<%&preDefault%>
<%res%> = <%defaultValue%>; /* defaultValue */
>>
else
<<
<%foundFirst%> = 0; /* <%dotPath(ri.path)%> lacks default-value */
>>)
(if foundFirst then
<<
<%foundFirst%> = 0; /* <%dotPath(ri.path)%> lacks default-value */
>>
else
<<
<%&preDefault%>
<%res%> = <%defaultValue%>; /* defaultValue */
>>)
)
let loop =
<<
while(1) {
Expand All @@ -6641,7 +6643,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
<%firstValue%>
<% if resTail then '<%resTail%> = &<%res%>;' %>
<%loop%>
<% if not ri.defaultValue then 'if (!<%foundFirst%>) <%generateThrow()%>;' %>
<% if foundFirst then 'if (!<%foundFirst%>) <%generateThrow()%>;' %>
<% if resTail then '*<%resTail%> = mmc_mk_nil();' %>
<% resTmp %> = <% res %>;
}<%\n%>
Expand Down

0 comments on commit 7fc85eb

Please sign in to comment.