OpenBK7231T_App icon indicating copy to clipboard operation
OpenBK7231T_App copied to clipboard

Try fixing possible memory leaks in TCP servers

Open MaxineMuster opened this issue 6 months ago • 2 comments

As a result from some tests in #1629:

I think there are possible memory leaks in TCP server code: in case realloc fails, regular TCP server will simply "return", leaving all allocated buffers untouched

For both servers: If realloc is used, "buf" pointer is not changed. if later "free(buf)" is called, that will probably be invalid (the memory was already freed during a successful "realloc") if realloc fails, we will lose pointer to the old (unchanged, in this case) buffer, which is overwritten by NULL

Some more theoretic problem: both servers will in theory increase buffers memory unlimited, until all data from recv fits. That could possibly consume all memory.

So here's my try to address these possible issues.

Tested only with W800 yet

As a test it uses low sizes for buffers to "force" resizing buffers (512 bytes as start, maximum is 20248)

There are some minor changes to allow new_tcp_server on W800.

In "new_tcp_server" I also needed to lower stacksize if memory is low - W800 else crashed from time to time directly after creating a TCP Client thread This will not happen on "regular" TCP server

This first commit will build "regular" TCP server on W800, next will be with new_tcp_server.

EDIT: And maybe someone knows, why the same code, which is successfully compiled here (all checks o.k.) fails for some "Beken" variants (powerMetering, IRremote..) im my repo with something like

 export app partition image: OpenBK7231T_TCP_server_memoryleak_3e1852758489_tuyaMCU_enc_uart_1.00.bin

time: 0.94s
Good bye!
End Combined
generate ota file
malloc(): corrupted top size
Aborted (core dumped)
make[1]: *** [Makefile:332: build-BK7231] Error 134
make[1]: Leaving directory '/home/runner/work/OpenBK7231T_App/OpenBK7231T_App'

MaxineMuster avatar Jun 10 '25 16:06 MaxineMuster

I think there are possible memory leaks in TCP server code:

probably a dumb question, but does/would @niteria leak checks pick this up?

https://github.com/openshwprojects/OpenBK7231T_App/pull/1664 https://github.com/openshwprojects/OpenBK7231T_App/pull/1651

divadiow avatar Jun 11 '25 07:06 divadiow

probably a dumb question, but does/would @niteria leak checks pick this up?

If it's not triggered by any of the simulator tests, then ASAN wouldn't catch it. If possible, it would be nice to add a test that triggers it.

niteria avatar Jun 11 '25 10:06 niteria