memleaktest.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2016-2021 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. /* __has_feature is a clang-ism, while __SANITIZE_ADDRESS__ is a gcc-ism */
  14. #if defined(__has_feature)
  15. # if __has_feature(address_sanitizer)
  16. # define __SANITIZE_ADDRESS__ 1
  17. # endif
  18. #endif
  19. /* If __SANITIZE_ADDRESS__ isn't defined, define it to be false */
  20. /* Leak detection is not yet supported with MSVC on Windows, so */
  21. /* set __SANITIZE_ADDRESS__ to false in this case as well. */
  22. #if !defined(__SANITIZE_ADDRESS__) || defined(_MSC_VER)
  23. # undef __SANITIZE_ADDRESS__
  24. # define __SANITIZE_ADDRESS__ 0
  25. #endif
  26. /*
  27. * We use a proper main function here instead of the custom main from the
  28. * test framework to avoid CRYPTO_mem_leaks stuff.
  29. */
  30. int main(int argc, char *argv[])
  31. {
  32. #if __SANITIZE_ADDRESS__
  33. int exitcode = EXIT_SUCCESS;
  34. #else
  35. /*
  36. * When we don't sanitize, we set the exit code to what we would expect
  37. * to get when we are sanitizing. This makes it easy for wrapper scripts
  38. * to detect that we get the result we expect.
  39. */
  40. int exitcode = EXIT_FAILURE;
  41. #endif
  42. char *lost;
  43. lost = OPENSSL_malloc(3);
  44. if (!TEST_ptr(lost))
  45. return EXIT_FAILURE;
  46. strcpy(lost, "ab");
  47. if (argv[1] && strcmp(argv[1], "freeit") == 0) {
  48. OPENSSL_free(lost);
  49. exitcode = EXIT_SUCCESS;
  50. }
  51. lost = NULL;
  52. return exitcode;
  53. }