Bug Summary

File:s/cmd/tests/secmodtest.c
Warning:line 83, column 25
Potential leak of memory pointed to by 'dbDir'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name secmodtest.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nss/cmd/tests -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nss/cmd/tests -resource-dir /usr/lib/llvm-18/lib/clang/18 -D HAVE_STRERROR -D LINUX -D linux -D XP_UNIX -D XP_UNIX -D DEBUG -U NDEBUG -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D _REENTRANT -D DEBUG -U NDEBUG -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D _REENTRANT -D NSS_DISABLE_SSE3 -D NSS_NO_INIT_SUPPORT -D USE_UTIL_DIRECTLY -D NO_NSPR_10_SUPPORT -D SSL_DISABLE_DEPRECATED_CIPHER_SUITE_NAMES -I ../../../dist/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/include -I ../../../dist/public/nss -I ../../../dist/private/nss -I ../../../dist/public/seccmd -I ../../../dist/public/dbm -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c99 -ferror-limit 19 -fgnuc-version=4.2.1 -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2024-05-18-082241-28900-1 -x c secmodtest.c
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5/*
6 * Regression test for bug 588269
7 *
8 * SECMOD_CloseUserDB should properly close the user DB, and it should
9 * be possible to later re-add that same user DB as a new slot
10 */
11
12#include "secutil.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "nspr.h"
19#include "nss.h"
20#include "secerr.h"
21#include "pk11pub.h"
22#include "plgetopt.h"
23
24void
25Usage(char *progName)
26{
27 fprintf(stderrstderr, "Usage: %s -d dbDir\n", progName);
28 exit(1);
29}
30
31SECStatus
32TestOpenCloseUserDB(char *progName, char *configDir, char *tokenName)
33{
34 char *modspec = NULL((void*)0);
35 SECStatus rv = SECSuccess;
36 PK11SlotInfo *userDbSlot = NULL((void*)0);
37
38 printf("Loading database <%s> - %s\n", configDir, tokenName);
39 modspec = PR_smprintf("configDir='%s' tokenDescription='%s'",
40 configDir, tokenName);
41 if (!modspec) {
42 rv = SECFailure;
43 goto loser;
44 }
45
46 userDbSlot = SECMOD_OpenUserDB(modspec);
47 PR_smprintf_free(modspec);
48 if (!userDbSlot) {
49 SECU_PrintError(progName, "couldn't open database");
50 rv = SECFailure;
51 goto loser;
52 }
53
54 printf("Closing database\n");
55 rv = SECMOD_CloseUserDB(userDbSlot);
56
57 if (rv != SECSuccess) {
58 SECU_PrintError(progName, "couldn't close database");
59 }
60
61 PK11_FreeSlot(userDbSlot);
62
63loser:
64 return rv;
65}
66
67int
68main(int argc, char **argv)
69{
70 PLOptState *optstate;
71 PLOptStatus optstatus;
72 SECStatus rv = SECFailure;
73 char *progName;
74 char *dbDir = NULL((void*)0);
75
76 progName = strrchr(argv[0], '/');
77 if (!progName) {
1
Assuming 'progName' is non-null
78 progName = strrchr(argv[0], '\\');
79 }
80 progName = progName
2.1
'progName' is non-null
? progName + 1 : argv[0];
2
Taking false branch
3
'?' condition is true
81
82 optstate = PL_CreateOptState(argc, argv, "d:");
83 while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
4
Assuming the condition is true
5
Loop condition is true. Entering loop body
8
Execution continues on line 83
9
Assuming the condition is true
10
Loop condition is true. Entering loop body
12
Execution continues on line 83
13
Potential leak of memory pointed to by 'dbDir'
84 switch (optstate->option) {
6
Control jumps to 'case 100:' at line 85
11
Control jumps to 'case 100:' at line 85
85 case 'd':
86 dbDir = strdup(optstate->value);
7
Memory is allocated
87 break;
88 }
89 }
90 if (optstatus == PL_OPT_BAD || dbDir == NULL((void*)0)) {
91 Usage(progName);
92 }
93
94 rv = NSS_NoDB_Init(NULL((void*)0));
95 if (rv != SECSuccess) {
96 SECU_PrintError(progName, "unable to initialize NSS");
97 goto loser;
98 }
99
100 printf("Open and Close Test 1\n");
101 rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 1");
102 if (rv != SECSuccess) {
103 goto loser;
104 }
105
106 printf("Open and Close Test 2\n");
107 rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 2");
108 if (rv != SECSuccess) {
109 goto loser;
110 }
111
112loser:
113 if (dbDir)
114 free(dbDir);
115
116 if (NSS_Shutdown() != SECSuccess) {
117 exit(1);
118 }
119 PR_Cleanup();
120
121 if (rv != SECSuccess) {
122 exit(1);
123 }
124
125 return 0;
126}