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