Skip to content

Commit

Permalink
sprintf() replaced by snprintf()
Browse files Browse the repository at this point in the history
Changes recommended by Ronald:
- .AsBearing() keeps the result in the range 0 ... 360° or 0 ... 2*Pi.
- sprintf() is a potential risk because it can write beyond the end of the buffer. snprintf() avoids this.
  • Loading branch information
Blaubart committed Apr 20, 2024
1 parent 0d2b33c commit 909bf18
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/Device/Driver/FreeVario.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,18 @@ FreeVarioDevice::OnSensorUpdate(const MoreData &basic)
}

if (basic.external_wind_available.IsValid() && basic.attitude.heading_available){
double relWindDirection = basic.external_wind.bearing.Degrees() - basic.attitude.heading.Degrees() + 180;
double norm = basic.external_wind.norm;
sprintf(nmeaOutbuffer,"PFV,AWD,%f", relWindDirection);
const Angle relWindDirection = (basic.external_wind.bearing - Angle::HalfCircle() - basic.attitude.heading).AsBearing();
snprintf(nmeaOutbuffer,sizeof(nmeaOutbuffer),"PFV,AWD,%f", relWindDirection.Degrees());
PortWriteNMEA(port, nmeaOutbuffer, env);
sprintf(nmeaOutbuffer,"PFV,AWS,%f", norm);
snprintf(nmeaOutbuffer,sizeof(nmeaOutbuffer),"PFV,AWS,%f", basic.external_wind.norm);
PortWriteNMEA(port, nmeaOutbuffer, env);
}

if (basic.external_instantaneous_wind_available.IsValid() && basic.attitude.heading_available){
double relWindDirection = basic.external_instantaneous_wind.bearing.Degrees() - basic.attitude.heading.Degrees() + 180;
double norm = basic.external_instantaneous_wind.norm;
sprintf(nmeaOutbuffer,"PFV,CWD,%f", relWindDirection);
const Angle relWindDirection = (basic.external_instantaneous_wind.bearing - Angle::HalfCircle() - basic.attitude.heading).AsBearing();
snprintf(nmeaOutbuffer,sizeof(nmeaOutbuffer),"PFV,CWD,%f", relWindDirection.Degrees());
PortWriteNMEA(port, nmeaOutbuffer, env);
sprintf(nmeaOutbuffer,"PFV,CWS,%f", norm);
snprintf(nmeaOutbuffer,sizeof(nmeaOutbuffer),"PFV,CWS,%f", basic.external_instantaneous_wind.norm);
PortWriteNMEA(port, nmeaOutbuffer, env);
}

Expand All @@ -298,11 +296,10 @@ FreeVarioDevice::OnCalculatedUpdate(const MoreData &basic,

char nmeaOutbuffer[80];
if (!basic.external_instantaneous_wind_available.IsValid() && calculated.wind.IsNonZero() && basic.track_available){
double relWindDirection = calculated.wind.bearing.Degrees() - basic.track.Degrees() + 180;
double norm = calculated.wind.norm;
sprintf(nmeaOutbuffer,"PFV,AWD,%f", relWindDirection);
const Angle relWindDirection = (calculated.wind.bearing - Angle::HalfCircle() - basic.track).AsBearing();
snprintf(nmeaOutbuffer,sizeof(nmeaOutbuffer),"PFV,AWD,%f", relWindDirection.Degrees());
PortWriteNMEA(port, nmeaOutbuffer, env);
sprintf(nmeaOutbuffer,"PFV,AWS,%f", norm);
snprintf(nmeaOutbuffer,sizeof(nmeaOutbuffer),"PFV,AWS,%f", calculated.wind.norm);
PortWriteNMEA(port, nmeaOutbuffer, env);
}

Expand Down

0 comments on commit 909bf18

Please sign in to comment.