Bug Summary

File:src/usr.bin/ctags/tree.c
Warning:line 89, column 1
Potential leak of memory pointed to by 'np'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name tree.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/ctags/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/usr.bin/ctags -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/ctags/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/ctags/tree.c
1/* $OpenBSD: tree.c,v 1.13 2015/09/29 03:19:24 guenther Exp $ */
2/* $NetBSD: tree.c,v 1.4 1995/03/26 20:14:11 glass Exp $ */
3
4/*
5 * Copyright (c) 1987, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <err.h>
34#include <limits.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/dirent.h>
39
40#include "ctags.h"
41
42boolchar in_preload = NO0;
43
44static void add_node(NODE *, NODE *);
45static void free_tree(NODE *);
46
47/*
48 * pfnote --
49 * enter a new node in the tree
50 */
51void
52pfnote(char *name, int ln)
53{
54 NODE *np;
55 char *fp;
56 char nbuf[1+MAXNAMLEN255+1];
57
58 if (!(np = malloc(sizeof(NODE)))) {
1
Memory is allocated
2
Assuming 'np' is non-null
3
Taking false branch
59 warnx("too many entries to sort");
60 put_entries(head);
61 free_tree(head);
62 if (!(head = np = malloc(sizeof(NODE))))
63 err(1, NULL((void *)0));
64 }
65 if (!xflag && !strcmp(name, "main")) {
4
Assuming 'xflag' is not equal to 0
66 if (!(fp = strrchr(curfile, '/')))
67 fp = curfile;
68 else
69 ++fp;
70 (void)snprintf(nbuf, sizeof nbuf, "M%s", fp);
71 fp = strrchr(nbuf, '.');
72 if (fp && !fp[2])
73 *fp = EOS'\0';
74 name = nbuf;
75 }
76 if (!(np->entry = strdup(name)))
5
Assuming field 'entry' is non-null
6
Taking false branch
77 err(1, NULL((void *)0));
78 np->file = curfile;
79 np->lno = ln;
80 np->left = np->right = 0;
81 np->been_warned = NO0;
82 np->dynfile = in_preload;
83 if (!(np->pat = strdup(lbuf)))
7
Assuming field 'pat' is non-null
8
Taking false branch
84 err(1, NULL((void *)0));
85 if (!head)
9
Assuming 'head' is non-null
10
Taking false branch
86 head = np;
87 else
88 add_node(np, head);
89}
11
Potential leak of memory pointed to by 'np'
90
91static void
92add_node(NODE *node, NODE *cur_node)
93{
94 int dif;
95
96 dif = strcmp(node->entry, cur_node->entry);
97 if (!dif) {
98 if (node->file == cur_node->file) {
99 if (!wflag)
100 fprintf(stderr(&__sF[2]), "Duplicate entry in file %s, "
101 "line %d: %s\nSecond entry ignored\n",
102 node->file, lineno, node->entry);
103 return;
104 }
105 if (!cur_node->been_warned)
106 if (!wflag)
107 fprintf(stderr(&__sF[2]), "Duplicate entry in files %s "
108 "and %s: %s (Warning only)\n",
109 node->file, cur_node->file, node->entry);
110 cur_node->been_warned = YES1;
111 }
112 else if (dif < 0)
113 if (cur_node->left)
114 add_node(node, cur_node->left);
115 else
116 cur_node->left = node;
117 else if (cur_node->right)
118 add_node(node, cur_node->right);
119 else
120 cur_node->right = node;
121}
122
123static void
124free_tree(NODE *node)
125{
126 if (node) {
127 free_tree(node->left);
128 free_tree(node->right);
129
130 free(node->entry);
131 free(node->pat);
132 if (node->dynfile == YES1)
133 free(node->file);
134 free(node);
135 }
136}