Bug Summary

File:src/lib/libelf/elf_scn.c
Warning:line 207, column 8
Although the value stored to 'scn' is used in the enclosing expression, the value is never actually read from 'scn'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name elf_scn.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 -fno-rounding-math -mconstructor-aliases -munwind-tables -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/libelf/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/lib/libelf -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/lib/libelf/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -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/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/lib/libelf/elf_scn.c
1/*-
2 * Copyright (c) 2006,2008-2010 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/queue.h>
28
29#include <assert.h>
30#include <errno(*__errno()).h>
31#include <gelf.h>
32#include <libelf.h>
33#include <stddef.h>
34#include <stdint.h>
35#include <stdlib.h>
36
37#include "_libelf.h"
38
39ELFTC_VCSID("$Id: elf_scn.c,v 1.2 2019/03/19 02:31:35 jsg Exp $")__asm__(".ident\t\"" "$Id: elf_scn.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"
"\"")
;
40
41/*
42 * Load an ELF section table and create a list of Elf_Scn structures.
43 */
44int
45_libelf_load_section_headers(Elf *e, void *ehdr)
46{
47 Elf_Scn *scn;
48 uint64_t shoff;
49 Elf32_Ehdr *eh32;
50 Elf64_Ehdr *eh64;
51 int ec, swapbytes;
52 unsigned char *src;
53 size_t fsz, i, shnum;
54 _libelf_translator_function *xlator;
55
56 assert(e != NULL)((e != ((void*)0)) ? (void)0 : __assert2("/usr/src/lib/libelf/elf_scn.c"
, 56, __func__, "e != NULL"))
;
57 assert(ehdr != NULL)((ehdr != ((void*)0)) ? (void)0 : __assert2("/usr/src/lib/libelf/elf_scn.c"
, 57, __func__, "ehdr != NULL"))
;
58 assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0)(((e->e_flags & 0x200000U) == 0) ? (void)0 : __assert2
("/usr/src/lib/libelf/elf_scn.c", 58, __func__, "(e->e_flags & LIBELF_F_SHDRS_LOADED) == 0"
))
;
59
60#define CHECK_EHDR(E,EH)do { uintmax_t rawsize = (uintmax_t) e->e_rawsize; if (shoff
> (uintmax_t) e->e_rawsize || fsz != (EH)->e_shentsize
|| shnum > 0xffffffffffffffffUL / fsz || fsz * shnum >
rawsize - shoff) { do { (_libelf.libelf_error) = (((ELF_E_HEADER
) & 0xFF) | (((0)) << 8)); } while (0); return (0);
} } while (0)
do { \
61 uintmax_t rawsize = (uintmax_t) e->e_rawsize; \
62 if (shoff > (uintmax_t) e->e_rawsize || \
63 fsz != (EH)->e_shentsize || \
64 shnum > SIZE_MAX0xffffffffffffffffUL / fsz || \
65 fsz * shnum > rawsize - shoff) { \
66 LIBELF_SET_ERROR(HEADER, 0)do { (_libelf.libelf_error) = (((ELF_E_HEADER) & 0xFF) | (
((0)) << 8)); } while (0)
; \
67 return (0); \
68 } \
69 } while (0)
70
71 ec = e->e_class;
72 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
73 assert(fsz > 0)((fsz > 0) ? (void)0 : __assert2("/usr/src/lib/libelf/elf_scn.c"
, 73, __func__, "fsz > 0"))
;
74
75 shnum = e->e_u.e_elf.e_nscn;
76
77 if (ec == ELFCLASS321) {
78 eh32 = (Elf32_Ehdr *) ehdr;
79 shoff = (uint64_t) eh32->e_shoff;
80 CHECK_EHDR(e, eh32)do { uintmax_t rawsize = (uintmax_t) e->e_rawsize; if (shoff
> (uintmax_t) e->e_rawsize || fsz != (eh32)->e_shentsize
|| shnum > 0xffffffffffffffffUL / fsz || fsz * shnum >
rawsize - shoff) { do { (_libelf.libelf_error) = (((ELF_E_HEADER
) & 0xFF) | (((0)) << 8)); } while (0); return (0);
} } while (0)
;
81 } else {
82 eh64 = (Elf64_Ehdr *) ehdr;
83 shoff = eh64->e_shoff;
84 CHECK_EHDR(e, eh64)do { uintmax_t rawsize = (uintmax_t) e->e_rawsize; if (shoff
> (uintmax_t) e->e_rawsize || fsz != (eh64)->e_shentsize
|| shnum > 0xffffffffffffffffUL / fsz || fsz * shnum >
rawsize - shoff) { do { (_libelf.libelf_error) = (((ELF_E_HEADER
) & 0xFF) | (((0)) << 8)); } while (0); return (0);
} } while (0)
;
85 }
86
87 xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec,
88 _libelf_elfmachine(e));
89
90 swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder)(_libelf.libelf_byteorder);
91 src = e->e_rawfile + shoff;
92
93 /*
94 * If the file is using extended numbering then section #0
95 * would have already been read in.
96 */
97
98 i = 0;
99 if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)(((&e->e_u.e_elf.e_scn)->stqh_first) == ((void*)0))) {
100 assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) ==((((&e->e_u.e_elf.e_scn)->stqh_first) == (((((&
e->e_u.e_elf.e_scn))->stqh_first) == ((void*)0)) ? ((void
*)0) : ((struct _Elf_Scn *)(void *) ((char *)((&e->e_u
.e_elf.e_scn)->stqh_last) - __builtin_offsetof(struct _Elf_Scn
, s_next))))) ? (void)0 : __assert2("/usr/src/lib/libelf/elf_scn.c"
, 101, __func__, "STAILQ_FIRST(&e->e_u.e_elf.e_scn) == STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next)"
))
101 STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next))((((&e->e_u.e_elf.e_scn)->stqh_first) == (((((&
e->e_u.e_elf.e_scn))->stqh_first) == ((void*)0)) ? ((void
*)0) : ((struct _Elf_Scn *)(void *) ((char *)((&e->e_u
.e_elf.e_scn)->stqh_last) - __builtin_offsetof(struct _Elf_Scn
, s_next))))) ? (void)0 : __assert2("/usr/src/lib/libelf/elf_scn.c"
, 101, __func__, "STAILQ_FIRST(&e->e_u.e_elf.e_scn) == STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next)"
))
;
102
103 i = 1;
104 src += fsz;
105 }
106
107 for (; i < shnum; i++, src += fsz) {
108 if ((scn = _libelf_allocate_scn(e, i)) == NULL((void*)0))
109 return (0);
110
111 (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
112 src, (size_t) 1, swapbytes);
113
114 if (ec == ELFCLASS321) {
115 scn->s_offset = scn->s_rawoff =
116 scn->s_shdr.s_shdr32.sh_offset;
117 scn->s_size = scn->s_shdr.s_shdr32.sh_size;
118 } else {
119 scn->s_offset = scn->s_rawoff =
120 scn->s_shdr.s_shdr64.sh_offset;
121 scn->s_size = scn->s_shdr.s_shdr64.sh_size;
122 }
123 }
124
125 e->e_flags |= LIBELF_F_SHDRS_LOADED0x200000U;
126
127 return (1);
128}
129
130
131Elf_Scn *
132elf_getscn(Elf *e, size_t index)
133{
134 int ec;
135 void *ehdr;
136 Elf_Scn *s;
137
138 if (e == NULL((void*)0) || e->e_kind != ELF_K_ELF ||
139 ((ec = e->e_class) != ELFCLASS321 && ec != ELFCLASS642)) {
140 LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) |
(((0)) << 8)); } while (0)
;
141 return (NULL((void*)0));
142 }
143
144 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL((void*)0))
145 return (NULL((void*)0));
146
147 if (e->e_cmd != ELF_C_WRITE &&
148 (e->e_flags & LIBELF_F_SHDRS_LOADED0x200000U) == 0 &&
149 _libelf_load_section_headers(e, ehdr) == 0)
150 return (NULL((void*)0));
151
152 STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)for ((s) = ((&e->e_u.e_elf.e_scn)->stqh_first); (s)
!= ((void*)0); (s) = ((s)->s_next.stqe_next))
153 if (s->s_ndx == index)
154 return (s);
155
156 LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) |
(((0)) << 8)); } while (0)
;
157 return (NULL((void*)0));
158}
159
160size_t
161elf_ndxscn(Elf_Scn *s)
162{
163 if (s == NULL((void*)0)) {
164 LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) |
(((0)) << 8)); } while (0)
;
165 return (SHN_UNDEF0);
166 }
167 return (s->s_ndx);
168}
169
170Elf_Scn *
171elf_newscn(Elf *e)
172{
173 int ec;
174 void *ehdr;
175 Elf_Scn *scn;
176
177 if (e == NULL((void*)0) || e->e_kind != ELF_K_ELF) {
178 LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) |
(((0)) << 8)); } while (0)
;
179 return (NULL((void*)0));
180 }
181
182 if ((ec = e->e_class) != ELFCLASS321 && ec != ELFCLASS642) {
183 LIBELF_SET_ERROR(CLASS, 0)do { (_libelf.libelf_error) = (((ELF_E_CLASS) & 0xFF) | (
((0)) << 8)); } while (0)
;
184 return (NULL((void*)0));
185 }
186
187 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL((void*)0))
188 return (NULL((void*)0));
189
190 /*
191 * The application may be asking for a new section descriptor
192 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We
193 * need to bring in the existing section information before
194 * appending a new one to the list.
195 *
196 * Per the ELF(3) API, an application is allowed to open a
197 * file using ELF_C_READ, mess with its internal structure and
198 * use elf_update(...,ELF_C_NULL) to compute its new layout.
199 */
200 if (e->e_cmd != ELF_C_WRITE &&
201 (e->e_flags & LIBELF_F_SHDRS_LOADED0x200000U) == 0 &&
202 _libelf_load_section_headers(e, ehdr) == 0)
203 return (NULL((void*)0));
204
205 if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)(((&e->e_u.e_elf.e_scn)->stqh_first) == ((void*)0))) {
206 assert(e->e_u.e_elf.e_nscn == 0)((e->e_u.e_elf.e_nscn == 0) ? (void)0 : __assert2("/usr/src/lib/libelf/elf_scn.c"
, 206, __func__, "e->e_u.e_elf.e_nscn == 0"))
;
207 if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF0)) ==
Although the value stored to 'scn' is used in the enclosing expression, the value is never actually read from 'scn'
208 NULL((void*)0))
209 return (NULL((void*)0));
210 e->e_u.e_elf.e_nscn++;
211 }
212
213 assert(e->e_u.e_elf.e_nscn > 0)((e->e_u.e_elf.e_nscn > 0) ? (void)0 : __assert2("/usr/src/lib/libelf/elf_scn.c"
, 213, __func__, "e->e_u.e_elf.e_nscn > 0"))
;
214
215 if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL((void*)0))
216 return (NULL((void*)0));
217
218 e->e_u.e_elf.e_nscn++;
219
220 (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY0x002U);
221
222 return (scn);
223}
224
225Elf_Scn *
226elf_nextscn(Elf *e, Elf_Scn *s)
227{
228 if (e == NULL((void*)0) || (e->e_kind != ELF_K_ELF) ||
229 (s && s->s_elf != e)) {
230 LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) |
(((0)) << 8)); } while (0)
;
231 return (NULL((void*)0));
232 }
233
234 return (s == NULL((void*)0) ? elf_getscn(e, (size_t) 1) :
235 STAILQ_NEXT(s, s_next)((s)->s_next.stqe_next));
236}