Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#6247 closed defect (invalid)

Uninitialized code in _functions.c

Reported by: Andreas Heuermann Owned by: Andreas Heuermann
Priority: high Milestone: 1.17.0
Component: Code Generation Version: v1.17.0-dev
Keywords: uninitialized memory Cc: daniel.bouskela@…

Description

While debugging ThermoSysPro.Examples.SimpleExamples.TestStaticDrum1 to find why it is not simulating I found a few issues using clang --analyze. One of the is uninitialized variables used in an equation in ThermoSysPro.Examples.SimpleExamples.TestStaticDrum1_functions.c

To reproduce run

loadModel(Modelica,{"3.2.3"}); getErrorString();
loadModel(ThermoSysPro); getErrorString();
setCommandLineOptions("-d=newInst"); getErrorString();
buildModel(ThermoSysPro.Examples.SimpleExamples.TestStaticDrum1); 

and add --analyze to CFLAGS in the generate makefile.

Running make again and clang will print:

ThermoSysPro.Examples.SimpleExamples.TestStaticDrum1_functions.c:19001:42: warning: The left operand of '-' is a garbage value
  _dxp = (-((_dh1satp + (_x) * (_dh2satp - _dh1satp)) / tmp6));

And in fact that is correct, _dh2satp and _dh1satp are not initialized.

See the code of the surrounding function:

DLLExport
ThermoSysPro_Properties_WaterSteamSimple_ThermoProperties__ph omc_ThermoSysPro_Properties_WaterSteamSimple_prop4__Ph(threadData_t *threadData, modelica_real _p, modelica_real _h)
{
  ThermoSysPro_Properties_WaterSteamSimple_ThermoProperties__ph _pro;
  modelica_real _h1sat;
  modelica_real _h2sat;
  modelica_real _T1sat;
  modelica_real _T2sat;
  modelica_real _d1sat;
  modelica_real _d2sat;
  modelica_real _u1sat;
  modelica_real _u2sat;
  modelica_real _s1sat;
  modelica_real _s2sat;
  modelica_real _cp1sat;
  modelica_real _cp2sat;
  modelica_real _dd1satp;
  modelica_real _dd2satp;
  modelica_real _du1satp;
  modelica_real _du2satp;
  modelica_real _dxh;
  modelica_real _dxp;
  modelica_real _dh1satp;
  modelica_real _dh2satp;
  modelica_real _x;
  modelica_real tmp1;
  modelica_real tmp2;
  modelica_real tmp3;
  modelica_real tmp4;
  modelica_real tmp5;
  modelica_real tmp6;
  modelica_real tmp7;
  modelica_real tmp8;
  modelica_real tmp9;
  modelica_real tmp10;
  modelica_real tmp11;
  modelica_real tmp12;
  modelica_real tmp13;
  modelica_real tmp14;
  modelica_real tmp15;
  modelica_real tmp16;
  _tailrecursive: OMC_LABEL_UNUSED
  ThermoSysPro_Properties_WaterSteamSimple_ThermoProperties__ph_construct(threadData, _pro); // _pro has no default value.
  // _h1sat has no default value.
  // _h2sat has no default value.
  // _T1sat has no default value.
  // _T2sat has no default value.
  // _d1sat has no default value.
  // _d2sat has no default value.
  // _u1sat has no default value.
  // _u2sat has no default value.
  // _s1sat has no default value.
  // _s2sat has no default value.
  // _cp1sat has no default value.
  // _cp2sat has no default value.
  // _dd1satp has no default value.
  // _dd2satp has no default value.
  // _du1satp has no default value.
  // _du2satp has no default value.
  // _dxh has no default value.
  // _dxp has no default value.
  // _dh1satp has no default value.
  // _dh2satp has no default value.
  // _x has no default value.
  _h1sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Enthalpy_h1sat__P(threadData, _p);

  _h2sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Enthalpy_h2sat__P(threadData, _p);

  _T1sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Temperature_T1__Ph(threadData, _p, _h1sat);

  _d1sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Density_d1__Ph(threadData, _p, _h1sat);

  _u1sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Energy_u1__Ph(threadData, _p, _h1sat);

  _s1sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Entropy_s1__Ph(threadData, _p, _h1sat);

  _cp1sat = omc_ThermoSysPro_Properties_WaterSteamSimple_HeatCapacity_cp1__Ph(threadData, _p, _h1sat);

  _T2sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Temperature_T2__Ph(threadData, _p, _h2sat);

  _d2sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Density_d2__Ph(threadData, _p, _h2sat);

  _u2sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Energy_u2__Ph(threadData, _p, _h2sat);

  _s2sat = omc_ThermoSysPro_Properties_WaterSteamSimple_Entropy_s2__Ph(threadData, _p, _h2sat);

  _cp2sat = omc_ThermoSysPro_Properties_WaterSteamSimple_HeatCapacity_cp2__Ph(threadData, _p, _h2sat);

  tmp1 = _h2sat - _h1sat;
  _x = (_h - _h1sat) / tmp1;

  _pro._x = _x;

  _pro._T = (1.0 - _x) * (_T1sat) + (_x) * (_T2sat);

  tmp2 = _d1sat;
  tmp3 = _d2sat;
  tmp4 = (1.0 - _x) / tmp2 + (_x) / tmp3;
  _pro._d = (1.0) / tmp4;

  _pro._u = (1.0 - _x) * (_u1sat) + (_x) * (_u2sat);

  _pro._s = (1.0 - _x) * (_s1sat) + (_x) * (_s2sat);

  _pro._cp = (1.0 - _x) * (_cp1sat) + (_x) * (_cp2sat);

  tmp5 = _h2sat - _h1sat;
  _dxh = (1.0) / tmp5;

  tmp6 = _h2sat - _h1sat;
  _dxp = (-((_dh1satp + (_x) * (_dh2satp - _dh1satp)) / tmp6));

  _dd1satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Density_dd1ph__Ph(threadData, _p, _h1sat);

  _du1satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Energy_du1ph__Ph(threadData, _p, _h1sat);

  _dd2satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Density_dd2ph__Ph(threadData, _p, _h2sat);

  _du2satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Energy_du2ph__Ph(threadData, _p, _h2sat);

  _dh1satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Enthalpy_dh1satp__P(threadData, _p);

  _dh2satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Enthalpy_dh2satp__P(threadData, _p);

  tmp7 = _pro._d;
  tmp8 = _d1sat;
  tmp9 = _d2sat;
  _pro._ddhp = (-(((tmp7 * tmp7)) * ((-((_dxh) / tmp8)) + (_dxh) / tmp9)));

  tmp10 = _pro._d;
  tmp11 = _d1sat;
  tmp12 = _d1sat;
  tmp13 = (tmp12 * tmp12);
  tmp14 = _d2sat;
  tmp15 = _d2sat;
  tmp16 = (tmp15 * tmp15);
  _pro._ddph = (-(((tmp10 * tmp10)) * ((-((_dxp) / tmp11)) + ((1.0 - _x) * ((-((1.0) / tmp13)))) * (_dd1satp) + (_dxp) / tmp14 + ((_x) * ((-((1.0) / tmp16)))) * (_dd2satp))));

  _pro._duph = (-((_dxp) * (_u1sat))) + (1.0 - _x) * (_du1satp) + (_dxp) * (_u2sat) + (_x) * (_du2satp);

  _pro._duhp = (-((_dxh) * (_u1sat))) + (_dxh) * (_u2sat);
  _return: OMC_LABEL_UNUSED
  return _pro;
}

Change History (5)

comment:1 by Andreas Heuermann, 4 years ago

The solution will be to switch

  _dxp = (-((_dh1satp + (_x) * (_dh2satp - _dh1satp)) / tmp6));

and

  _dd1satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Density_dd1ph__Ph(threadData, _p, _h1sat);

  _du1satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Energy_du1ph__Ph(threadData, _p, _h1sat);

  _dd2satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Density_dd2ph__Ph(threadData, _p, _h2sat);

  _du2satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Energy_du2ph__Ph(threadData, _p, _h2sat);

  _dh1satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Enthalpy_dh1satp__P(threadData, _p);

  _dh2satp = omc_ThermoSysPro_Properties_WaterSteamSimple_Enthalpy_dh2satp__P(threadData, _p);

Probably a missing listReverse somewhere.

comment:2 by Andreas Heuermann, 4 years ago

It turns out that ThermoSysPro 3.2/Properties/WaterSteamSimple/prop4_Ph.mo is (probably) wrong.
In an algorithm section

dxp := -(dh1satp + x*(dh2satp - dh1satp))/(h2sat - h1sat);

is using dh1satp and dh2satp before they are initialized a few lines later.

comment:4 by Andreas Heuermann, 4 years ago

Resolution: invalid
Status: newclosed

This needs to be solved by the developers of the library, so I'm closing this ticket.

comment:5 by Francesco Casella, 4 years ago

Cc: daniel.bouskela@… added
Note: See TracTickets for help on using tickets.