Bug Summary

File:src/lib/libc/stdlib/setenv.c
Warning:line 110, column 7
Although the value stored to 'C' is used in the enclosing expression, the value is never actually read from 'C'

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 setenv.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/lib/libc/obj -resource-dir /usr/local/llvm16/lib/clang/16 -include namespace.h -I /usr/src/lib/libc/include -I /usr/src/lib/libc/hidden -D __LIBC__ -D APIWARN -D YP -I /usr/src/lib/libc/yp -I /usr/src/lib/libc -I /usr/src/lib/libc/gdtoa -I /usr/src/lib/libc/arch/amd64/gdtoa -D INFNAN_CHECK -D MULTIPLE_THREADS -D NO_FENV_H -D USE_LOCALE -I /usr/src/lib/libc -I /usr/src/lib/libc/citrus -D RESOLVSORT -D FLOATING_POINT -D PRINTF_WIDE_CHAR -D SCANF_WIDE_CHAR -D FUTEX -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/lib/libc/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/lib/libc/stdlib/setenv.c
1/* $OpenBSD: setenv.c,v 1.20 2022/08/08 22:40:03 millert Exp $ */
2/*
3 * Copyright (c) 1987 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <errno(*__errno()).h>
32#include <stdlib.h>
33#include <string.h>
34
35static char **lastenv; /* last value of environ */
36
37/*
38 * putenv --
39 * Add a name=value string directly to the environmental, replacing
40 * any current value.
41 */
42int
43putenv(char *str)
44{
45 char **P, *cp;
46 size_t cnt = 0;
47 int offset = 0;
48
49 for (cp = str; *cp && *cp != '='; ++cp)
50 ;
51 if (cp == str || *cp != '=') {
52 /* '=' is the first character of string or is missing. */
53 errno(*__errno()) = EINVAL22;
54 return (-1);
55 }
56
57 if (__findenv(str, (int)(cp - str), &offset) != NULL((void *)0)) {
58 environ[offset++] = str;
59 /* could be set multiple times */
60 while (__findenv(str, (int)(cp - str), &offset)) {
61 for (P = &environ[offset];; ++P)
62 if (!(*P = *(P + 1)))
63 break;
64 }
65 return (0);
66 }
67
68 /* create new slot for string */
69 if (environ != NULL((void *)0)) {
70 for (P = environ; *P != NULL((void *)0); P++)
71 ;
72 cnt = P - environ;
73 }
74 P = reallocarray(lastenv, cnt + 2, sizeof(char *));
75 if (!P)
76 return (-1);
77 if (lastenv != environ && environ != NULL((void *)0))
78 memcpy(P, environ, cnt * sizeof(char *));
79 lastenv = environ = P;
80 environ[cnt] = str;
81 environ[cnt + 1] = NULL((void *)0);
82 return (0);
83}
84DEF_WEAK(putenv)__asm__(".weak " "putenv" " ; " "putenv" " = " "_libc_putenv"
)
;
85
86/*
87 * setenv --
88 * Set the value of the environmental variable "name" to be
89 * "value". If rewrite is set, replace any current value.
90 */
91int
92setenv(const char *name, const char *value, int rewrite)
93{
94 char *C, **P;
95 const char *np;
96 int l_value, offset = 0;
97
98 if (!name || !*name) {
99 errno(*__errno()) = EINVAL22;
100 return (-1);
101 }
102 for (np = name; *np && *np != '='; ++np)
103 ;
104 if (*np) {
105 errno(*__errno()) = EINVAL22;
106 return (-1); /* has `=' in name */
107 }
108
109 l_value = strlen(value);
110 if ((C = __findenv(name, (int)(np - name), &offset)) != NULL((void *)0)) {
Although the value stored to 'C' is used in the enclosing expression, the value is never actually read from 'C'
111 int tmpoff = offset + 1;
112 if (!rewrite)
113 return (0);
114#if 0 /* XXX - existing entry may not be writable */
115 if (strlen(C) >= l_value) { /* old larger; copy over */
116 while ((*C++ = *value++))
117 ;
118 return (0);
119 }
120#endif
121 /* could be set multiple times */
122 while (__findenv(name, (int)(np - name), &tmpoff)) {
123 for (P = &environ[tmpoff];; ++P)
124 if (!(*P = *(P + 1)))
125 break;
126 }
127 } else { /* create new slot */
128 size_t cnt = 0;
129
130 if (environ != NULL((void *)0)) {
131 for (P = environ; *P != NULL((void *)0); P++)
132 ;
133 cnt = P - environ;
134 }
135 P = reallocarray(lastenv, cnt + 2, sizeof(char *));
136 if (!P)
137 return (-1);
138 if (lastenv != environ && environ != NULL((void *)0))
139 memcpy(P, environ, cnt * sizeof(char *));
140 lastenv = environ = P;
141 offset = cnt;
142 environ[cnt + 1] = NULL((void *)0);
143 }
144 if (!(environ[offset] = /* name + `=' + value */
145 malloc((int)(np - name) + l_value + 2)))
146 return (-1);
147 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
148 ;
149 for (*C++ = '='; (*C++ = *value++); )
150 ;
151 return (0);
152}
153DEF_WEAK(setenv)__asm__(".weak " "setenv" " ; " "setenv" " = " "_libc_setenv"
)
;
154
155/*
156 * unsetenv(name) --
157 * Delete environmental variable "name".
158 */
159int
160unsetenv(const char *name)
161{
162 char **P;
163 const char *np;
164 int offset = 0;
165
166 if (!name || !*name) {
167 errno(*__errno()) = EINVAL22;
168 return (-1);
169 }
170 for (np = name; *np && *np != '='; ++np)
171 ;
172 if (*np) {
173 errno(*__errno()) = EINVAL22;
174 return (-1); /* has `=' in name */
175 }
176
177 /* could be set multiple times */
178 while (__findenv(name, (int)(np - name), &offset)) {
179 for (P = &environ[offset];; ++P)
180 if (!(*P = *(P + 1)))
181 break;
182 }
183 return (0);
184}
185DEF_WEAK(unsetenv)__asm__(".weak " "unsetenv" " ; " "unsetenv" " = " "_libc_unsetenv"
)
;