Bug Summary

File:java-utils.c
Warning:line 263, column 7
Although the value stored to 'i' is used in the enclosing expression, the value is never actually read from 'i'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name java-utils.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 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/rootdir/src -resource-dir /usr/lib/llvm-16/lib/clang/16 -D HAVE_CONFIG_H -I . -I .. -I .. -I .. -D GRAPA_RESOURCE_UI_PATH="/org/cafe/Grapa/ui" -D FR_PREFIX="/usr" -D FR_SYSCONFDIR="/usr/etc" -D FR_DATADIR="/usr/share" -D PRIVDATADIR="/usr/share/grapa/" -D FR_LIBDIR="/usr/lib" -D PKG_DATA_DIR="/usr/share/grapa" -D PIXMAPSDIR="/usr/share/pixmaps" -D GLADEDIR="" -D LOCALEDIR="/usr/share/locale" -D SHDIR="/usr/libexec/grapa/" -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/gio-unix-2.0 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/ctk-3.0 -I /usr/include/pango-1.0 -I /usr/include/harfbuzz -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/fribidi -I /usr/include/cairo -I /usr/include/pixman-1 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/x86_64-linux-gnu -I /usr/include/webp -I /usr/include/atk-1.0 -I /usr/include/at-spi2-atk/2.0 -I /usr/include/at-spi-2.0 -I /usr/include/dbus-1.0 -I /usr/lib/x86_64-linux-gnu/dbus-1.0/include -I /usr/include/json-glib-1.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/libmount -I /usr/include/blkid -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir=/rootdir/src -ferror-limit 19 -fgnuc-version=4.2.1 -analyzer-checker deadcode.DeadStores -analyzer-checker alpha.deadcode.UnreachableCode -analyzer-checker alpha.core.CastSize -analyzer-checker alpha.core.CastToStruct -analyzer-checker alpha.core.IdenticalExpr -analyzer-checker alpha.core.SizeofPtr -analyzer-checker alpha.security.ArrayBoundV2 -analyzer-checker alpha.security.MallocOverflow -analyzer-checker alpha.security.ReturnPtrRange -analyzer-checker alpha.unix.SimpleStream -analyzer-checker alpha.unix.cstring.BufferOverlap -analyzer-checker alpha.unix.cstring.NotNullTerminated -analyzer-checker alpha.unix.cstring.OutOfBounds -analyzer-checker alpha.core.FixedAddr -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /rootdir/html-report/2024-03-28-110600-51145-1 -x c java-utils.c
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3/*
4 * Grapa
5 *
6 * Copyright (C) 2006 The Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <config.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <errno(*__errno_location ()).h>
27#include <string.h>
28#include <ctype.h>
29#include <glib.h>
30#include "java-utils.h"
31
32
33/*
34 * The following code conforms to the JVM specification.(Java 2 Platform)
35 * For further changes to the classfile structure, please update the
36 * following macros.
37 */
38
39
40/* Tags that identify structures */
41
42#define CONST_CLASS7 7
43#define CONST_FIELDREF9 9
44#define CONST_METHODREF10 10
45#define CONST_INTERFACEMETHODREF11 11
46#define CONST_STRING8 8
47#define CONST_INTEGER3 3
48#define CONST_FLOAT4 4
49#define CONST_LONG5 5
50#define CONST_DOUBLE6 6
51#define CONST_NAMEANDTYPE12 12
52#define CONST_UTF81 1
53
54/* Sizes of structures */
55
56#define CONST_CLASS_INFO2 2
57#define CONST_FIELDREF_INFO4 4
58#define CONST_METHODREF_INFO4 4
59#define CONST_INTERFACEMETHODREF_INFO4 4
60#define CONST_STRING_INFO2 2
61#define CONST_INTEGER_INFO4 4
62#define CONST_FLOAT_INFO4 4
63#define CONST_LONG_INFO8 8
64#define CONST_DOUBLE_INFO8 8
65#define CONST_NAMEANDTYPE_INFO4 4
66
67
68/* represents the utf8 strings in class file */
69struct utf_string
70{
71 guint16 index;
72 guint16 length;
73 char *str;
74};
75
76/* structure that holds class information in a class file */
77struct class_info
78{
79 guint16 index;
80 guint16 name_index; /* index into the utf_strings */
81};
82
83typedef struct {
84 int fd;
85
86 guint32 magic_no; /* 0xCAFEBABE (JVM Specification) :) */
87
88 guint16 major; /* versions */
89 guint16 minor;
90
91 guint16 const_pool_count;
92 GSList *const_pool_class; /* (const_pool_count - 1) elements of tye 'CONST_class_info' */
93 GSList *const_pool_utf; /* (const_pool_count - 1) elements of type 'utf_strings' */
94
95 guint16 access_flags;
96 guint16 this_class; /* the index of the class the file is named after. */
97
98#if 0 /* not needed */
99 guint16 super_class;
100 guint16 interfaces_count;
101 guint16 *interfaces;
102 guint16 fields_count;
103 field_info *fields;
104 guint16 methods_count;
105 method_info *methods;
106 guint16 attributes_count;
107 attribute_info *attributes;
108#endif
109} JavaClassFile;
110
111
112static JavaClassFile*
113java_class_file_new (void)
114{
115 JavaClassFile *cfile;
116
117 cfile = g_new0 (JavaClassFile, 1)((JavaClassFile *) g_malloc0_n ((1), sizeof (JavaClassFile)));
118 cfile->fd = -1;
119
120 return cfile;
121}
122
123
124static void
125java_class_file_free (JavaClassFile *cfile)
126{
127 GSList *scan;
128
129 if (cfile->const_pool_class != NULL((void*)0))
130 g_slist_free_full (cfile->const_pool_class, g_free);
131
132 for (scan = cfile->const_pool_utf; scan ; scan = scan->next) {
133 struct utf_string *string = scan->data;
134 g_free (string->str);
135 }
136
137 if (cfile->const_pool_utf != NULL((void*)0))
138 g_slist_free_full (cfile->const_pool_utf, g_free);
139
140 if (cfile->fd != -1)
141 close (cfile->fd);
142
143 g_free (cfile);
144}
145
146
147/* The following function loads the utf8 strings and class structures from the
148 * class file. */
149static void
150load_constant_pool_utfs (JavaClassFile *cfile)
151{
152 guint8 tag;
153 guint16 i = 0; /* should be comparable with const_pool_count */
154
155 while ((i < cfile->const_pool_count - 1) && (read (cfile->fd, &tag, 1) != -1)) {
156 struct utf_string *txt = NULL((void*)0);
157 struct class_info *class = NULL((void*)0);
158
159 switch (tag) {
160 case CONST_CLASS7:
161 class = g_new0 (struct class_info, 1)((struct class_info *) g_malloc0_n ((1), sizeof (struct class_info
)))
;
162 class->index = i + 1;
163 if (read (cfile->fd, &class->name_index, 2) != 2) {
164 g_free (class);
165 return; /* error reading */
166 }
167 class->name_index = GUINT16_FROM_BE (class->name_index)(((((guint16) ( (guint16) ((guint16) (class->name_index) >>
8) | (guint16) ((guint16) (class->name_index) << 8)
)))))
;
168 cfile->const_pool_class = g_slist_append (cfile->const_pool_class, class);
169 break;
170
171 case CONST_FIELDREF9:
172 lseek (cfile->fd, CONST_FIELDREF_INFO4, SEEK_CUR1);
173 break;
174
175 case CONST_METHODREF10:
176 lseek (cfile->fd, CONST_METHODREF_INFO4, SEEK_CUR1);
177 break;
178
179 case CONST_INTERFACEMETHODREF11:
180 lseek (cfile->fd, CONST_INTERFACEMETHODREF_INFO4, SEEK_CUR1);
181 break;
182
183 case CONST_STRING8:
184 lseek (cfile->fd, CONST_STRING_INFO2, SEEK_CUR1);
185 break;
186
187 case CONST_INTEGER3:
188 lseek (cfile->fd, CONST_INTEGER_INFO4, SEEK_CUR1);
189 break;
190
191 case CONST_FLOAT4:
192 lseek (cfile->fd, CONST_FLOAT_INFO4, SEEK_CUR1);
193 break;
194
195 case CONST_LONG5:
196 lseek (cfile->fd, CONST_LONG_INFO8, SEEK_CUR1);
197 break;
198
199 case CONST_DOUBLE6:
200 lseek (cfile->fd, CONST_DOUBLE_INFO8, SEEK_CUR1);
201 break;
202
203 case CONST_NAMEANDTYPE12:
204 lseek (cfile->fd, CONST_NAMEANDTYPE_INFO4, SEEK_CUR1);
205 break;
206
207 case CONST_UTF81:
208 txt = g_new0 (struct utf_string, 1)((struct utf_string *) g_malloc0_n ((1), sizeof (struct utf_string
)))
;
209 txt->index = i + 1;
210 if (read (cfile->fd, &(txt->length), 2) == -1) {
211 g_free (txt);
212 return; /* error while reading */
213 }
214 txt->length = GUINT16_FROM_BE (txt->length)(((((guint16) ( (guint16) ((guint16) (txt->length) >>
8) | (guint16) ((guint16) (txt->length) << 8))))))
;
215 txt->str = g_new0 (char, txt->length)((char *) g_malloc0_n ((txt->length), sizeof (char)));
216 if (read (cfile->fd, txt->str, txt->length) == -1) {
217 g_free (txt);
218 return; /* error while reading */
219 }
220 cfile->const_pool_utf = g_slist_append (cfile->const_pool_utf, txt);
221 break;
222
223 default:
224 return; /* error - unknown tag in class file */
225 break;
226 }
227 i++;
228 }
229
230#ifdef CAFE_ENABLE_DEBUG
231 g_print( "Number of Entries: %d\n", i );
232#endif
233}
234
235
236static char*
237close_and_exit (JavaClassFile *cfile)
238{
239 java_class_file_free (cfile);
240 return NULL((void*)0);
241}
242
243
244/* This function extracts the package name from a class file */
245char*
246get_package_name_from_class_file (char *fname)
247{
248 char *package = NULL((void*)0);
249 JavaClassFile *cfile;
250 guint16 length = 0, end = 0, utf_index = 0;
251 guint32 magic;
252 guint16 major, minor, count;
253 guint i = 0;
254
255 if (! g_file_test (fname, G_FILE_TEST_EXISTS))
256 return NULL((void*)0);
257
258 cfile = java_class_file_new ();
259 cfile->fd = open (fname, O_RDONLY00);
260 if (cfile->fd == -1)
261 return close_and_exit (cfile);
262
263 if ((i = read (cfile->fd, &magic, 4)) != 4)
Although the value stored to 'i' is used in the enclosing expression, the value is never actually read from 'i'
264 return close_and_exit (cfile);
265 cfile->magic_no = GUINT32_FROM_BE (magic)(((((guint32) ( (((guint32) (magic) & (guint32) 0x000000ffU
) << 24) | (((guint32) (magic) & (guint32) 0x0000ff00U
) << 8) | (((guint32) (magic) & (guint32) 0x00ff0000U
) >> 8) | (((guint32) (magic) & (guint32) 0xff000000U
) >> 24))))))
;
266
267 if (read (cfile->fd, &major, 2 ) != 2)
268 return close_and_exit (cfile);
269 cfile->major = GUINT16_FROM_BE (major)(((((guint16) ( (guint16) ((guint16) (major) >> 8) | (guint16
) ((guint16) (major) << 8))))))
;
270
271 if (read (cfile->fd, &minor, 2) != 2)
272 return close_and_exit (cfile);
273 cfile->minor = GUINT16_FROM_BE (minor)(((((guint16) ( (guint16) ((guint16) (minor) >> 8) | (guint16
) ((guint16) (minor) << 8))))))
;
274
275 if (read (cfile->fd, &count, 2) != 2)
276 return close_and_exit (cfile);
277 cfile->const_pool_count = GUINT16_FROM_BE(count)(((((guint16) ( (guint16) ((guint16) (count) >> 8) | (guint16
) ((guint16) (count) << 8))))))
;
278 load_constant_pool_utfs (cfile);
279
280 if (read (cfile->fd, &cfile->access_flags, 2) != 2)
281 return close_and_exit (cfile);
282 cfile->access_flags = GUINT16_FROM_BE (cfile->access_flags)(((((guint16) ( (guint16) ((guint16) (cfile->access_flags)
>> 8) | (guint16) ((guint16) (cfile->access_flags) <<
8))))))
;
283
284 if (read (cfile->fd, &cfile->this_class, 2) != 2)
285 return close_and_exit (cfile);
286 cfile->this_class = GUINT16_FROM_BE(cfile->this_class)(((((guint16) ( (guint16) ((guint16) (cfile->this_class) >>
8) | (guint16) ((guint16) (cfile->this_class) << 8)
)))))
;
287
288 /* now search for the class structure with index = cfile->this_class */
289
290 for (i = 0; (i < g_slist_length (cfile->const_pool_class)) && (utf_index == 0); i++ ) {
291 struct class_info *class = g_slist_nth_data (cfile->const_pool_class, i);
292 if (class->index == cfile->this_class)
293 utf_index = class->name_index; /* terminates loop */
294 }
295
296 /* now search for the utf8 string with index = utf_index */
297
298 for (i = 0; i < g_slist_length (cfile->const_pool_utf); i++) {
299 struct utf_string *data = g_slist_nth_data (cfile->const_pool_utf, i);
300 if (data->index == utf_index) {
301 package = g_strndup (data->str, data->length);
302 length = data->length;
303 break;
304 }
305 }
306
307 if (package != NULL((void*)0)) {
308 int j;
309 for (j = length; (j >= 0) && (end == 0); j--)
310 if (package[j] == '/')
311 end = j;
312 char *package_padded = g_strndup (package, end);
313 g_free(package);
314 package = package_padded;
315 }
316
317 java_class_file_free (cfile);
318
319 return package;
320}
321
322
323/* This function consumes a comment from the java file
324 * multiline = TRUE implies that comment is multiline */
325static void
326consume_comment (int fdesc,
327 gboolean multiline)
328{
329 gboolean escaped = FALSE(0);
330 gboolean star = FALSE(0);
331 char ch;
332
333 while (read (fdesc, &ch, 1) == 1) {
334 switch (ch) {
335 case '/':
336 if (escaped)
337 break;
338 else if (star)
339 return;
340 break;
341
342 case '\n':
343 if (! multiline)
344 return;
345 break;
346
347 case '*':
348 escaped = FALSE(0);
349 star = TRUE(!(0));
350 break;
351
352 case '\\':
353 escaped = ! escaped;
354 break;
355
356 default:
357 escaped = FALSE(0);
358 star = FALSE(0);
359 break;
360 }
361 }
362}
363
364
365/* This function extracts package name from a java file */
366char*
367get_package_name_from_java_file (char *fname)
368{
369 char *package = NULL((void*)0);
370 JavaClassFile *cfile;
371 gboolean prev_char_is_bslash = FALSE(0);
372 gboolean valid_char_found = FALSE(0);
373 char ch;
374
375 if (! g_file_test (fname, G_FILE_TEST_EXISTS))
376 return NULL((void*)0);
377
378 cfile = java_class_file_new ();
379 cfile->fd = open (fname, O_RDONLY00);
380 if (cfile->fd == -1)
381 return close_and_exit (cfile);
382
383 while (! valid_char_found && (read (cfile->fd, &ch, 1) == 1)) {
384 switch (ch) {
385 case '/':
386 if (prev_char_is_bslash == TRUE(!(0))) {
387 consume_comment (cfile->fd, FALSE(0));
388 prev_char_is_bslash = FALSE(0);
389 }
390 else
391 prev_char_is_bslash = TRUE(!(0));
392 break;
393
394 case '*':
395 if (prev_char_is_bslash == TRUE(!(0)))
396 consume_comment (cfile->fd, TRUE(!(0)));
397 prev_char_is_bslash = FALSE(0);
398 break;
399
400 case ' ':
401 case '\t':
402 case '\r':
403 case '\n':
404 prev_char_is_bslash = FALSE(0);
405 break;
406
407 default:
408 prev_char_is_bslash = FALSE(0);
409 valid_char_found = TRUE(!(0));
410 break;
411 }
412 }
413
414 if (ch == 'p') {
415 char first_valid_word[8] = "";
416
417 first_valid_word[0] = 'p';
418 if (read (cfile->fd, &first_valid_word[1], 6) != 6)
419 return close_and_exit (cfile);
420
421 first_valid_word[7] = 0;
422 if (g_ascii_strcasecmp (first_valid_word, "package") == 0) {
423 char buffer[500];
424 int index = 0;
425
426 while (read (cfile->fd, &ch, 1) == 1) {
427 if ((ch != ' ') && (ch != '\t'))
428 break;
429 }
430 do {
431 if (ch == ';')
432 break;
433 if (ch == '.') {
434 buffer[index++] = '/';
435 } else if (isalnum (ch)((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int)
_ISalnum)
!= 0) {
436 buffer[index++] = ch;
437 } else if ((ch == '_') || (ch == '$')) {
438 buffer[index++] = ch;
439 } else {
440 index = 0;
441 break;
442 }
443 } while (read (cfile->fd, &ch, 1) == 1);
444 buffer[index] = 0;
445 package = g_strdup (buffer)g_strdup_inline (buffer);
446 }
447 }
448
449 java_class_file_free (cfile);
450
451 return package;
452}