Bug Summary

File:src/bin/ps/keyword.c
Warning:line 242, column 16
Access to field 'next' results in a dereference of a null pointer (loaded from variable 'vtail')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name keyword.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 -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -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/bin/ps/obj -resource-dir /usr/local/llvm16/lib/clang/16 -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/bin/ps/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -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/scan/2024-01-11-140451-98009-1 -x c /usr/src/bin/ps/keyword.c
1/* $OpenBSD: keyword.c,v 1.52 2022/02/25 18:05:49 rob Exp $ */
2/* $NetBSD: keyword.c,v 1.12.6.1 1996/05/30 21:25:13 cgd Exp $ */
3
4/*-
5 * Copyright (c) 1990, 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 <sys/types.h>
34#include <sys/time.h>
35#include <sys/resource.h>
36
37#include <err.h>
38#include <errno(*__errno()).h>
39#include <stddef.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44#include "ps.h"
45
46#include <sys/ucred.h>
47#include <sys/sysctl.h>
48
49int needheader;
50
51static VAR *findvar(char *);
52static int vcmp(const void *, const void *);
53
54#ifdef NOTINUSE
55int utime(), stime(), ixrss(), idrss(), isrss();
56 {{"utime"}, "UTIME", USER0x04, utime, 4},
57 {{"stime"}, "STIME", USER0x04, stime, 4},
58 {{"ixrss"}, "IXRSS", USER0x04, ixrss, 4},
59 {{"idrss"}, "IDRSS", USER0x04, idrss, 4},
60 {{"isrss"}, "ISRSS", USER0x04, isrss, 4},
61#endif
62
63/* Compute offset in common structures. */
64#define POFF(x)__builtin_offsetof(struct kinfo_proc, x) offsetof(struct kinfo_proc, x)__builtin_offsetof(struct kinfo_proc, x)
65
66#define UIDFMT"u" "u"
67#define UIDLEN5 5
68#define UID(n1, n2, fn, off){ n1, n2, ((void *)0), 0, fn, 5, 0, off, UINT32, "u" } \
69 { n1, n2, NULL((void *)0), 0, fn, UIDLEN5, 0, off, UINT32, UIDFMT"u" }
70#define GID(n1, n2, fn, off){ n1, n2, ((void *)0), 0, fn, 5, 0, off, UINT32, "u" } UID(n1, n2, fn, off){ n1, n2, ((void *)0), 0, fn, 5, 0, off, UINT32, "u" }
71
72#define PIDFMT"d" "d"
73#define PIDLEN5 5
74#define PID(n1, n2, fn, off){ n1, n2, ((void *)0), 0, fn, 5, 0, off, INT32, "d" } \
75 { n1, n2, NULL((void *)0), 0, fn, PIDLEN5, 0, off, INT32, PIDFMT"d" }
76
77#define TIDFMT"d" "d"
78#define TIDLEN7 7
79#define TID(n1, n2, fn, off){ n1, n2, ((void *)0), 0, fn, 7, 0, off, INT32, "d" } \
80 { n1, n2, NULL((void *)0), 0, fn, TIDLEN7, 0, off, INT32, TIDFMT"d" }
81
82#define USERLEN8 8
83#define CWDLEN40 40
84#define UCOMMLEN(sizeof(((struct kinfo_proc *)((void *)0))->p_comm) - 1) (sizeof(((struct kinfo_proc *)NULL((void *)0))->p_comm) - 1)
85#define WCHANLEN(sizeof(((struct kinfo_proc *)((void *)0))->p_wmesg) - 1) (sizeof(((struct kinfo_proc *)NULL((void *)0))->p_wmesg) - 1)
86
87/* Bit types must match their respective entries in struct kinfo_proc */
88/* Entries must be sorted in lexical ascending order! */
89VAR var[] = {
90 {"%cpu", "%CPU", NULL((void *)0), NLIST0x10, pcpu, 4},
91 {"%mem", "%MEM", NULL((void *)0), NLIST0x10, pmem, 4},
92 {"acflag", "ACFLG", NULL((void *)0), 0, pvar, 3, 0, POFF(p_acflag)__builtin_offsetof(struct kinfo_proc, p_acflag), UINT32, "x"},
93 {"acflg", "", "acflag"},
94 {"args", "", "command"},
95 {"blocked", "", "sigmask"},
96 {"caught", "", "sigcatch"},
97 {"comm", "COMMAND", "ucomm"},
98 {"command", "COMMAND", NULL((void *)0), COMM0x01|LJUST0x02|USER0x04, command, 16},
99 {"cpu", "CPU", NULL((void *)0), 0, pvar, 3, 0, POFF(p_estcpu)__builtin_offsetof(struct kinfo_proc, p_estcpu), UINT32, "d"},
100 {"cpuid", "CPUID", NULL((void *)0), 0, pvar, 8, 0, POFF(p_cpuid)__builtin_offsetof(struct kinfo_proc, p_cpuid), UINT64, "lld"},
101 {"cputime", "", "time"},
102 {"cwd", "CWD", NULL((void *)0), LJUST0x02, curwd, CWDLEN40},
103 {"dsiz", "DSIZ", NULL((void *)0), 0, dsize, 4},
104 {"etime", "ELAPSED", NULL((void *)0), USER0x04, elapsed, 12},
105 {"f", "F", NULL((void *)0), 0, pvar, 7, 0, POFF(p_flag)__builtin_offsetof(struct kinfo_proc, p_flag), INT32, "x"},
106 {"flags", "", "f"},
107 GID("gid", "GID", pvar, POFF(p_gid)){ "gid", "GID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_gid), UINT32, "u" }
,
108 {"group", "GROUP", NULL((void *)0), LJUST0x02, gname, USERLEN8},
109 {"ignored", "", "sigignore"},
110 {"inblk", "INBLK", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_inblock)__builtin_offsetof(struct kinfo_proc, p_uru_inblock), UINT64, "lld"},
111 {"inblock", "", "inblk"},
112 {"jobc", "JOBC", NULL((void *)0), 0, pvar, 4, 0, POFF(p_jobc)__builtin_offsetof(struct kinfo_proc, p_jobc), INT16, "d"},
113 {"ktrace", "KTRACE", NULL((void *)0), 0, pvar, 8, 0, POFF(p_traceflag)__builtin_offsetof(struct kinfo_proc, p_traceflag), INT32, "x"},
114 /* XXX */
115 {"ktracep", "KTRACEP", NULL((void *)0), 0, pvar, PTRWIDTH16, 0, POFF(p_tracep)__builtin_offsetof(struct kinfo_proc, p_tracep), UINT64, "llx"},
116 {"lim", "LIM", NULL((void *)0), 0, maxrss, 5},
117 {"login", "LOGIN", NULL((void *)0), LJUST0x02, logname, LOGIN_NAME_MAX32},
118 {"logname", "", "login"},
119 {"lstart", "STARTED", NULL((void *)0), LJUST0x02|USER0x04, lstarted, 28},
120 {"majflt", "MAJFLT", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_majflt)__builtin_offsetof(struct kinfo_proc, p_uru_majflt), UINT64, "lld"},
121 {"maxrss", "MAXRSS", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_maxrss)__builtin_offsetof(struct kinfo_proc, p_uru_maxrss), UINT64, "lld"},
122 {"minflt", "MINFLT", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_minflt)__builtin_offsetof(struct kinfo_proc, p_uru_minflt), UINT64, "lld"},
123 {"msgrcv", "MSGRCV", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_msgrcv)__builtin_offsetof(struct kinfo_proc, p_uru_msgrcv), UINT64, "lld"},
124 {"msgsnd", "MSGSND", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_msgsnd)__builtin_offsetof(struct kinfo_proc, p_uru_msgsnd), UINT64, "lld"},
125 {"ni", "", "nice"},
126 {"nice", "NI", NULL((void *)0), 0, pnice, 3},
127 {"nivcsw", "NIVCSW", NULL((void *)0), USER0x04, pvar, 5, 0, POFF(p_uru_nivcsw)__builtin_offsetof(struct kinfo_proc, p_uru_nivcsw), UINT64, "lld"},
128 {"nsignals", "", "nsigs"},
129 {"nsigs", "NSIGS", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_nsignals)__builtin_offsetof(struct kinfo_proc, p_uru_nsignals), UINT64, "lld"},
130 {"nswap", "NSWAP", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_nswap)__builtin_offsetof(struct kinfo_proc, p_uru_nswap), UINT64, "lld"},
131 {"nvcsw", "NVCSW", NULL((void *)0), USER0x04, pvar, 5, 0, POFF(p_uru_nvcsw)__builtin_offsetof(struct kinfo_proc, p_uru_nvcsw), UINT64, "lld"},
132 /* XXX */
133 {"nwchan", "WCHAN", NULL((void *)0), 0, pvar, PTRWIDTH16, 0, POFF(p_wchan)__builtin_offsetof(struct kinfo_proc, p_wchan), UINT64, "llx"},
134 {"oublk", "OUBLK", NULL((void *)0), USER0x04, pvar, 4, 0, POFF(p_uru_oublock)__builtin_offsetof(struct kinfo_proc, p_uru_oublock), UINT64, "lld"},
135 {"oublock", "", "oublk"},
136 /* XXX */
137 {"p_ru", "P_RU", NULL((void *)0), 0, pvar, PTRWIDTH16, 0, POFF(p_ru)__builtin_offsetof(struct kinfo_proc, p_ru), UINT64, "llx"},
138 /* XXX */
139 {"paddr", "PADDR", NULL((void *)0), 0, pvar, PTRWIDTH16, 0, POFF(p_paddr)__builtin_offsetof(struct kinfo_proc, p_paddr), UINT64, "llx"},
140 {"pagein", "PAGEIN", NULL((void *)0), USER0x04, pagein, 6},
141 {"pcpu", "", "%cpu"},
142 {"pending", "", "sig"},
143 PID("pgid", "PGID", pvar, POFF(p__pgid)){ "pgid", "PGID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p__pgid), INT32, "d" }
,
144 PID("pid", "PID", pvar, POFF(p_pid)){ "pid", "PID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_pid), INT32, "d" }
,
145 {"pledge", "PLEDGE", NULL((void *)0), LJUST0x02|NLIST0x10, printpledge, 64},
146 {"pmem", "", "%mem"},
147 PID("ppid", "PPID", pvar, POFF(p_ppid)){ "ppid", "PPID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_ppid), INT32, "d" }
,
148 {"pri", "PRI", NULL((void *)0), 0, pri, 3},
149 {"procflags", "PROCF", NULL((void *)0), 0, pvar, 7, 0, POFF(p_psflags)__builtin_offsetof(struct kinfo_proc, p_psflags), INT32, "x"},
150 {"re", "RE", NULL((void *)0), INF1270x08, pvar, 3, 0, POFF(p_swtime)__builtin_offsetof(struct kinfo_proc, p_swtime), UINT32, "u"},
151 GID("rgid", "RGID", pvar, POFF(p_rgid)){ "rgid", "RGID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_rgid), UINT32, "u" }
,
152 /* XXX */
153 {"rgroup", "RGROUP", NULL((void *)0), LJUST0x02, rgname, USERLEN8},
154 {"rlink", "RLINK", NULL((void *)0), 0, pvar, 8, 0, POFF(p_back)__builtin_offsetof(struct kinfo_proc, p_back), UINT64, "llx"},
155 {"rss", "RSS", NULL((void *)0), 0, p_rssize, 5},
156 {"rssize", "", "rsz"},
157 {"rsz", "RSZ", NULL((void *)0), 0, rssize, 4},
158 {"rtable", "RTABLE", NULL((void *)0), 0, pvar, 0, 0, POFF(p_rtableid)__builtin_offsetof(struct kinfo_proc, p_rtableid), INT32, "d"},
159 UID("ruid", "RUID", pvar, POFF(p_ruid)){ "ruid", "RUID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_ruid), UINT32, "u" }
,
160 {"ruser", "RUSER", NULL((void *)0), LJUST0x02, runame, USERLEN8},
161 {"sess", "SESS", NULL((void *)0), 0, pvar, PTRWIDTH16, 0, POFF(p_sess)__builtin_offsetof(struct kinfo_proc, p_sess), UINT64, "llx"},
162 {"sig", "PENDING", NULL((void *)0), 0, pvar, 8, 0, POFF(p_siglist)__builtin_offsetof(struct kinfo_proc, p_siglist), INT32, "x"},
163 {"sigcatch", "CAUGHT", NULL((void *)0), 0, pvar, 8, 0, POFF(p_sigcatch)__builtin_offsetof(struct kinfo_proc, p_sigcatch), UINT32, "x"},
164 {"sigignore", "IGNORED",
165 NULL((void *)0), 0, pvar, 8, 0, POFF(p_sigignore)__builtin_offsetof(struct kinfo_proc, p_sigignore), UINT32, "x"},
166 {"sigmask", "BLOCKED", NULL((void *)0), 0, pvar, 8, 0, POFF(p_sigmask)__builtin_offsetof(struct kinfo_proc, p_sigmask), UINT32, "x"},
167 {"sl", "SL", NULL((void *)0), INF1270x08, pvar, 3, 0, POFF(p_slptime)__builtin_offsetof(struct kinfo_proc, p_slptime), UINT32, "u"},
168 {"ssiz", "SSIZ", NULL((void *)0), 0, ssize, 4},
169 {"start", "STARTED", NULL((void *)0), LJUST0x02|USER0x04, started, 8},
170 {"stat", "", "state"},
171 {"state", "STAT", NULL((void *)0), LJUST0x02|NLIST0x10, printstate, 6},
172 {"supgid", "SUPGID", NULL((void *)0), LJUST0x02, supgid, 64},
173 {"supgrp", "SUPGRP", NULL((void *)0), LJUST0x02, supgrp, 64},
174 GID("svgid", "SVGID", pvar, POFF(p_svgid)){ "svgid", "SVGID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_svgid), UINT32, "u" }
,
175 UID("svuid", "SVUID", pvar, POFF(p_svuid)){ "svuid", "SVUID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_svuid), UINT32, "u" }
,
176 {"tdev", "TDEV", NULL((void *)0), 0, tdev, 4},
177 TID("tid", "TID", pvar, POFF(p_tid)){ "tid", "TID", ((void *)0), 0, pvar, 7, 0, __builtin_offsetof
(struct kinfo_proc, p_tid), INT32, "d" }
,
178 {"time", "TIME", NULL((void *)0), USER0x04, cputime, 9},
179 PID("tpgid", "TPGID", pvar, POFF(p_tpgid)){ "tpgid", "TPGID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_tpgid), INT32, "d" }
,
180 {"tsess", "TSESS", NULL((void *)0), 0, pvar, PTRWIDTH16, 0, POFF(p_tsess)__builtin_offsetof(struct kinfo_proc, p_tsess), UINT64, "llx"},
181 {"tsiz", "TSIZ", NULL((void *)0), 0, tsize, 4},
182 {"tt", "TT", NULL((void *)0), LJUST0x02, tname, 3},
183 {"tty", "TTY", NULL((void *)0), LJUST0x02, longtname, 8},
184 {"ucomm", "UCOMM", NULL((void *)0), LJUST0x02, ucomm, UCOMMLEN(sizeof(((struct kinfo_proc *)((void *)0))->p_comm) - 1)},
185 UID("uid", "UID", pvar, POFF(p_uid)){ "uid", "UID", ((void *)0), 0, pvar, 5, 0, __builtin_offsetof
(struct kinfo_proc, p_uid), UINT32, "u" }
,
186 {"upr", "UPR", NULL((void *)0), 0, pvar, 3, 0, POFF(p_usrpri)__builtin_offsetof(struct kinfo_proc, p_usrpri), UINT8, "d"},
187 {"user", "USER", NULL((void *)0), LJUST0x02, euname, USERLEN8},
188 {"usrpri", "", "upr"},
189 {"vsize", "", "vsz"},
190 {"vsz", "VSZ", NULL((void *)0), 0, vsize, 5},
191 {"wchan", "WCHAN", NULL((void *)0), LJUST0x02, wchan, WCHANLEN(sizeof(((struct kinfo_proc *)((void *)0))->p_wmesg) - 1)},
192 {"xstat", "XSTAT", NULL((void *)0), 0, pvar, 4, 0, POFF(p_xstat)__builtin_offsetof(struct kinfo_proc, p_xstat), UINT16, "x"},
193 {""},
194};
195
196void
197showkey(void)
198{
199 VAR *v;
200 int i;
201 char *p, *sep;
202
203 i = 0;
204 sep = "";
205 for (v = var; *(p = v->name); ++v) {
206 int len = strlen(p);
207 if (termwidth && (i += len + 1) > termwidth) {
208 i = len;
209 sep = "\n";
210 }
211 (void) printf("%s%s", sep, p);
212 sep = " ";
213 }
214 (void) printf("\n");
215}
216
217void
218parsefmt(char *p)
219{
220 static struct varent *vtail;
1
'vtail' initialized to a null pointer value
221
222#define FMTSEP" \t,\n" " \t,\n"
223 while (p && *p) {
2
Assuming 'p' is non-null
3
Loop condition is true. Entering loop body
224 char *cp;
225 VAR *v;
226 struct varent *vent;
227
228 while ((cp = strsep(&p, FMTSEP" \t,\n")) != NULL((void *)0) && *cp == '\0')
4
Assuming the condition is false
5
Loop condition is false. Execution continues on line 230
229 /* void */;
230 if (!cp
5.1
'cp' is non-null
)
231 break;
232 if (!(v = findvar(cp)) || v->parsed == 1)
6
Assuming 'v' is non-null
7
Assuming field 'parsed' is not equal to 1
8
Taking false branch
233 continue;
234 v->parsed = 1;
235 if ((vent = malloc(sizeof(struct varent))) == NULL((void *)0))
9
Assuming the condition is false
10
Taking false branch
236 err(1, NULL((void *)0));
237 vent->var = v;
238 vent->next = NULL((void *)0);
239 if (vhead == NULL((void *)0))
11
Assuming 'vhead' is not equal to NULL
12
Taking false branch
240 vhead = vtail = vent;
241 else {
242 vtail->next = vent;
13
Access to field 'next' results in a dereference of a null pointer (loaded from variable 'vtail')
243 vtail = vent;
244 }
245 needheader |= v->header[0] != '\0';
246 }
247 if (!vhead)
248 errx(1, "no valid keywords");
249}
250
251static VAR *
252findvar(char *p)
253{
254 VAR *v, key;
255 char *hp;
256
257 key.name = p;
258
259 hp = strchr(p, '=');
260 if (hp)
261 *hp++ = '\0';
262
263aliased:
264 key.name = p;
265 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
266
267 if (v && v->alias) {
268 p = v->alias;
269 if (hp == NULL((void *)0) && v->header[0] != '\0')
270 hp = v->header;
271 goto aliased;
272 }
273 if (!v) {
274 warnx("%s: keyword not found", p);
275 eval = 1;
276 return (NULL((void *)0));
277 }
278 if (hp)
279 v->header = hp;
280 return (v);
281}
282
283static int
284vcmp(const void *a, const void *b)
285{
286 return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
287}