ronzeiller/NMEA0183-AIS

memory leak

lsoltero opened this issue · 1 comments

malloc is called but a corresponding free is never called.

lsoltero@ubuntu-linux-20-04-desktop:~/openwrt/19.07/local_packages/nmead/NMEA0183-AIS$ grep malloc *
grep: Examples: Is a directory
NMEA0183AISMsg.cpp: char to = (char) malloc(337);
NMEA0183AISMsg.cpp: char to = (char) malloc(91);
NMEA0183AISMsg.cpp: char to = (char) malloc(169); // Part A has Length 168
NMEA0183AISMsg.cpp: char to = (char) malloc(169); // Part B has Length 168

lsoltero@ubuntu-linux-20-04-desktop:~/openwrt/19.07/local_packages/nmead/NMEA0183-AIS$ grep free *
grep: Examples: Is a directory
NMEA0183AISMessages.cpp:Permission is hereby granted, free of charge, to any person obtaining a copy of
NMEA0183AISMessages.h:Permission is hereby granted, free of charge, to any person obtaining a copy of
NMEA0183AISMsg.cpp:Permission is hereby granted, free of charge, to any person obtaining a copy of
NMEA0183AISMsg.h:Permission is hereby granted, free of charge, to any person obtaining a copy of
README.md:Permission is hereby granted, free of charge, to any person obtaining a copy of

in every case malloc is used to allocate a temporary buffer to store the binary payload that is to be converted in ConvertBinaryAISPlayloadBinToAscii(). This function copies the converted payload to a static buffer called Payload

so.. it seems that either a free should be called after every call to ConvertBinaryAISPlayloadBinToAscii() or the payload buffer should be allocated on the stack.

as in this...

const char *tNMEA0183AISMsg::GetPayloadType5_Part1() {

uint16_t lenbin = strlen( PayloadBin);
if ( lenbin != 424 ) return nullptr;

char to = (char) malloc(337);
strncpy(to, PayloadBin, 336); // First Part is always 336 Length
to[336]=0;

if ( !ConvertBinaryAISPayloadBinToAscii( to ) ) return nullptr;

return Payload;
}

goes to...

const char *tNMEA0183AISMsg::GetPayloadType5_Part1() {

uint16_t lenbin = strlen( PayloadBin);
if ( lenbin != 424 ) return nullptr;

char to[337];
strncpy(to, PayloadBin, 336); // First Part is always 336 Length
to[336]=0;

if ( !ConvertBinaryAISPayloadBinToAscii( to ) ) return nullptr;

return Payload;
}

Thank you Luis,
merged your version into my master.