memleaktest.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/crypto.h>
  12. #include "testutil.h"
  13. /*
  14. * We use a proper main function here instead of the custom main from the
  15. * test framework because the CRYPTO_mem_leaks_fp function cannot be called
  16. * a second time without trying to use a null pointer. The test framework
  17. * calls this function as part of its close down.
  18. *
  19. * A work around is to call putenv("OPENSSL_DEBUG_MEMORY=0"); before exiting
  20. * but that is worse than avoiding the test framework's main.
  21. */
  22. int main(int argc, char *argv[])
  23. {
  24. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  25. char *p;
  26. char *lost;
  27. int noleak;
  28. p = getenv("OPENSSL_DEBUG_MEMORY");
  29. if (p != NULL && strcmp(p, "on") == 0)
  30. CRYPTO_set_mem_debug(1);
  31. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  32. lost = OPENSSL_malloc(3);
  33. if (!TEST_ptr(lost))
  34. return EXIT_FAILURE;
  35. if (argv[1] && strcmp(argv[1], "freeit") == 0) {
  36. OPENSSL_free(lost);
  37. lost = NULL;
  38. }
  39. noleak = CRYPTO_mem_leaks_fp(stderr);
  40. /* If -1 return value something bad happened */
  41. if (!TEST_int_ne(noleak, -1))
  42. return EXIT_FAILURE;
  43. return TEST_int_eq(lost != NULL, noleak == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  44. #else
  45. return EXIT_SUCCESS;
  46. #endif
  47. }