Bug Summary

File:var/lib/jenkins/workspace/firefox-scan-build/config/nsinstall.c
Warning:line 153, column 7
2nd function call argument is an uninitialized value

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