clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.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/ksh/obj -resource-dir /usr/local/llvm16/lib/clang/16 -D EMACS -D VI -I . -I /usr/src/bin/ksh -I /usr/src/bin/ksh/../../lib/libc/gen -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/bin/ksh/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/ksh/main.c
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | #include <sys/stat.h> |
8 | |
9 | #include <errno.h> |
10 | #include <fcntl.h> |
11 | #include <paths.h> |
12 | #include <pwd.h> |
13 | #include <stdio.h> |
14 | #include <stdlib.h> |
15 | #include <string.h> |
16 | #include <unistd.h> |
17 | |
18 | #include "sh.h" |
19 | |
20 | extern char **environ; |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | static void reclaim(void); |
27 | static void remove_temps(struct temp *tp); |
28 | static int is_restricted(char *name); |
29 | static void init_username(void); |
30 | |
31 | const char *kshname; |
32 | pid_t kshpid; |
33 | pid_t procpid; |
34 | uid_t ksheuid; |
35 | int exstat; |
36 | int subst_exstat; |
37 | const char *safe_prompt; |
38 | int disable_subst; |
39 | |
40 | Area aperm; |
41 | |
42 | struct env *genv; |
43 | |
44 | char shell_flags[FNFLAGS]; |
45 | |
46 | char null[] = ""; |
47 | |
48 | int shl_stdout_ok; |
49 | |
50 | unsigned int ksh_tmout; |
51 | enum tmout_enum ksh_tmout_state = TMOUT_EXECUTING; |
52 | |
53 | int really_exit; |
54 | |
55 | int ifs0 = ' '; |
56 | |
57 | volatile sig_atomic_t trap; |
58 | volatile sig_atomic_t intrsig; |
59 | volatile sig_atomic_t fatal_trap; |
60 | |
61 | Getopt builtin_opt; |
62 | Getopt user_opt; |
63 | |
64 | struct coproc coproc; |
65 | sigset_t sm_default, sm_sigchld; |
66 | |
67 | char *builtin_argv0; |
68 | int builtin_flag; |
69 | |
70 | char *current_wd; |
71 | int current_wd_size; |
72 | |
73 | int x_cols = 80; |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | static const char initifs[] = "IFS= \t\n"; |
80 | |
81 | static const char initsubs[] = "${PS2=> } ${PS3=#? } ${PS4=+ }"; |
82 | |
83 | static const char *initcoms [] = { |
84 | #ifndef SMALL |
85 | "typeset", "-r", "KSH_VERSION", NULL, |
86 | #endif /* SMALL */ |
87 | "typeset", "-x", "SHELL", "PATH", "HOME", "PWD", "OLDPWD", NULL, |
88 | "typeset", "-ir", "PPID", NULL, |
89 | "typeset", "-i", "OPTIND=1", NULL, |
90 | #ifndef SMALL |
91 | "eval", "typeset -i RANDOM MAILCHECK=\"${MAILCHECK-600}\" SECONDS=\"${SECONDS-0}\" TMOUT=\"${TMOUT-0}\"", NULL, |
92 | #else |
93 | "eval", "typeset -i RANDOM SECONDS=\"${SECONDS-0}\" TMOUT=\"${TMOUT-0}\"", NULL, |
94 | #endif /* SMALL */ |
95 | "alias", |
96 | |
97 | "hash=alias -t", |
98 | "stop=kill -STOP", |
99 | "autoload=typeset -fu", |
100 | "functions=typeset -f", |
101 | "history=fc -l", |
102 | "integer=typeset -i", |
103 | "nohup=nohup ", |
104 | "local=typeset", |
105 | "r=fc -s", |
106 | |
107 | "login=exec login", |
108 | NULL, |
109 | |
110 | "alias", "-tU", |
111 | "cat", "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls", |
112 | "mail", "make", "mv", "pr", "rm", "sed", "sh", "vi", "who", |
113 | NULL, |
114 | NULL |
115 | }; |
116 | |
117 | char username[_PW_NAME_LEN + 1]; |
118 | |
119 | #ifndef SMALL |
120 | #define version_param (initcoms[2]) |
121 | #endif /* SMALL */ |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | static char ** |
129 | make_argv(int argc, char *argv[]) |
130 | { |
131 | int i; |
132 | char **nargv; |
133 | |
134 | nargv = areallocarray(NULL, argc + 1, sizeof(char *), &aperm); |
135 | nargv[0] = (char *) kshname; |
136 | for (i = 1; i < argc; i++) |
137 | nargv[i] = argv[i]; |
138 | nargv[i] = NULL; |
139 | |
140 | return nargv; |
141 | } |
142 | |
143 | int |
144 | main(int argc, char *argv[]) |
145 | { |
146 | int i; |
147 | int argi; |
148 | Source *s; |
149 | struct block *l; |
150 | int restricted, errexit; |
151 | char **wp; |
152 | struct env env; |
153 | pid_t ppid; |
154 | |
155 | kshname = argv[0]; |
156 | |
157 | if (issetugid()) { |
158 | if (pledge("stdio rpath wpath cpath fattr flock getpw proc " |
159 | "exec tty id", NULL) == -1) { |
160 | perror("pledge"); |
161 | exit(1); |
162 | } |
163 | } else { |
164 | if (pledge("stdio rpath wpath cpath fattr flock getpw proc " |
165 | "exec tty", NULL) == -1) { |
166 | perror("pledge"); |
167 | exit(1); |
168 | } |
169 | } |
170 | |
171 | ainit(&aperm); |
172 | |
173 | |
174 | memset(&env, 0, sizeof(env)); |
175 | env.type = E_NONE; |
176 | ainit(&env.area); |
177 | genv = &env; |
178 | newblock(); |
179 | |
180 | |
181 | initio(); |
182 | |
183 | initvar(); |
184 | |
185 | initctypes(); |
186 | |
187 | inittraps(); |
188 | |
189 | coproc_init(); |
190 | |
191 | |
192 | ktinit(&taliases, APERM, 0); |
193 | ktinit(&aliases, APERM, 0); |
194 | ktinit(&homedirs, APERM, 0); |
195 | |
196 | |
197 | initkeywords(); |
198 | |
199 | |
200 | ktinit(&builtins, APERM, 64); |
201 | for (i = 0; shbuiltins[i].name != NULL; i++) |
202 | builtin(shbuiltins[i].name, shbuiltins[i].func); |
203 | for (i = 0; kshbuiltins[i].name != NULL; i++) |
204 | builtin(kshbuiltins[i].name, kshbuiltins[i].func); |
205 | |
206 | init_histvec(); |
207 | |
208 | def_path = _PATH_DEFPATH; |
209 | { |
210 | size_t len = confstr(_CS_PATH, NULL, 0); |
211 | char *new; |
212 | |
213 | if (len > 0) { |
214 | confstr(_CS_PATH, new = alloc(len + 1, APERM), len + 1); |
215 | def_path = new; |
216 | } |
217 | } |
218 | |
219 | |
220 | |
221 | |
222 | { |
223 | struct tbl *vp = global("PATH"); |
224 | |
225 | setstr(vp, def_path, KSH_RETURN_ERROR); |
226 | } |
227 | |
228 | |
229 | |
230 | |
231 | |
232 | |
233 | |
234 | Flag(FNOHUP) = 1; |
235 | |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | Flag(FBRACEEXPAND) = 1; |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | #ifdef POSIXLY_CORRECT |
250 | change_flag(FPOSIX, OF_SPECIAL, 1); |
251 | #endif /* POSIXLY_CORRECT */ |
252 | |
253 | |
254 | if (!strcmp(kshname, "sh") || !strcmp(kshname, "-sh") || |
255 | (strlen(kshname) >= 3 && |
256 | !strcmp(&kshname[strlen(kshname) - 3], "/sh"))) { |
257 | Flag(FSH) = 1; |
258 | #ifndef SMALL |
259 | version_param = "SH_VERSION"; |
260 | #endif /* SMALL */ |
261 | } |
262 | |
263 | |
264 | |
265 | |
266 | #if defined(EMACS) |
267 | change_flag(FEMACS, OF_SPECIAL, 1); |
268 | #endif /* EMACS */ |
269 | #if defined(VI) |
270 | Flag(FVITABCOMPLETE) = 1; |
271 | #endif /* VI */ |
272 | |
273 | |
274 | if (environ != NULL) |
275 | for (wp = environ; *wp != NULL; wp++) |
276 | typeset(*wp, IMPORT|EXPORT, 0, 0, 0); |
277 | |
278 | kshpid = procpid = getpid(); |
279 | typeset(initifs, 0, 0, 0, 0); |
280 | |
281 | |
282 | substitute(initsubs, 0); |
283 | |
284 | |
285 | { |
286 | struct stat s_pwd, s_dot; |
287 | struct tbl *pwd_v = global("PWD"); |
288 | char *pwd = str_val(pwd_v); |
289 | char *pwdx = pwd; |
290 | |
291 | |
292 | if (pwd[0] != '/' || |
293 | stat(pwd, &s_pwd) == -1 || stat(".", &s_dot) == -1 || |
294 | s_pwd.st_dev != s_dot.st_dev || |
295 | s_pwd.st_ino != s_dot.st_ino) |
296 | pwdx = NULL; |
297 | set_current_wd(pwdx); |
298 | if (current_wd[0]) |
299 | simplify_path(current_wd); |
300 | |
301 | |
302 | |
303 | if (current_wd[0] || pwd != null) |
304 | |
305 | setstr(pwd_v, current_wd, KSH_RETURN_ERROR); |
306 | } |
307 | ppid = getppid(); |
308 | setint(global("PPID"), (int64_t) ppid); |
309 | #ifndef SMALL |
310 | |
311 | setstr(global(version_param), ksh_version, KSH_RETURN_ERROR); |
312 | #endif /* SMALL */ |
313 | |
314 | |
315 | for (wp = (char**) initcoms; *wp != NULL; wp++) { |
316 | shcomexec(wp); |
317 | for (; *wp != NULL; wp++) |
318 | ; |
319 | } |
320 | |
321 | |
322 | ksheuid = geteuid(); |
323 | init_username(); |
324 | safe_prompt = ksheuid ? "$ " : "# "; |
325 | { |
326 | struct tbl *vp = global("PS1"); |
327 | |
328 | |
329 | if (!(vp->flag & ISSET)) { |
330 | |
331 | setstr(vp, "\\h\\$ ", KSH_RETURN_ERROR); |
332 | } |
333 | } |
334 | |
335 | |
336 | Flag(FPRIVILEGED) = getuid() != ksheuid || getgid() != getegid(); |
337 | |
338 | |
339 | Flag(FMONITOR) = 127; |
340 | argi = parse_args(argv, OF_CMDLINE, NULL); |
341 | if (argi < 0) |
342 | exit(1); |
343 | |
344 | if (Flag(FCOMMAND)) { |
345 | s = pushs(SSTRING, ATEMP); |
346 | if (!(s->start = s->str = argv[argi++])) |
347 | errorf("-c requires an argument"); |
348 | if (argv[argi]) |
349 | kshname = argv[argi++]; |
350 | } else if (argi < argc && !Flag(FSTDIN)) { |
351 | s = pushs(SFILE, ATEMP); |
352 | s->file = argv[argi++]; |
353 | s->u.shf = shf_open(s->file, O_RDONLY, 0, SHF_MAPHI|SHF_CLEXEC); |
354 | if (s->u.shf == NULL) { |
355 | exstat = 127; |
356 | errorf("%s: %s", s->file, strerror(errno)); |
357 | } |
358 | kshname = s->file; |
359 | } else { |
360 | Flag(FSTDIN) = 1; |
361 | s = pushs(SSTDIN, ATEMP); |
362 | s->file = "<stdin>"; |
363 | s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0), NULL); |
364 | if (isatty(0) && isatty(2)) { |
365 | Flag(FTALKING) = Flag(FTALKING_I) = 1; |
366 | |
367 | s->flags |= SF_TTY; |
368 | s->u.shf->flags |= SHF_INTERRUPT; |
369 | s->file = NULL; |
370 | } |
371 | } |
372 | |
373 | |
374 | { |
375 | struct stat s_stdin; |
376 | |
377 | if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode) && |
378 | Flag(FTALKING)) |
379 | reset_nonblock(0); |
380 | } |
381 | |
382 | |
383 | i = Flag(FMONITOR) != 127; |
384 | Flag(FMONITOR) = 0; |
385 | j_init(i); |
386 | |
387 | if (Flag(FTALKING)) |
388 | x_init(); |
389 | |
390 | l = genv->loc; |
391 | l->argv = make_argv(argc - (argi - 1), &argv[argi - 1]); |
392 | l->argc = argc - argi; |
393 | getopts_reset(1); |
394 | |
395 | |
396 | restricted = Flag(FRESTRICTED); |
397 | Flag(FRESTRICTED) = 0; |
398 | errexit = Flag(FERREXIT); |
399 | Flag(FERREXIT) = 0; |
400 | |
401 | |
402 | |
403 | |
404 | if (!current_wd[0] && Flag(FTALKING)) |
405 | warningf(false, "Cannot determine current working directory"); |
406 | |
407 | if (Flag(FLOGIN)) { |
408 | include(KSH_SYSTEM_PROFILE, 0, NULL, 1); |
409 | if (!Flag(FPRIVILEGED)) |
410 | include(substitute("$HOME/.profile", 0), 0, NULL, 1); |
411 | } |
412 | |
413 | if (Flag(FPRIVILEGED)) |
414 | include("/etc/suid_profile", 0, NULL, 1); |
415 | else if (Flag(FTALKING)) { |
416 | char *env_file; |
417 | |
418 | |
419 | env_file = str_val(global("ENV")); |
420 | |
421 | #ifdef DEFAULT_ENV |
422 | |
423 | if (env_file == null) |
424 | env_file = DEFAULT_ENV; |
425 | #endif /* DEFAULT_ENV */ |
426 | env_file = substitute(env_file, DOTILDE); |
427 | if (*env_file != '\0') |
428 | include(env_file, 0, NULL, 1); |
429 | } |
430 | |
431 | if (is_restricted(argv[0]) || is_restricted(str_val(global("SHELL")))) |
432 | restricted = 1; |
433 | if (restricted) { |
434 | static const char *const restr_com[] = { |
435 | "typeset", "-r", "PATH", |
436 | "ENV", "SHELL", |
437 | NULL |
438 | }; |
439 | shcomexec((char **) restr_com); |
440 | |
441 | Flag(FRESTRICTED) = 1; |
442 | } |
443 | if (errexit) |
444 | Flag(FERREXIT) = 1; |
445 | |
446 | if (Flag(FTALKING)) { |
447 | hist_init(s); |
448 | alarm_init(); |
449 | } else |
450 | Flag(FTRACKALL) = 1; |
451 | |
452 | shell(s, true); |
453 | return 0; |
454 | } |
455 | |
456 | static void |
457 | init_username(void) |
458 | { |
459 | char *p; |
460 | struct tbl *vp = global("USER"); |
461 | |
462 | if (vp->flag & ISSET) |
463 | p = ksheuid == 0 ? "root" : str_val(vp); |
464 | else |
465 | p = getlogin(); |
466 | |
467 | strlcpy(username, p != NULL ? p : "?", sizeof username); |
468 | } |
469 | |
470 | int |
471 | include(const char *name, int argc, char **argv, int intr_ok) |
472 | { |
473 | Source *volatile s = NULL; |
474 | struct shf *shf; |
475 | char **volatile old_argv; |
476 | volatile int old_argc; |
477 | int i; |
478 | |
479 | shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI|SHF_CLEXEC); |
480 | if (shf == NULL) |
481 | return -1; |
482 | |
483 | if (argv) { |
484 | old_argv = genv->loc->argv; |
485 | old_argc = genv->loc->argc; |
486 | } else { |
487 | old_argv = NULL; |
488 | old_argc = 0; |
489 | } |
490 | newenv(E_INCL); |
491 | i = sigsetjmp(genv->jbuf, 0); |
492 | if (i) { |
493 | quitenv(s ? s->u.shf : NULL); |
494 | if (old_argv) { |
495 | genv->loc->argv = old_argv; |
496 | genv->loc->argc = old_argc; |
497 | } |
498 | switch (i) { |
499 | case LRETURN: |
500 | case LERROR: |
501 | return exstat & 0xff; |
502 | case LINTR: |
503 | |
504 | |
505 | |
506 | if (intr_ok && (exstat - 128) != SIGTERM) |
507 | return 1; |
508 | |
509 | case LEXIT: |
510 | case LLEAVE: |
511 | case LSHELL: |
512 | unwind(i); |
513 | |
514 | default: |
515 | internal_errorf("%s: %d", __func__, i); |
516 | |
517 | } |
518 | } |
519 | if (argv) { |
520 | genv->loc->argv = argv; |
521 | genv->loc->argc = argc; |
522 | } |
523 | s = pushs(SFILE, ATEMP); |
524 | s->u.shf = shf; |
525 | s->file = str_save(name, ATEMP); |
526 | i = shell(s, false); |
527 | quitenv(s->u.shf); |
528 | if (old_argv) { |
529 | genv->loc->argv = old_argv; |
530 | genv->loc->argc = old_argc; |
531 | } |
532 | return i & 0xff; |
533 | } |
534 | |
535 | |
536 | |
537 | |
538 | |
539 | int |
540 | command(const char *comm, int line) |
541 | { |
542 | Source *s; |
543 | |
544 | s = pushs(SSTRING, ATEMP); |
545 | s->start = s->str = comm; |
546 | s->line = line; |
547 | return shell(s, false); |
548 | } |
549 | |
550 | |
551 | |
552 | |
553 | int |
554 | shell(Source *volatile s, volatile int toplevel) |
555 | { |
556 | struct op *t; |
557 | volatile int wastty = s->flags & SF_TTY; |
558 | volatile int attempts = 13; |
559 | volatile int interactive = Flag(FTALKING) && toplevel; |
560 | Source *volatile old_source = source; |
561 | int i; |
562 | |
563 | newenv(E_PARSE); |
564 | if (interactive) |
565 | really_exit = 0; |
566 | i = sigsetjmp(genv->jbuf, 0); |
567 | if (i) { |
568 | switch (i) { |
569 | case LINTR: |
570 | case LERROR: |
571 | case LSHELL: |
572 | if (interactive) { |
573 | c_fc_reset(); |
574 | if (i == LINTR) |
575 | shellf("\n"); |
576 | |
577 | |
578 | |
579 | if (Flag(FIGNOREEOF) && s->type == SEOF && |
580 | wastty) |
581 | s->type = SSTDIN; |
582 | |
583 | |
584 | |
585 | |
586 | |
587 | |
588 | |
589 | s->start = s->str = null; |
590 | break; |
591 | } |
592 | |
593 | case LEXIT: |
594 | case LLEAVE: |
595 | case LRETURN: |
596 | source = old_source; |
597 | quitenv(NULL); |
598 | unwind(i); |
599 | |
600 | default: |
601 | source = old_source; |
602 | quitenv(NULL); |
603 | internal_errorf("%s: %d", __func__, i); |
604 | |
605 | } |
606 | } |
607 | |
608 | while (1) { |
609 | if (trap) |
610 | runtraps(0); |
611 | |
612 | if (s->next == NULL) { |
613 | if (Flag(FVERBOSE)) |
614 | s->flags |= SF_ECHO; |
615 | else |
616 | s->flags &= ~SF_ECHO; |
617 | } |
618 | |
619 | if (interactive) { |
620 | got_sigwinch = 1; |
621 | j_notify(); |
622 | #ifndef SMALL |
623 | mcheck(); |
624 | #endif /* SMALL */ |
625 | set_prompt(PS1); |
626 | } |
627 | |
628 | t = compile(s); |
629 | if (t != NULL && t->type == TEOF) { |
630 | if (wastty && Flag(FIGNOREEOF) && --attempts > 0) { |
631 | shellf("Use `exit' to leave ksh\n"); |
632 | s->type = SSTDIN; |
633 | } else if (wastty && !really_exit && |
634 | j_stopped_running()) { |
635 | really_exit = 1; |
636 | s->type = SSTDIN; |
637 | } else { |
638 | |
639 | |
640 | |
641 | |
642 | |
643 | if (toplevel) |
644 | unwind(LEXIT); |
645 | break; |
646 | } |
647 | } |
648 | |
649 | if (t && (!Flag(FNOEXEC) || (s->flags & SF_TTY))) |
650 | exstat = execute(t, 0, NULL); |
651 | |
652 | if (t != NULL && t->type != TEOF && interactive && really_exit) |
653 | really_exit = 0; |
654 | |
655 | reclaim(); |
656 | } |
657 | quitenv(NULL); |
658 | source = old_source; |
659 | return exstat; |
660 | } |
661 | |
662 | |
663 | void |
664 | unwind(int i) |
665 | { |
666 | |
667 | if (i == LEXIT || (Flag(FERREXIT) && (i == LERROR || i == LINTR) && |
668 | sigtraps[SIGEXIT_].trap)) { |
669 | if (trap) |
670 | runtraps(0); |
671 | runtrap(&sigtraps[SIGEXIT_]); |
672 | i = LLEAVE; |
673 | } else if (Flag(FERREXIT) && (i == LERROR || i == LINTR)) { |
674 | if (trap) |
675 | runtraps(0); |
676 | runtrap(&sigtraps[SIGERR_]); |
677 | i = LLEAVE; |
678 | } |
679 | while (1) { |
680 | switch (genv->type) { |
681 | case E_PARSE: |
682 | case E_FUNC: |
683 | case E_INCL: |
684 | case E_LOOP: |
685 | case E_ERRH: |
686 | siglongjmp(genv->jbuf, i); |
687 | |
688 | |
689 | case E_NONE: |
690 | if (i == LINTR) |
691 | genv->flags |= EF_FAKE_SIGDIE; |
692 | |
693 | |
694 | default: |
695 | quitenv(NULL); |
696 | |
697 | |
698 | |
699 | |
700 | |
701 | |
702 | source = NULL; |
703 | } |
704 | } |
705 | } |
706 | |
707 | void |
708 | newenv(int type) |
709 | { |
710 | struct env *ep; |
711 | |
712 | ep = alloc(sizeof(*ep), ATEMP); |
713 | ep->type = type; |
714 | ep->flags = 0; |
715 | ainit(&ep->area); |
716 | ep->loc = genv->loc; |
717 | ep->savefd = NULL; |
718 | ep->oenv = genv; |
719 | ep->temps = NULL; |
720 | genv = ep; |
721 | } |
722 | |
723 | void |
724 | quitenv(struct shf *shf) |
725 | { |
726 | struct env *ep = genv; |
727 | int fd; |
728 | |
729 | if (ep->oenv && ep->oenv->loc != ep->loc) |
730 | popblock(); |
731 | if (ep->savefd != NULL) { |
732 | for (fd = 0; fd < NUFILE; fd++) |
733 | |
734 | if (ep->savefd[fd]) |
735 | restfd(fd, ep->savefd[fd]); |
736 | if (ep->savefd[2]) |
737 | shf_reopen(2, SHF_WR, shl_out); |
738 | } |
739 | |
740 | |
741 | |
742 | |
743 | if (ep->oenv == NULL) { |
744 | if (ep->type == E_NONE) { |
745 | if (Flag(FTALKING)) |
746 | hist_finish(); |
747 | j_exit(); |
748 | if (ep->flags & EF_FAKE_SIGDIE) { |
749 | int sig = exstat - 128; |
750 | |
751 | |
752 | |
753 | |
754 | |
755 | |
756 | if ((sig == SIGINT || sig == SIGTERM) && |
757 | getpgrp() == kshpid) { |
758 | setsig(&sigtraps[sig], SIG_DFL, |
759 | SS_RESTORE_CURR|SS_FORCE); |
760 | kill(0, sig); |
761 | } |
762 | } |
763 | } |
764 | if (shf) |
765 | shf_close(shf); |
766 | reclaim(); |
767 | exit(exstat); |
768 | } |
769 | if (shf) |
770 | shf_close(shf); |
771 | reclaim(); |
772 | |
773 | genv = genv->oenv; |
774 | afree(ep, ATEMP); |
775 | } |
776 | |
777 | |
778 | void |
779 | cleanup_parents_env(void) |
780 | { |
781 | struct env *ep; |
782 | int fd; |
783 | |
784 | |
785 | |
786 | |
787 | |
788 | |
789 | |
790 | for (ep = genv; ep; ep = ep->oenv) { |
| 1 | Assuming pointer value is null | |
|
| 2 | | Loop condition is false. Execution continues on line 799 | |
|
791 | if (ep->savefd) { |
792 | for (fd = 0; fd < NUFILE; fd++) |
793 | if (ep->savefd[fd] > 0) |
794 | close(ep->savefd[fd]); |
795 | afree(ep->savefd, &ep->area); |
796 | ep->savefd = NULL; |
797 | } |
798 | } |
799 | genv->oenv = NULL; |
| 3 | | Access to field 'oenv' results in a dereference of a null pointer (loaded from variable 'genv') |
|
800 | } |
801 | |
802 | |
803 | void |
804 | cleanup_proc_env(void) |
805 | { |
806 | struct env *ep; |
807 | |
808 | for (ep = genv; ep; ep = ep->oenv) |
809 | remove_temps(ep->temps); |
810 | } |
811 | |
812 | |
813 | static void |
814 | reclaim(void) |
815 | { |
816 | remove_temps(genv->temps); |
817 | genv->temps = NULL; |
818 | afreeall(&genv->area); |
819 | } |
820 | |
821 | static void |
822 | remove_temps(struct temp *tp) |
823 | { |
824 | |
825 | for (; tp != NULL; tp = tp->next) |
826 | if (tp->pid == procpid) { |
827 | unlink(tp->name); |
828 | } |
829 | } |
830 | |
831 | |
832 | static int |
833 | is_restricted(char *name) |
834 | { |
835 | char *p; |
836 | |
837 | if ((p = strrchr(name, '/'))) |
838 | name = p + 1; |
839 | |
840 | if (strcmp(name, "rsh") && \ |
841 | strcmp(name, "rksh") && \ |
842 | strcmp(name, "rpdksh") && \ |
843 | strcmp(name, "pdrksh")) |
844 | return(0); |
845 | else |
846 | return(1); |
847 | |
848 | } |