[Ovmsdev] Use of deallocated memory in a bunch of places!?

Michael Geddes frog at bunyip.wheelycreek.net
Sun Oct 2 19:37:48 HKT 2022


Hi all,

I noticed some behavior on the *tpms* command I'd copied from the Kia/Kona
code, which sent me down a small rabbit hole.
What I was seeing was that the temperature was repeated instead of seeing
the pressure and temperature.

Even in vehicle.cpp, we can see code like this:

  const char* range_est = StdMetrics.ms_v_bat_range_est->AsUnitString("-",
rangeUnit, 0).c_str();
  if (*range_est != '-')
    writer->printf("Est. range: %s\n", range_est);

My C++ is a bit rusty, however I believe that the 'const char*' won't hold
the temporary std::string object returned by 'AsUnitString'.  To do that, I
believe we need to assign to a reference (which I know will work).  The
problem is that the original code will mostly work (especially in cases
like the above), as the deallocated heap memory won't have been reallocated.

  const std::string& range_est =
StdMetrics.ms_v_bat_range_est->AsUnitString("-", rangeUnit, 0);
  if (!is_empty_metric(range_est))
    writer->printf("Est. range: %s\n", range_est.c_str());

Where I've defined in vehicle.h the following:
inline bool is_empty_metric(const std::string &measure)
{
  return (measure == "") || (measure[0] == '-');
}

Will the above work functionally?  Would it be better to be:

return (measure =="") || (measure == "-")

This occurs in 8 files (outside my new Ioniq 5 files) that I have noted.

I would make a separate commit for this and do a pull request for it,

//.ichael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openvehicles.com/pipermail/ovmsdev/attachments/20221002/88bf8c80/attachment.htm>


More information about the OvmsDev mailing list