Bug Summary

File:src/lib/libelf/libelf_phdr.c
Warning:line 123, column 7
Although the value stored to 'ehdr' is used in the enclosing expression, the value is never actually read from 'ehdr'

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 libelf_phdr.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/libelf_phdr.c
1/*-
2 * Copyright (c) 2006,2008 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 <assert.h>
28#include <gelf.h>
29#include <libelf.h>
30#include <stdlib.h>
31
32#include "_libelf.h"
33
34ELFTC_VCSID("$Id: libelf_phdr.c,v 1.3 2020/05/18 06:46:23 jsg Exp $")__asm__(".ident\t\"" "$Id: libelf_phdr.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"
"\"")
;
35
36void *
37_libelf_getphdr(Elf *e, int ec)
38{
39 size_t phnum;
40 size_t fsz, msz;
41 uint64_t phoff;
42 Elf32_Ehdr *eh32;
43 Elf64_Ehdr *eh64;
44 void *ehdr, *phdr;
45 _libelf_translator_function *xlator;
46
47 assert(ec == ELFCLASS32 || ec == ELFCLASS64)((ec == 1 || ec == 2) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_phdr.c"
, 47, __func__, "ec == ELFCLASS32 || ec == ELFCLASS64"))
;
48
49 if (e == NULL((void *)0)) {
50 LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) |
(((0)) << 8)); } while (0)
;
51 return (NULL((void *)0));
52 }
53
54 if ((phdr = (ec == ELFCLASS321 ?
55 (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
56 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL((void *)0))
57 return (phdr);
58
59 /*
60 * Check the PHDR related fields in the EHDR for sanity.
61 */
62
63 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL((void *)0))
64 return (NULL((void *)0));
65
66 phnum = e->e_u.e_elf.e_nphdr;
67
68 if (ec == ELFCLASS321) {
69 eh32 = (Elf32_Ehdr *) ehdr;
70 phoff = (uint64_t) eh32->e_phoff;
71 } else {
72 eh64 = (Elf64_Ehdr *) ehdr;
73 phoff = (uint64_t) eh64->e_phoff;
74 }
75
76 fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
77
78 assert(fsz > 0)((fsz > 0) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_phdr.c"
, 78, __func__, "fsz > 0"))
;
79
80 if (phoff + fsz < phoff) { /* Numeric overflow. */
81 LIBELF_SET_ERROR(HEADER, 0)do { (_libelf.libelf_error) = (((ELF_E_HEADER) & 0xFF) | (
((0)) << 8)); } while (0)
;
82 return (NULL((void *)0));
83 }
84
85 if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
86 LIBELF_SET_ERROR(HEADER, 0)do { (_libelf.libelf_error) = (((ELF_E_HEADER) & 0xFF) | (
((0)) << 8)); } while (0)
;
87 return (NULL((void *)0));
88 }
89
90 if ((msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT1)) == 0)
91 return (NULL((void *)0));
92
93 if ((phdr = calloc(phnum, msz)) == NULL((void *)0)) {
94 LIBELF_SET_ERROR(RESOURCE, 0)do { (_libelf.libelf_error) = (((ELF_E_RESOURCE) & 0xFF) |
(((0)) << 8)); } while (0)
;
95 return (NULL((void *)0));
96 }
97
98 if (ec == ELFCLASS321)
99 e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
100 else
101 e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
102
103
104 xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec,
105 _libelf_elfmachine(e));
106 (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
107 e->e_byteorder != LIBELF_PRIVATE(byteorder)(_libelf.libelf_byteorder));
108
109 return (phdr);
110}
111
112void *
113_libelf_newphdr(Elf *e, int ec, size_t count)
114{
115 void *ehdr, *newphdr, *oldphdr;
116 size_t msz;
117
118 if (e == NULL((void *)0)) {
119 LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) |
(((0)) << 8)); } while (0)
;
120 return (NULL((void *)0));
121 }
122
123 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL((void *)0)) {
Although the value stored to 'ehdr' is used in the enclosing expression, the value is never actually read from 'ehdr'
124 LIBELF_SET_ERROR(SEQUENCE, 0)do { (_libelf.libelf_error) = (((ELF_E_SEQUENCE) & 0xFF) |
(((0)) << 8)); } while (0)
;
125 return (NULL((void *)0));
126 }
127
128 assert(e->e_class == ec)((e->e_class == ec) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_phdr.c"
, 128, __func__, "e->e_class == ec"))
;
129 assert(ec == ELFCLASS32 || ec == ELFCLASS64)((ec == 1 || ec == 2) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_phdr.c"
, 129, __func__, "ec == ELFCLASS32 || ec == ELFCLASS64"))
;
130 assert(e->e_version == EV_CURRENT)((e->e_version == 1) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_phdr.c"
, 130, __func__, "e->e_version == EV_CURRENT"))
;
131
132 if ((msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version)) == 0)
133 return (NULL((void *)0));
134
135 newphdr = NULL((void *)0);
136 if (count > 0 && (newphdr = calloc(count, msz)) == NULL((void *)0)) {
137 LIBELF_SET_ERROR(RESOURCE, 0)do { (_libelf.libelf_error) = (((ELF_E_RESOURCE) & 0xFF) |
(((0)) << 8)); } while (0)
;
138 return (NULL((void *)0));
139 }
140
141 if (ec == ELFCLASS321) {
142 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL((void *)0))
143 free(oldphdr);
144 e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
145 } else {
146 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL((void *)0))
147 free(oldphdr);
148 e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
149 }
150
151 e->e_u.e_elf.e_nphdr = count;
152
153 elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY0x002U);
154
155 return (newphdr);
156}