uNetworking / uSockets

Miniscule cross-platform eventing, networking & crypto for async applications
Apache License 2.0
1.29k stars 268 forks source link

Fix memory leak with dh params file #121

Closed fatduckling closed 4 years ago

fatduckling commented 4 years ago

Hi

This is my first time contributing to open source project, so please let me know if I made a mistake anywhere :)

There is a memory leak when parsing the DH params file.

Here is a test case:

#include <iostream>
#include "uWebSockets/App.h"

int main() {
  struct PerSocketData {};
  uWS::SSLApp(
      {
          .dh_params_file_name = "dhparam.pem",
      }
  ).listen(9001, [](auto *token) {
    if (token) {
      std::cout << "Listening on port " << 9001 << std::endl;
    } else {
      std::cout << "Failed to listen on port. Token is null? " << std::boolalpha << (token == NULL) << std::endl;
    }
    std::cout << "Closing the server" << std::endl;
    us_listen_socket_close(1, token);
  }).run();
  return 0;
}

With Valgrind address sanitiser:

saleem@ryzen-destroyer:~/CLionProjects/ZMQ1/build$ valgrind --leak-check=full ./ZMQ1 
==9944== Memcheck, a memory error detector
==9944== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9944== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==9944== Command: ./ZMQ1
==9944== 
Listening on port 9001
Closing the server
==9944== 
==9944== HEAP SUMMARY:
==9944==     in use at exit: 512 bytes in 6 blocks
==9944==   total heap usage: 4,884 allocs, 4,878 frees, 1,352,614 bytes allocated
==9944== 
==9944== 512 (144 direct, 368 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==9944==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9944==    by 0x4A1796D: CRYPTO_zalloc (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x4989243: DH_new_method (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x4987A14: dh_cb (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x493157C: ASN1_item_ex_new (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x492F3BA: ASN1_item_ex_d2i (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x492F57E: ASN1_item_d2i (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x4A318EF: PEM_read_bio_DHparams (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x4A319D0: PEM_read_DHparams (in /usr/local/lib/libcrypto.so.1.1)
==9944==    by 0x122965: us_internal_create_ssl_socket_context (openssl.c:468)
==9944==    by 0x1215BE: us_create_socket_context (context.c:111)
==9944==    by 0x114792: uWS::HttpContext<true>::create(uWS::Loop*, us_socket_context_options_t) (HttpContext.h:340)
==9944== 
==9944== LEAK SUMMARY:
==9944==    definitely lost: 144 bytes in 1 blocks
==9944==    indirectly lost: 368 bytes in 5 blocks
==9944==      possibly lost: 0 bytes in 0 blocks
==9944==    still reachable: 0 bytes in 0 blocks
==9944==         suppressed: 0 bytes in 0 blocks
==9944== 
==9944== For lists of detected and suppressed errors, rerun with: -s
==9944== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

After applying my patch, there appears no more memory leaks:

==10100== Memcheck, a memory error detector
==10100== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10100== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==10100== Command: ./ZMQ1
==10100== 
Listening on port 9001
Closing the server
==10100== 
==10100== HEAP SUMMARY:
==10100==     in use at exit: 0 bytes in 0 blocks
==10100==   total heap usage: 4,884 allocs, 4,884 frees, 1,352,614 bytes allocated
==10100== 
==10100== All heap blocks were freed -- no leaks are possible
==10100== 
==10100== For lists of detected and suppressed errors, rerun with: -s
==10100== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Thanks