Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory leak #3

Closed
lsoltero opened this issue Apr 27, 2022 · 1 comment
Closed

memory leak #3

lsoltero opened this issue Apr 27, 2022 · 1 comment

Comments

@lsoltero
Copy link

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;
}

@ronzeiller
Copy link
Owner

Thank you Luis,
merged your version into my master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants