yeganemehr / arduino-at

It's a fully async library for communicate with AT modems.
MIT License
0 stars 1 forks source link

Free an already freed pointer in putTheCommandInBuffer(ExecuteCommand *command) #4

Open GiPe66 opened 11 months ago

GiPe66 commented 11 months ago

Hello @yeganemehr

in ATConnection.cpp line 72, the pointer command->secondpart is freed and set to nullptr. delete[] command->secondPart; command->secondPart = nullptr;

As it is a copy, the pointer is not set to nullptr in the commandQueue

This copy is done here (ATConnection.cpp line 237) auto item = commandQueue.front();

In some cases (f.i. when sending sms text with several \r\n) the answer of the sim800 may include several '>' and the delete[] is called for each '>' and crashes the code at the second call (already freed).

I fixed it by changing the line 237 with CommandQueueItem *item = &commandQueue.front(); and using the pointer instead of the union in the next lines

Then the commandQueue is changed and the secondPart is set to nullPtr

and testing the command.execute.secondPart value before calling putTheCommandInBuffer // assert(item->command.execute.secondPart); buffer.clear(); if (item->command.execute.secondPart) { putTheCommandInBuffer(&item->command.execute); }

Thanks for your attention.

GiPe66 commented 10 months ago

Better and cleaner to change line ATConnection.cpp line 237 from auto item = commandQueue.front();

to auto &item = commandQueue.front();

and testing the command.execute.secondPart value before calling putTheCommandInBuffer // assert(item->command.execute.secondPart); *** To comment out buffer.clear(); if (item->command.execute.secondPart) { putTheCommandInBuffer(&item->command.execute); }