fix AIS decoding memory leaks

This commit is contained in:
andreas 2021-10-30 13:08:42 +02:00
parent e7b2c6e756
commit 8182ce1fe9
3 changed files with 9 additions and 10 deletions

View File

@ -898,7 +898,7 @@ public:
registerConverter(128275UL, &N2kToNMEA0183Functions::HandleLog); registerConverter(128275UL, &N2kToNMEA0183Functions::HandleLog);
registerConverter(127245UL, &N2kToNMEA0183Functions::HandleRudder); registerConverter(127245UL, &N2kToNMEA0183Functions::HandleRudder);
registerConverter(130310UL, &N2kToNMEA0183Functions::HandleWaterTemp); registerConverter(130310UL, &N2kToNMEA0183Functions::HandleWaterTemp);
#define HANDLE_AIS 1 #define HANDLE_AIS
#ifdef HANDLE_AIS #ifdef HANDLE_AIS
registerConverter(129038UL, &N2kToNMEA0183Functions::HandleAISClassAPosReport); // AIS Class A Position Report, Message Type 1 registerConverter(129038UL, &N2kToNMEA0183Functions::HandleAISClassAPosReport); // AIS Class A Position Report, Message Type 1
registerConverter(129039UL, &N2kToNMEA0183Functions::HandleAISClassBMessage18); // AIS Class B Position Report, Message Type 18 registerConverter(129039UL, &N2kToNMEA0183Functions::HandleAISClassBMessage18); // AIS Class B Position Report, Message Type 18

View File

@ -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 if ( (iAddPldBin + countBits ) >= AIS_BIN_MAX_LEN ) return false; // Is there room for any data
bset = ival; AISBitSet bset(ival);
PayloadBin[iAddPldBin]=0; PayloadBin[iAddPldBin]=0;
uint16_t iAdd=iAddPldBin; uint16_t iAdd=iAddPldBin;
char buf[1];
for(int i = countBits-1; i >= 0 ; i--) { for(int i = countBits-1; i >= 0 ; i--) {
sprintf(buf, "%d", (int) bset[i]); PayloadBin[iAdd] = bset[i]?'1':'0';
PayloadBin[iAdd] = buf[0];
iAdd++; iAdd++;
} }
@ -233,7 +231,7 @@ const char *tNMEA0183AISMsg::GetPayloadType5_Part1() {
uint16_t lenbin = strlen( PayloadBin); uint16_t lenbin = strlen( PayloadBin);
if ( lenbin != 424 ) return nullptr; if ( lenbin != 424 ) return nullptr;
char *to = (char*) malloc(337); char to[337];
strncpy(to, PayloadBin, 336); // First Part is always 336 Length strncpy(to, PayloadBin, 336); // First Part is always 336 Length
to[336]=0; to[336]=0;
@ -250,7 +248,7 @@ const char *tNMEA0183AISMsg::GetPayloadType5_Part2() {
if ( lenbin != 424 ) return nullptr; if ( lenbin != 424 ) return nullptr;
lenbin = 88; // Second Part is always 424 - 336 + 2 padding Zeros in Length 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); strncpy(to, PayloadBin + 336, lenbin);
to[88]='0'; to[89]='0'; to[90]=0; to[88]='0'; to[89]='0'; to[90]=0;
@ -266,7 +264,7 @@ const char *tNMEA0183AISMsg::GetPayloadType24_PartA() {
uint16_t lenbin = strlen( PayloadBin); uint16_t lenbin = strlen( PayloadBin);
if ( lenbin != 296 ) return nullptr; // too short for Part A 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'; *to = '\0';
for (int i=0; i<168; i++){ for (int i=0; i<168; i++){
to[i] = PayloadBin[i]; to[i] = PayloadBin[i];
@ -284,7 +282,7 @@ const char *tNMEA0183AISMsg::GetPayloadType24_PartA() {
const char *tNMEA0183AISMsg::GetPayloadType24_PartB() { const char *tNMEA0183AISMsg::GetPayloadType24_PartB() {
uint16_t lenbin = strlen( PayloadBin); uint16_t lenbin = strlen( PayloadBin);
if ( lenbin != 296 ) return nullptr; // too short for Part B 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'; *to = '\0';
for (int i=0; i<39; i++){ for (int i=0; i<39; i++){
to[i] = PayloadBin[i]; to[i] = PayloadBin[i];

View File

@ -45,10 +45,11 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define BITSET_LENGTH 120 #define BITSET_LENGTH 120
typedef std::bitset<BITSET_LENGTH> AISBitSet;
class tNMEA0183AISMsg : public tNMEA0183Msg { class tNMEA0183AISMsg : public tNMEA0183Msg {
protected: // AIS-NMEA protected: // AIS-NMEA
std::bitset<BITSET_LENGTH> bset; AISBitSet bset;
static const char *EmptyAISField; // 6bits 0 not used yet..... static const char *EmptyAISField; // 6bits 0 not used yet.....
static const char *AsciChar; static const char *AsciChar;