logo elektroda
logo elektroda
X
logo elektroda

OBK Heap Drops Under Repeated Large HTTP Requests - leak/realloc question

divadiow 870 10
Best answers LABEL_AI_GENERATED

Why do repeated oversized HTTP requests make OpenBK7231T’s heap steadily drop until reboot, and is this caused by a realloc bug?

Yes — the heap drop is caused by a realloc bug: when memory is low, realloc fails and returns NULL, but the original buffer is still valid, and the code then overwrites request.received with NULL, orphaning the old allocation. [#21837597] That means repeated large HTTP requests can steadily consume heap until the device reboots. [#21837597]
AI summary based on the discussion. May contain errors.
ADVERTISEMENT
  • Helpful post

    Heap drop test with repeated oversized HTTP requests

    #1 21837181
    divadiow
    Level 38  
    Posts: 5220
    Help: 449
    Rate: 918
    I have a question. I've been trying to make OBK fallover by spamming it with repeated oversized http requests.

    Using this:

    Code: Powershell
    Log in, to see the code


    Code: Powershell
    Log in, to see the code


    and with 1.18.253 I get this kind of dropping heap until device reboots, not always, but mostly, especially if clicking about in the gui. This device has MQTT, HA, BL0937 configured.
    Code: Text
    Log in, to see the code


    with these changes https://github.com/openshwprojects/OpenBK7231...mpare/main...divadiow:OpenBK7231T_App:tcpleak

    I can run the stress-test script over and over with seemingly little effect. (starting HA discovery drops heap to 30k from which it doesn't seem to recover - maybe that's a different issue or to be expected?)

    Code: Text
    Log in, to see the code


    is this a realloc bug?
  • ADVERTISEMENT
  • #2 21837597
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14770
    Help: 659
    Rate: 12913
    Ahh yes, fair point, you have figured it out well.
    Screenshot of realloc documentation with “Parameters” and “Return value” sections
    In the low heap scenario, realloc fails to reallocate the memory, so it returns NULL, but original pointer is not invalidated. So, by overwriting request.received with returned NULL, we orphan the original (still correct) buffer.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #4 21837675
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14770
    Help: 659
    Rate: 12913
    Yes, great finding, keep it on! It's possible that wrong realloc usage pattern repeats somewhere else in the codebase.

    Just extra question - why return has changed to goto exit?
    Screenshot of a C code diff: realloc result stored in newbuf and NULL handled with goto exit
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • Multiple realloc clobbers and overflow risks identified

    #5 21837689
    divadiow
    Level 38  
    Posts: 5220
    Help: 449
    Rate: 918
    p.kaczmarek2 wrote:
    why return has changed to goto exit?


    so the path still hits the function's unified cleanup block. The return would skip those and leak the already-allocated buffers and socket resources.

    is the answer i'm being given ;)

    p.kaczmarek2 wrote:
    it's possible that wrong realloc usage pattern repeats somewhere else in the codebase.


    yes, potentials to be investigated:

    src/driver/drv_adcSmoother.c — realloc clobber + NULL memset; possible divide-by-zero if window unset/0
    src/driver/drv_girierMCU.c — queue buffer realloc clobber; no failure handling; bookkeeping corruption
    src/driver/drv_httpButtons.c — list realloc clobber + NULL deref; also prints bt->label before NULL check
    src/driver/drv_pixelAnim.c — work-buffer realloc clobber; “OOM handling” still leaks old buffer
    src/driver/drv_tuyaMCU.c — multiple realloc clobbers (queue data, payload buffers, rawData); immediate memcpy/usage after

    src/httpserver/hass.c — unbounded strcat into small static buffer (overflow risk)
    src/httpserver/hass.c + src/httpserver/hass.h — unique_id sizing mismatch when appending title (overflow risk)

    src/cmnds/cmd_script.c — realloc clobber; state updated before success (OOM inconsistency)
    src/cmnds/cmd_tokenizer.c — fixed 20-byte ${…} buffer overflow risk for long tokens
  • Helpful post
    #6 21838166
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14770
    Help: 659
    Rate: 12913
    Perfect, merged, proceed to futher links and show me solutions one by one. I want to review them, because automated methods still gives false positives.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • LLM checks still produce false positives

    #7 21838201
    divadiow
    Level 38  
    Posts: 5220
    Help: 449
    Rate: 918
    thanks for merging!

    p.kaczmarek2 wrote:
    I want to review them, because automated methods still gives false positives.


    for sure. I double, triple, quadruple etc check with LLM (new chats, different contexts, different questions etc) and, if necessary, confirm with other tools too. eg:

    Screenshot of a Cppcheck report listing memleakOnRealloc errors across multiple C source files.

    Added after 5 [minutes]:

    it's fascinating seeing what these tools have to say about the code. opens up another world of things to look into, wondering if these *are* real issues requiring attention, what does and doesn't matter...
    Screenshot of a Cppcheck report listing static analysis errors in C and C++ files

    Added after 10 [minutes]:

    this is that report if curious
    Attachments:
    • cppcheck-report.zip (3.25 MB) You must be logged in to download this attachment.
  • Helpful post

    Questioning whether to cap HTTP buffer growth

    #8 21838216
    max4elektroda
    Level 24  
    Posts: 758
    Help: 49
    Rate: 189
    Just a quick feedback about the initial http code:
    In my PR I also limited the maximum buffer size. What do you think about this?

    In most cases that should not be necessary:
    If buffer is to small, we reallocate a 1024 bytes bigger one.
    Since there's no limit, we will fail at some point if input data is huge.
    As long there is no other process claiming memory, we will roughly take half of the memory: every realloc needs "actual buffer"*2 +1024 to succeed, so a successful realloc must leave as free round half of the memory -1k on that point.

    The smaller free memory is , the smaller the memory left when realloc fails.

    Is this sufficient or should we limit the buffer from the beginning?
    Is my description above even correct and complete?
  • #9 21838733
    divadiow
    Level 38  
    Posts: 5220
    Help: 449
    Rate: 918
    max4elektroda wrote:
    What do you think about this?


    I guess this was directed at p.kaczmarek2 because I don't have the knowledge or experience from which to answer :)

    whatever's better/a good thing is my obvious preference. that goes without saying though
  • Helpful post
    #10 21838754
    max4elektroda
    Level 24  
    Posts: 758
    Help: 49
    Rate: 189
    Yes it was a general question to whoever thinks fit to answer or decide ;-)
  • Hardened HTTPButtons and MQTT parsing fixes

    #11 21930117
    divadiow
    Level 38  
    Posts: 5220
    Help: 449
    Rate: 918
    I went mad checking on the existing self-tests and adding new ones to see if any bugs came to the surface as a result.

    https://github.com/divadiow/OpenBK7231T_App/tree/refs/heads/selftests

    Code: Text
    Log in, to see the code


    other points of note

    Code: Text
    Log in, to see the code


    anything catch anyone's eye?

Topic summary

LABEL_AI_GENERATED
The discussion investigates why repeated oversized HTTP requests can drive OBK/OpenBK7231T heap usage down and trigger failure behavior. The root cause identified is unsafe realloc handling: when realloc fails under low-memory conditions, it returns NULL while the original buffer remains valid, but the code overwrites the existing pointer with NULL and orphans the allocated buffer. The fix is merged into a pull request, with additional review of cleanup flow to ensure unified exit paths do not leak buffers or socket resources. The thread also expands into a broader code audit, listing several other potential realloc-clobber, NULL-dereference, overflow, and cleanup issues across drivers and HTTP-related modules, and discusses whether to impose a maximum buffer size or rely on growth until allocation failure.
AI summary based on the discussion. May contain errors.
ADVERTISEMENT