Bug Summary

File:jdk/src/java.base/unix/native/libjava/path_util.c
Warning:line 134, column 13
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name path_util.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 2 -mthread-model posix -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base/linux -I /home/daniel/Projects/java/jdk/src/java.base/share/native/libjava -I /home/daniel/Projects/java/jdk/src/java.base/unix/native/libjava -I /home/daniel/Projects/java/jdk/src/hotspot/share/include -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix/include -D LIBC=gnu -D _GNU_SOURCE -D _REENTRANT -D _LARGEFILE64_SOURCE -D LINUX -D DEBUG -D _LITTLE_ENDIAN -D ARCH="amd64" -D amd64 -D _LP64=1 -D ARCHPROPNAME="amd64" -I /home/daniel/Projects/java/jdk/src/java.base/linux/native/libjava -I /home/daniel/Projects/java/jdk/src/java.base/unix/native/libjava -I /home/daniel/Projects/java/jdk/src/java.base/share/native/libjava -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/headers/java.base -I /home/daniel/Projects/java/jdk/src/java.base/share/native/libfdlibm -D _FORTIFY_SOURCE=2 -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wno-unused -Wno-unused-result -Wno-unused-function -std=c99 -fdebug-compilation-dir /home/daniel/Projects/java/jdk/make -ferror-limit 19 -fmessage-length 0 -fvisibility hidden -stack-protector 1 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /home/daniel/Projects/java/scan/2021-12-21-193737-8510-1 -x c /home/daniel/Projects/java/jdk/src/java.base/unix/native/libjava/path_util.c
1/*
2 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include <stdlib.h>
27#include <string.h>
28#if !defined(_ALLBSD_SOURCE)
29#include <alloca.h>
30#endif
31
32#include "path_util.h"
33
34/* Check the given name sequence to see if it can be further collapsed.
35 Return zero if not, otherwise return the number of names in the sequence. */
36
37static int
38collapsible(char *names)
39{
40 char *p = names;
41 int dots = 0, n = 0;
42
43 while (*p) {
44 if ((p[0] == '.') && ((p[1] == '\0')
45 || (p[1] == '/')
46 || ((p[1] == '.') && ((p[2] == '\0')
47 || (p[2] == '/'))))) {
48 dots = 1;
49 }
50 n++;
51 while (*p) {
52 if (*p == '/') {
53 p++;
54 break;
55 }
56 p++;
57 }
58 }
59 return (dots ? n : 0);
60}
61
62
63/* Split the names in the given name sequence,
64 replacing slashes with nulls and filling in the given index array */
65
66static void
67splitNames(char *names, char **ix)
68{
69 char *p = names;
70 int i = 0;
71
72 while (*p) {
6
Loop condition is false. Execution continues on line 72
73 ix[i++] = p++;
74 while (*p) {
75 if (*p == '/') {
76 *p++ = '\0';
77 break;
78 }
79 p++;
80 }
81 }
82}
83
84
85/* Join the names in the given name sequence, ignoring names whose index
86 entries have been cleared and replacing nulls with slashes as needed */
87
88static void
89joinNames(char *names, int nc, char **ix)
90{
91 int i;
92 char *p;
93
94 for (i = 0, p = names; i < nc; i++) {
95 if (!ix[i]) continue;
96 if (i > 0) {
97 p[-1] = '/';
98 }
99 if (p == ix[i]) {
100 p += strlen(p) + 1;
101 } else {
102 char *q = ix[i];
103 while ((*p++ = *q++));
104 }
105 }
106 *p = '\0';
107}
108
109/* Collapse "." and ".." names in the given path wherever possible.
110 A "." name may always be eliminated; a ".." name may be eliminated if it
111 follows a name that is neither "." nor "..". This is a syntactic operation
112 that performs no filesystem queries, so it should only be used to cleanup
113 after invoking the realpath() procedure. */
114
115void
116collapse(char *path)
117{
118 char *names = (path[0] == '/') ? path + 1 : path; /* Preserve first '/' */
1
Assuming the condition is false
2
'?' condition is false
119 int nc;
120 char **ix;
121 int i, j;
122 char *p, *q;
123
124 nc = collapsible(names);
125 if (nc < 2) return; /* Nothing to do */
3
Assuming 'nc' is >= 2
4
Taking false branch
126 ix = (char **)alloca(nc * sizeof(char *))__builtin_alloca (nc * sizeof(char *));
127 splitNames(names, ix);
5
Calling 'splitNames'
7
Returning from 'splitNames'
128
129 for (i = 0; i
8.1
'i' is < 'nc'
< nc; i++) {
8
The value 0 is assigned to 'i'
9
Loop condition is true. Entering loop body
130 int dots = 0;
131
132 /* Find next occurrence of "." or ".." */
133 do {
134 char *p = ix[i];
10
Assigned value is garbage or undefined
135 if (p[0] == '.') {
136 if (p[1] == '\0') {
137 dots = 1;
138 break;
139 }
140 if ((p[1] == '.') && (p[2] == '\0')) {
141 dots = 2;
142 break;
143 }
144 }
145 i++;
146 } while (i < nc);
147 if (i >= nc) break;
148
149 /* At this point i is the index of either a "." or a "..", so take the
150 appropriate action and then continue the outer loop */
151 if (dots == 1) {
152 /* Remove this instance of "." */
153 ix[i] = 0;
154 }
155 else {
156 /* If there is a preceding name, remove both that name and this
157 instance of ".."; otherwise, leave the ".." as is */
158 for (j = i - 1; j >= 0; j--) {
159 if (ix[j]) break;
160 }
161 if (j < 0) continue;
162 ix[j] = 0;
163 ix[i] = 0;
164 }
165 /* i will be incremented at the top of the loop */
166 }
167
168 joinNames(names, nc, ix);
169}