From 8182ce1fe9ba32b486cffb99b2212e62e820ee89 Mon Sep 17 00:00:00 2001 From: andreas Date: Sat, 30 Oct 2021 13:08:42 +0200 Subject: [PATCH] fix AIS decoding memory leaks --- lib/nmea2kto0183/N2kToNMEA0183Functions.h | 2 +- lib/nmea2ktoais/NMEA0183AISMsg.cpp | 14 ++++++-------- lib/nmea2ktoais/NMEA0183AISMsg.h | 3 ++- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/nmea2kto0183/N2kToNMEA0183Functions.h b/lib/nmea2kto0183/N2kToNMEA0183Functions.h index cf509d8..fb80e18 100644 --- a/lib/nmea2kto0183/N2kToNMEA0183Functions.h +++ b/lib/nmea2kto0183/N2kToNMEA0183Functions.h @@ -898,7 +898,7 @@ public: registerConverter(128275UL, &N2kToNMEA0183Functions::HandleLog); registerConverter(127245UL, &N2kToNMEA0183Functions::HandleRudder); registerConverter(130310UL, &N2kToNMEA0183Functions::HandleWaterTemp); -#define HANDLE_AIS 1 +#define HANDLE_AIS #ifdef HANDLE_AIS registerConverter(129038UL, &N2kToNMEA0183Functions::HandleAISClassAPosReport); // AIS Class A Position Report, Message Type 1 registerConverter(129039UL, &N2kToNMEA0183Functions::HandleAISClassBMessage18); // AIS Class B Position Report, Message Type 18 diff --git a/lib/nmea2ktoais/NMEA0183AISMsg.cpp b/lib/nmea2ktoais/NMEA0183AISMsg.cpp index e3d0d78..6abcf42 100644 --- a/lib/nmea2ktoais/NMEA0183AISMsg.cpp +++ b/lib/nmea2ktoais/NMEA0183AISMsg.cpp @@ -68,15 +68,13 @@ bool tNMEA0183AISMsg::AddIntToPayloadBin(int32_t ival, uint16_t countBits) { if ( (iAddPldBin + countBits ) >= AIS_BIN_MAX_LEN ) return false; // Is there room for any data - bset = ival; + AISBitSet bset(ival); PayloadBin[iAddPldBin]=0; uint16_t iAdd=iAddPldBin; - char buf[1]; for(int i = countBits-1; i >= 0 ; i--) { - sprintf(buf, "%d", (int) bset[i]); - PayloadBin[iAdd] = buf[0]; + PayloadBin[iAdd] = bset[i]?'1':'0'; iAdd++; } @@ -233,7 +231,7 @@ const char *tNMEA0183AISMsg::GetPayloadType5_Part1() { uint16_t lenbin = strlen( PayloadBin); if ( lenbin != 424 ) return nullptr; - char *to = (char*) malloc(337); + char to[337]; strncpy(to, PayloadBin, 336); // First Part is always 336 Length to[336]=0; @@ -250,7 +248,7 @@ const char *tNMEA0183AISMsg::GetPayloadType5_Part2() { if ( lenbin != 424 ) return nullptr; lenbin = 88; // Second Part is always 424 - 336 + 2 padding Zeros in Length - char *to = (char*) malloc(91); + char to[91]; strncpy(to, PayloadBin + 336, lenbin); to[88]='0'; to[89]='0'; to[90]=0; @@ -266,7 +264,7 @@ const char *tNMEA0183AISMsg::GetPayloadType24_PartA() { uint16_t lenbin = strlen( PayloadBin); if ( lenbin != 296 ) return nullptr; // too short for Part A - char *to = (char*) malloc(169); // Part A has Length 168 + char to[169]; // Part A has Length 168 *to = '\0'; for (int i=0; i<168; i++){ to[i] = PayloadBin[i]; @@ -284,7 +282,7 @@ const char *tNMEA0183AISMsg::GetPayloadType24_PartA() { const char *tNMEA0183AISMsg::GetPayloadType24_PartB() { uint16_t lenbin = strlen( PayloadBin); if ( lenbin != 296 ) return nullptr; // too short for Part B - char *to = (char*) malloc(169); // Part B has Length 168 + char to[169]; // Part B has Length 168 *to = '\0'; for (int i=0; i<39; i++){ to[i] = PayloadBin[i]; diff --git a/lib/nmea2ktoais/NMEA0183AISMsg.h b/lib/nmea2ktoais/NMEA0183AISMsg.h index cfa77ae..387f8f0 100644 --- a/lib/nmea2ktoais/NMEA0183AISMsg.h +++ b/lib/nmea2ktoais/NMEA0183AISMsg.h @@ -45,10 +45,11 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define BITSET_LENGTH 120 +typedef std::bitset AISBitSet; class tNMEA0183AISMsg : public tNMEA0183Msg { protected: // AIS-NMEA - std::bitset bset; + AISBitSet bset; static const char *EmptyAISField; // 6bits 0 not used yet..... static const char *AsciChar;