Bug Summary

File:root/firefox-clang/config/nsinstall.c
Warning:line 316, column 11
Null pointer passed to 1st parameter expecting 'nonnull'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name nsinstall.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 -pic-is-pie -mframe-pointer=none -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=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config -resource-dir /usr/lib/llvm-23/lib/clang/23 -D XP_UNIX -D DEBUG=1 -D UNICODE -D _UNICODE -I /root/firefox-clang/config -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/config -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-07-16-093543-1626485-1 -x c /root/firefox-clang/config/nsinstall.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** Netscape portable install command.
6**
7** Brendan Eich, 7/20/95
8*/
9#include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
10#include <assert.h>
11#include <fcntl.h>
12#include <errno(*__errno_location ()).h>
13#include <dirent.h>
14#include <limits.h>
15#include <grp.h>
16#include <pwd.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <utime.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include "pathsub.h"
25
26#ifdef HAVE_GETOPT_H
27# include <getopt.h>
28#endif
29
30#ifdef SUNOS4
31# include "sunos4.h"
32#endif
33
34#ifdef NEXTSTEP
35# include <bsd/libc.h>
36#endif
37
38#ifdef __QNX__
39# include <unix1.h>
40#endif
41
42#ifdef NEED_S_ISLNK
43# if !defined(S_ISLNK) && defined(S_IFLNK0120000)
44# define S_ISLNK(a)((((a)) & 0170000) == (0120000)) (((a) & S_IFMT0170000) == S_IFLNK0120000)
45# endif
46#endif
47
48#ifndef _DIRECTORY_SEPARATOR"/"
49# define _DIRECTORY_SEPARATOR"/" "/"
50#endif /* _DIRECTORY_SEPARATOR */
51
52#ifdef NEED_FCHMOD_PROTO
53extern int fchmod(int fildes, mode_t mode);
54#endif
55
56static void usage(void) {
57 fprintf(stderrstderr,
58 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
59 " %*s [-DdltR] file [file ...] directory\n",
60 program, (int)strlen(program), "");
61 exit(2);
62}
63
64static int mkdirs(char* path, mode_t mode) {
65 char* cp;
66 struct stat sb;
67 int res;
68 int l;
69
70 /* strip trailing "/." */
71 l = strlen(path);
72 if (l > 1 && path[l - 1] == '.' && path[l - 2] == '/') path[l - 2] = 0;
73
74 while (*path == '/' && path[1] == '/') path++;
75 for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
76 if (cp && cp != path) {
77 *cp = '\0';
78 if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)((((sb.st_mode)) & 0170000) == (0040000))) &&
79 mkdirs(path, mode) < 0) {
80 return -1;
81 }
82 *cp = '/';
83 }
84
85 res = mkdir(path, mode);
86 if ((res != 0) && (errno(*__errno_location ()) == EEXIST17))
87 return 0;
88 else
89 return res;
90}
91
92static uid_t touid(char* owner) {
93 struct passwd* pw;
94 uid_t uid;
95 char* cp;
96
97 pw = getpwnam(owner);
98 if (pw) return pw->pw_uid;
99 uid = strtol(owner, &cp, 0);
100 if (uid == 0 && cp == owner) fail("cannot find uid for %s", owner);
101 return uid;
102}
103
104static gid_t togid(char* group) {
105 struct group* gr;
106 gid_t gid;
107 char* cp;
108
109 gr = getgrnam(group);
110 if (gr) return gr->gr_gid;
111 gid = strtol(group, &cp, 0);
112 if (gid == 0 && cp == group) fail("cannot find gid for %s", group);
113 return gid;
114}
115
116static void copyfile(char* name, char* toname, mode_t mode, char* group,
117 char* owner, int dotimes, uid_t uid, gid_t gid) {
118 int fromfd, tofd = -1, cc, wc, exists;
119 char buf[BUFSIZ8192], *bp;
120 struct stat sb, tosb;
121 struct utimbuf utb;
122
123 exists = (lstat(toname, &tosb) == 0);
124
125 fromfd = open(name, O_RDONLY00);
126 if (fromfd < 0 || fstat(fromfd, &sb) < 0) fail("cannot access %s", name);
127 if (exists) {
128 if (S_ISREG(tosb.st_mode)((((tosb.st_mode)) & 0170000) == (0100000))) {
129 /* See if we can open it. This is more reliable than 'access'. */
130 tofd = open(toname, O_CREAT0100 | O_WRONLY01, 0666);
131 }
132 if (tofd < 0) {
133 (void)(S_ISDIR(tosb.st_mode)((((tosb.st_mode)) & 0170000) == (0040000)) ? rmdir : unlink)(toname);
134 }
135 }
136 if (tofd < 0) {
137 tofd = open(toname, O_CREAT0100 | O_WRONLY01, 0666);
138 if (tofd < 0) fail("cannot create %s", toname);
139 }
140
141 bp = buf;
142 while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
143 while ((wc = write(tofd, bp, (unsigned int)cc)) > 0) {
144 if ((cc -= wc) == 0) break;
145 bp += wc;
146 }
147 if (wc < 0) fail("cannot write to %s", toname);
148 }
149 if (cc < 0) fail("cannot read from %s", name);
150
151 if (ftruncate(tofd, sb.st_size) < 0) fail("cannot truncate %s", toname);
152#if !defined(VMS)
153 if (dotimes) {
154 utb.actime = sb.st_atimest_atim.tv_sec;
155 utb.modtime = sb.st_mtimest_mtim.tv_sec;
156 if (utime(toname, &utb) < 0) fail("cannot set times of %s", toname);
157 }
158# ifdef HAVE_FCHMOD
159 if (fchmod(tofd, mode) < 0)
160# else
161 if (chmod(toname, mode) < 0)
162# endif
163 fail("cannot change mode of %s", toname);
164#endif
165 if ((owner || group) && fchown(tofd, uid, gid) < 0)
166 fail("cannot change owner of %s", toname);
167
168 /* Must check for delayed (NFS) write errors on close. */
169 if (close(tofd) < 0) fail("cannot write to %s", toname);
170 close(fromfd);
171#if defined(VMS)
172 if (chmod(toname, (mode & (S_IREAD0400 | S_IWRITE0200))) < 0)
173 fail("cannot change mode of %s", toname);
174 if (dotimes) {
175 utb.actime = sb.st_atimest_atim.tv_sec;
176 utb.modtime = sb.st_mtimest_mtim.tv_sec;
177 if (utime(toname, &utb) < 0) fail("cannot set times of %s", toname);
178 }
179#endif
180}
181
182static void copydir(char* from, char* to, mode_t mode, char* group, char* owner,
183 int dotimes, uid_t uid, gid_t gid) {
184 DIR* dir;
185 struct dirent* ep;
186 struct stat sb;
187 char *base, *destdir, *direntry, *destentry;
188
189 base = xbasename(from);
190
191 /* create destination directory */
192 destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1));
193 sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR"/", base);
194 if (mkdirs(destdir, mode) != 0) {
195 fail("cannot make directory %s\n", destdir);
196 free(destdir);
197 return;
198 }
199
200 if (!(dir = opendir(from))) {
201 fail("cannot open directory %s\n", from);
202 free(destdir);
203 return;
204 }
205
206 direntry = xmalloc((unsigned int)PATH_MAX4096);
207 destentry = xmalloc((unsigned int)PATH_MAX4096);
208
209 while ((ep = readdir(dir))) {
210 if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue;
211
212 sprintf(direntry, "%s/%s", from, ep->d_name);
213 sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR"/", ep->d_name);
214
215 if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode)((((sb.st_mode)) & 0170000) == (0040000)))
216 copydir(direntry, destdir, mode, group, owner, dotimes, uid, gid);
217 else
218 copyfile(direntry, destentry, mode, group, owner, dotimes, uid, gid);
219 }
220
221 free(destdir);
222 free(direntry);
223 free(destentry);
224 closedir(dir);
225}
226
227int main(int argc, char** argv) {
228 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen,
229 bnlen, exists;
230 mode_t mode = 0755;
231 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base,
232 *linkname, buf[BUFSIZ8192];
233 uid_t uid;
234 gid_t gid;
235 struct stat sb, tosb, fromsb;
236
237 program = argv[0];
238 cwd = linkname = linkprefix = owner = group = 0;
239 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
240
241 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF(-1)) {
1
Assuming the condition is false
2
Loop condition is false. Execution continues on line 278
242 switch (opt) {
243 case 'C':
244 cwd = optarg;
245 break;
246 case 'D':
247 onlydir = 1;
248 break;
249 case 'd':
250 dodir = 1;
251 break;
252 case 'L':
253 linkprefix = optarg;
254 lplen = strlen(linkprefix);
255 dolink = 1;
256 break;
257 case 'R':
258 dolink = dorelsymlink = 1;
259 break;
260 case 'm':
261 mode = strtoul(optarg, &cp, 8);
262 if (mode == 0 && cp == optarg) usage();
263 break;
264 case 'o':
265 owner = optarg;
266 break;
267 case 'g':
268 group = optarg;
269 break;
270 case 't':
271 dotimes = 1;
272 break;
273 default:
274 usage();
275 }
276 }
277
278 argc -= optind;
279 argv += optind;
280 if (argc < 2 - onlydir) usage();
3
Assuming the condition is false
4
Taking false branch
281
282 todir = argv[argc - 1];
283 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)((((sb.st_mode)) & 0170000) == (0040000))) &&
5
Assuming the condition is true
284 mkdirs(todir, 0777) < 0) {
285 fail("cannot make directory %s", todir);
286 }
287 if (onlydir
5.1
'onlydir' is 0
) return 0;
6
Taking false branch
288
289 if (!cwd
6.1
'cwd' is null
) {
7
Taking true branch
290#ifndef NEEDS_GETCWD
291# ifndef GETCWD_CANT_MALLOC
292 cwd = getcwd(0, PATH_MAX4096);
293# else
294 cwd = malloc(PATH_MAX4096 + 1);
295 cwd = getcwd(cwd, PATH_MAX4096);
296# endif
297#else
298 cwd = malloc(PATH_MAX4096 + 1);
299 cwd = getwd(cwd);
300#endif
301 }
302
303 xchdir(todir);
304#ifndef NEEDS_GETCWD
305# ifndef GETCWD_CANT_MALLOC
306 todir = getcwd(0, PATH_MAX4096);
8
Assuming that 'getcwd' fails
9
Value assigned to 'todir'
307# else
308 todir = malloc(PATH_MAX4096 + 1);
309 todir = getcwd(todir, PATH_MAX4096);
310# endif
311#else
312 todir = malloc(PATH_MAX4096 + 1);
313 todir = getwd(todir);
314#endif
315 xchdir(cwd);
316 tdlen = strlen(todir);
10
Null pointer passed to 1st parameter expecting 'nonnull'
317
318 uid = owner ? touid(owner) : (uid_t)(-1);
319 gid = group ? togid(group) : (gid_t)(-1);
320
321 while (--argc > 0) {
322 name = *argv++;
323 len = strlen(name);
324 base = xbasename(name);
325 bnlen = strlen(base);
326 toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1));
327 sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR"/", base);
328 exists = (lstat(toname, &tosb) == 0);
329
330 if (dodir) {
331 /* -d means create a directory, always */
332 if (exists && !S_ISDIR(tosb.st_mode)((((tosb.st_mode)) & 0170000) == (0040000))) {
333 (void)unlink(toname);
334 exists = 0;
335 }
336 if (!exists && mkdir(toname, mode) < 0)
337 fail("cannot make directory %s", toname);
338 if ((owner || group) && chown(toname, uid, gid) < 0)
339 fail("cannot change owner of %s", toname);
340 } else if (dolink) {
341 if (access(name, R_OK4) != 0) {
342 fail("cannot access %s", name);
343 }
344 if (*name == '/') {
345 /* source is absolute pathname, link to it directly */
346 linkname = 0;
347 } else {
348 if (linkprefix) {
349 /* -L prefixes names with a $cwd arg. */
350 len += lplen + 1;
351 linkname = xmalloc((unsigned int)(len + 1));
352 sprintf(linkname, "%s/%s", linkprefix, name);
353 } else if (dorelsymlink) {
354 /* Symlink the relative path from todir to source name. */
355 linkname = xmalloc(PATH_MAX4096);
356
357 if (*todir == '/') {
358 /* todir is absolute: skip over common prefix. */
359 lplen = relatepaths(todir, cwd, linkname);
360 strcpy(linkname + lplen, name);
361 } else {
362 /* todir is named by a relative path: reverse it. */
363 reversepath(todir, name, len, linkname);
364 xchdir(cwd);
365 }
366
367 len = strlen(linkname);
368 }
369 name = linkname;
370 }
371
372 /* Check for a pre-existing symlink with identical content. */
373 if (exists &&
374 (!S_ISLNK(tosb.st_mode)((((tosb.st_mode)) & 0170000) == (0120000)) || readlink(toname, buf, sizeof buf) != len ||
375 strncmp(buf, name, (unsigned int)len) != 0 ||
376 ((stat(name, &fromsb) == 0) && (fromsb.st_mtimest_mtim.tv_sec > tosb.st_mtimest_mtim.tv_sec)))) {
377 (void)(S_ISDIR(tosb.st_mode)((((tosb.st_mode)) & 0170000) == (0040000)) ? rmdir : unlink)(toname);
378 exists = 0;
379 }
380 if (!exists && symlink(name, toname) < 0)
381 fail("cannot make symbolic link %s", toname);
382#ifdef HAVE_LCHOWN
383 if ((owner || group) && lchown(toname, uid, gid) < 0)
384 fail("cannot change owner of %s", toname);
385#endif
386
387 if (linkname) {
388 free(linkname);
389 linkname = 0;
390 }
391 } else {
392 /* Copy from name to toname, which might be the same file. */
393 if (stat(name, &sb) == 0 && S_IFDIR0040000 & sb.st_mode) {
394 /* then is directory: must explicitly create destination dir */
395 /* and manually copy files over */
396 copydir(name, todir, mode, group, owner, dotimes, uid, gid);
397 } else {
398 copyfile(name, toname, mode, group, owner, dotimes, uid, gid);
399 }
400 }
401
402 free(toname);
403 }
404
405 free(cwd);
406 free(todir);
407 return 0;
408}