-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjlint.cc
1107 lines (1029 loc) · 34.7 KB
/
jlint.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-< JLINT.CC >------------------------------------------------------+--------+
// Jlint Version 3.1 (c) 1998 GARRET | ? |
// (Java Lint) | /\| |
// | / \ |
// Created: 28-Mar-98 K.A. Knizhnik | / [] \ |
// Version 2.X: Last update: 05-Jun-01 Cyrille Artho | GARRET |
// Version 3.0: Last update: 20-Aug-03 Raphael Ackermann | |
// Version 3.1: Last update: 13-Oct-06 Cyrille Artho | |
//-------------------------------------------------------------------+--------+
// Java verifier
//-------------------------------------------------------------------+--------+
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <ctype.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#endif
#include "jlint.hh"
extern "C"
{
#include <zlib.h> // should I use <zlib.h> or "zlib.h" ??
// #include "zlib.h"
};
bool verbose = false;
int max_shown_paths = 4;
int n_messages;
char* source_file_path;
int source_file_path_len;
bool source_path_redefined = false;
int reported_message_mask = cat_all;
FILE* history;
string_pool stringPool;
field_desc* is_const;
message_descriptor msg_table[] =
{
#define MSG(category, code, position_dependent, format) \
{cat_##category, MSG_LOCATION_PREFIX format, #code, position_dependent, true},
#include "jlint.msg"
{cat_all}
};
void set_class_source_path(class_desc* cls);
void usage();
bool parse_class_file(byte* fp);
void proceed_file(char* file_name, bool recursive);
unsigned int string_hash_function(byte* p) {
unsigned int h = 0, g;
while(*p) {
h = (h << 4) + *p++;
if ((g = h & 0xF0000000) != 0) {
h ^= g >> 24;
}
h &= ~g;
}
return h;
}
//
// Functions for extracting information from type descriptors
//
void message_at(int code, utf_string const& file, int line, ...)
{
va_list ap;
va_start(ap, line);
format_message(code, file, line, ap);
va_end(ap);
}
int get_number_of_parameters(utf_string const& str)
{
const char* p = str.as_asciz();
// TODO: assertion changes variable
assert(*p++ =='(');
int n_params = 0;
while (*p != ')') {
switch (*p++) {
case '[':
while (*p == '[') p += 1;
if (*p++ == 'L') {
while (*p++ != ';');
}
n_params += 1;
break;
case 'D':
case 'J':
n_params += 2;
break;
case 'L':
while (*p++ != ';');
nobreak;
default:
n_params += 1;
}
}
return n_params;
}
int get_type(utf_string const& str)
{
const char* p = str.as_asciz();
if (*p == '(') {
while (*++p != ')');
p += 1;
}
int indirect = 0;
while (*p == '[') {
p += 1;
indirect += 1;
}
type_tag tag = tp_object;
switch (*p) {
case 'I': tag = tp_int; break;
case 'S': tag = tp_short; break;
case 'D': tag = tp_double; break;
case 'J': tag = tp_long; break;
case 'F': tag = tp_float; break;
case 'V': tag = tp_void; break;
case 'B': tag = tp_byte; break;
case 'C': tag = tp_char; break;
case 'Z': tag = tp_bool; break;
default:
if (strcmp(p, "Ljava/lang/String;") == 0) tag = tp_string;
}
return int(tag) + (indirect << 8);
}
//
// All messages are reported by this function
//
void format_message(int code, utf_string const& file, int line, __VALIST ap)
{
static int loop_id;
static message_node *first, *last;
static char* compound_message;
const void* parameter[MAX_MSG_PARAMETERS];
int n_parameters = 2;
if (code == msg_loop || code == msg_sync_loop) { // extract loop identifier
parameter[n_parameters++] = va_arg(ap, void*);
}
if (history != NULL) {
if (compound_message != NULL
&& ((loop_id != 0
&& ((code != msg_loop && code != msg_sync_loop)
|| (int)(long)parameter[2] != loop_id))
|| (loop_id == 0 && code != msg_wait_path)))
{
if (!message_node::find(compound_message)) {
message_node *msg = first, *next;
do {
next = msg->next;
fprintf(stdout, "%s\n", msg->text);
delete msg;
n_messages += 1;
} while ((msg = next) != NULL);
fprintf(history, "%s\n", compound_message);
}
delete[] compound_message;
compound_message = NULL;
}
}
if ((reported_message_mask & msg_table[code].category)
&& msg_table[code].enabled)
{
static int prev_line = 0;
char msg_buf[MAX_MSG_LENGTH];
char his_buf[MAX_MSG_LENGTH];
char* hp = his_buf;
if (line == 0) {
line = ++prev_line; // avoid several messages with 0 line number
} else {
prev_line = 0;
}
parameter[0] = file.as_asciz();
parameter[1] = (void*)line;
if (history) {
hp += sprintf(hp, "%s", msg_table[code].name);
}
char const* src = msg_table[code].format;
char* dst = msg_buf;
if (code == msg_done) {
// do not output location prefix for termination message
src += strlen(MSG_LOCATION_PREFIX);
parameter[0] = (void*)n_messages;
}
while (*src != 0) {
if (*src == '%') {
int index;
int pos;
int n = sscanf(++src, "%d%n", &index, &pos);
assert(n == 1);
assert(index < MAX_MSG_PARAMETERS);
while (index >= n_parameters) {
parameter[n_parameters++] = va_arg(ap, void*);
}
src += pos;
char* save_dst = dst;
switch (*src++) {
case 's': // zero terminated string
dst += sprintf(dst, "%s", (char*)parameter[index]);
break;
case 'm': // method descriptor
dst += ((method_desc*)parameter[index])->
demangle_method_name(dst);
break;
case 'u': // utf8 string
dst += sprintf(dst, "%s",
((utf_string*)parameter[index])->as_asciz());
break;
case 'c': // class descriptor
dst += sprintf(dst, "%s", ((class_desc*)parameter[index])->
name.as_asciz());
break;
case 'd': // integer
dst += sprintf(dst, "%d", (int)(long)parameter[index]);
break;
default:
assert(false/*bad message parameter format*/);
}
if (history) {
// append parameeter to history buffer
if ((index >= 2 || msg_table[code].position_dependent)
&& (code != msg_sync_loop || index > 2)
&& (code != msg_loop || index > 3))
{
// Do not inlude loop number in history message
hp += sprintf(hp, ":%.*s", (int)(dst - save_dst), save_dst);
}
}
} else {
*dst++ = *src++;
}
}
*dst++ = '.';
*dst = 0;
if (history != NULL) {
if (compound_message != NULL) {
char* new_msg = new char[strlen(compound_message)
+strlen(his_buf)+2];
sprintf(new_msg, "%s;%s", compound_message, his_buf);
last = last->next = new message_node(msg_buf);
delete[] compound_message;
compound_message = new_msg;
} else {
if (code == msg_loop || code == msg_sync_loop
|| code == msg_wait)
{
compound_message = strdup(his_buf);
first = last = new message_node(msg_buf);
if (code != msg_wait) {
loop_id = (int)(long)parameter[2];
}
} else if (!message_node::find(his_buf)) {
fprintf(stdout, "%s\n", msg_buf);
if (code != msg_done) {
fprintf(history, "%s\n", his_buf);
n_messages += 1;
}
}
}
} else {
fprintf(stdout, "%s\n", msg_buf);
if (code != msg_done) {
n_messages += 1;
}
}
}
}
graph_vertex* graph_vertex::graph;
int graph_vertex::n_vertexes;
//
// Methods of class_desc class
//
class_desc* class_desc::hash_table[class_hash_table_size];
class_desc* class_desc::chain;
int class_desc::n_classes;
void set_class_source_path(class_desc* cls)
{
if (source_file_path_len != 0) {
const char* class_file_name = cls->source_file.as_asciz();
if (!source_path_redefined) {
const char* dirend = strrchr(class_file_name, '/');
if (dirend != NULL) {
int dirlen = dirend - class_file_name;
if (dirlen <= source_file_path_len
&& memcmp(class_file_name,
source_file_path + source_file_path_len - dirlen,
dirlen) == 0)
{
class_file_name = dirend+1;
}
}
}
char file_name_buf[MAX_MSG_LENGTH];
int len = sprintf(file_name_buf, "%.*s%c%s",
source_file_path_len, source_file_path,
FILE_SEP, class_file_name);
cls->source_file = utf_string(len, (byte*)file_name_buf);
}
}
bool parse_class_file(byte* fp)
{
int i;
unsigned magic = unpack4(fp);
fp += 4;
if (magic != 0xCAFEBABE) return false;
int minor_version = unpack2(fp);
fp += 2;
int major_version = unpack2(fp);
fp += 2;
int constant_pool_count = unpack2(fp);
fp += 2;
constant** constant_pool = new constant*[constant_pool_count];
memset(constant_pool, 0, sizeof(constant*)*constant_pool_count);
int name_index1 = 0;
for (i = 1; i < constant_pool_count; i++) {
constant* cp = NULL;
int n_extra_cells = 0;
switch (*fp) {
case c_utf8:
cp = new const_utf8(fp);
if (!strcmp(((const_utf8*)cp)->as_asciz(), "this")) {
name_index1 = i;
}
break;
case c_integer:
cp = new const_int(fp);
break;
case c_float:
cp = new const_float(fp);
break;
case c_long:
cp = new const_long(fp);
n_extra_cells += 1;
break;
case c_double:
cp = new const_double(fp);
n_extra_cells += 1;
break;
case c_class:
cp = new const_class(fp);
break;
case c_string:
cp = new const_string(fp);
break;
case c_field_ref:
case c_method_ref:
case c_interface_method_ref:
cp = new const_ref(fp);
break;
case c_name_and_type:
cp = new const_name_and_type(fp);
break;
}
assert(cp != NULL);
fp += cp->length();
constant_pool[i] = cp;
i += n_extra_cells;
}
int access_flags1 = unpack2(fp);
fp += 2;
int this_class_name = unpack2(fp);
fp += 2;
int super_class_name = unpack2(fp);
fp += 2;
int interfaces_count = unpack2(fp);
fp += 2;
class_desc* this_class =
class_desc::get(*(const_utf8*)constant_pool
[((const_class*)constant_pool[this_class_name])->name]);
set_class_source_path(this_class);
// init. is_this
field_desc* is_this = new field_desc(utf_string("<this>"), this_class, NULL);
// assign name_and_type for is_this - find name entry "this" and obj. type
is_this->name_and_type =
new const_name_and_type(name_index1,
((const_class*)constant_pool[this_class_name])->name);
is_this->equals = is_this;
is_this->cls = this_class;
// init. is_const
// TODO: shadow variable
field_desc* is_const = new field_desc(utf_string("<const>"), NULL, NULL);
this_class->attr = access_flags1;
if (super_class_name == 0) { // Object class
assert(interfaces_count == 0);
this_class->n_bases = 0;
} else {
this_class->n_bases = interfaces_count + 1;
this_class->bases = new class_desc*[interfaces_count + 1];
this_class->bases[0] =
class_desc::get(*(const_utf8*)constant_pool
[((const_class*)constant_pool[super_class_name])->name]);
for (i = 1; i <= interfaces_count; i++) {
int interface_name = unpack2(fp);
fp += 2;
this_class->bases[i] =
class_desc::get(*(const_utf8*)constant_pool
[((const_class*)constant_pool[interface_name])->name]);
}
}
int fields_count = unpack2(fp);
fp += 2;
while (--fields_count >= 0) {
int myaccess_flags = unpack2(fp); fp += 2;
int myname_index = unpack2(fp); fp += 2;
int desc_index = unpack2(fp); fp += 2;
int attr_count = unpack2(fp); fp += 2;
field_desc* field =
this_class->get_field(*(const_utf8*)constant_pool[myname_index]);
field->attr |= myaccess_flags;
while (--attr_count >= 0) {
int attr_len = unpack4(fp+2);
fp += 6 + attr_len;
}
}
int methods_count = unpack2(fp);
fp += 2;
//
// We need "SourceFile" attribute first, so remember position and
// skip methods definitions
//
byte* method_part = fp;
for (i = 0; i < methods_count; i++) {
int attr_count = unpack2(fp+6);
fp += 8;
while (--attr_count >= 0) {
int attr_len = unpack4(fp+2);
fp += 6 + attr_len;
}
}
int attr_count1 = unpack2(fp); fp += 2;
while (--attr_count1 >= 0) {
int attr_name = unpack2(fp); fp += 2;
int attr_len = unpack4(fp); fp += 4;
utf_string attr(*(const_utf8*)constant_pool[attr_name]);
if (attr == "SourceFile") {
int source_name = unpack2(fp); fp += 2;
int file_name_idx = this_class->source_file.rindex(FILE_SEP);
utf_string* file_name = (const_utf8*)constant_pool[source_name];
if (file_name_idx >= 0) {
this_class->source_file.append(file_name_idx+1, *file_name);
} else {
this_class->source_file = *file_name;
}
} else {
fp += attr_len;
}
}
fp = method_part;
while (--methods_count >= 0) {
int access_flags = unpack2(fp); fp += 2;
int name_index = unpack2(fp); fp += 2;
int desc_index = unpack2(fp); fp += 2;
int attr_count = unpack2(fp); fp += 2;
const_utf8* mth_name = (const_utf8*)constant_pool[name_index];
const_utf8* mth_desc = (const_utf8*)constant_pool[desc_index];
method_desc* method = this_class->get_method(*mth_name, *mth_desc);
method->attr |= access_flags;
method->vars = NULL;
while (--attr_count >= 0) {
int attr_name = unpack2(fp); fp += 2;
int attr_len = unpack4(fp); fp += 4;
utf_string attr1(*(const_utf8*)constant_pool[attr_name]);
if (attr1 == "Code") {
int max_stack = unpack2(fp); fp += 2;
int max_locals = unpack2(fp); fp += 2;
int code_length = unpack4(fp); fp += 4;
method->code = fp;
method->code_length = code_length;
fp += code_length;
method->n_vars = max_locals;
method->vars = max_locals > 0 ? new var_desc[max_locals] : NULL;
method->line_table = new word[code_length];
memset(method->line_table, 0, sizeof(word)*code_length);
method->context = new local_context*[code_length+1];
memset(method->context, 0,
sizeof(local_context*)*(code_length+1));
int exception_table_length = unpack2(fp); fp += 2;
/* add new entry for each distinct "byte code adress of handle".
**
** if an exception handler at byte code "pos" handles exception of
** more than one byte code range, call
** "new ctx_entry_point(&method->context[pos]);" only once! Because
** otherwise the stack gets out of control.
**
** in the following example there are two different handle addresses
** 16 and 25. and for each of them
** "new ctx_entry_point(&method->context[handler_pc]);" is called
** exactly once. Therefore the program calls :
** new ctx_entry_point(&method->context[16]);
** new ctx_entry_point(&method->context[25]);
**********************************************************************
** Example Exception Table: **
** ------------------------------------- **
** **
** byte code adress **
** from to of handle **
** 2 10 16 **
** 12 14 16 **
** 20 23 25 **
**********************************************************************
**
** it is expected that the byte code addresses of the handles are
** ordered. If this would not be the case, a simple comparison of
** handler_pc and old_handler_pc would not be sufficient!
*/
int old_handler_pc = -1;
while (--exception_table_length >= 0) {
int handler_pc = unpack2(fp+4);
if ( handler_pc != old_handler_pc) {
new ctx_entry_point(&method->context[handler_pc]);
}
fp += 8;
old_handler_pc = handler_pc;
}
int method_attr_count = unpack2(fp); fp += 2;
bool line_table_present = false;
bool local_var_table_present = false;
while (--method_attr_count >= 0) {
int mth_attr_name = unpack2(fp); fp += 2;
int mth_attr_len = unpack4(fp); fp += 4;
utf_string* attr =
(const_utf8*)constant_pool[mth_attr_name];
if (*attr == "LineNumberTable") {
int table_length = unpack2(fp); fp += 2;
while (--table_length >= 0) {
int start_pc = unpack2(fp); fp += 2;
int line_no = unpack2(fp); fp += 2;
method->line_table[start_pc] = line_no;
}
method->first_line = method->line_table[0];
if (method->first_line != 0) method->first_line -= 1;
line_table_present = true;
} else if (*attr == "LocalVariableTable") {
method->local_variable_table_present = true;
int table_length = unpack2(fp); fp += 2;
while (--table_length >= 0) {
int start_pc = unpack2(fp); fp += 2;
int length = unpack2(fp); fp += 2;
int name = unpack2(fp); fp += 2;
int desc = unpack2(fp); fp += 2;
int index = unpack2(fp); fp += 2;
int type = (index == 0 &&
!(access_flags & method_desc::m_static))
? tp_self
: get_type(*(const_utf8*)constant_pool[desc]);
new ctx_push_var(&method->context[start_pc],
(const_utf8*)constant_pool[name],
type, index, start_pc);
new ctx_pop_var(&method->context[start_pc+length],
index);
}
local_var_table_present = true;
} else {
fp += mth_attr_len;
}
}
if (verbose && !(access_flags & method_desc::m_native)) {
char buf[MAX_MSG_LENGTH];
method->demangle_method_name(buf);
if (!line_table_present) {
fprintf(stderr, "No line table present "
"for method %s\n", buf);
}
if (!local_var_table_present) {
fprintf(stderr, "No local variable table present "
"for method %s\n", buf);
}
}
method->parse_code(constant_pool, is_this);
} else {
fp += attr_len;
}
}
}
for (i = 1; i < constant_pool_count; i++) {
if (constant_pool[i] != NULL)
delete constant_pool[i];
}
/*
for (method_desc* m = this_class->methods; m != NULL; m = m->next) {
m->locksAtEntry.clear();
}
*/
delete[] constant_pool;
delete is_this->name_and_type;
//delete is_this;
delete is_const;
monitor_stack::const_iterator it;
for (it = this_class->usedLocks.begin();
it != this_class->usedLocks.end(); ++it) {
if (!((*it)->writes.empty())) {
vector<int>::const_iterator ii;
for (ii = (*it)->writes.begin(); ii != (*it)->writes.end(); ++ii) {
message_at(msg_lock_assign, this_class->source_file, *ii, *it);
// *i: line number; *it = field_desc*
}
}
}
return true;
}
//
// Determine type of file and extract Java class description from the file
//
inline int stricmp(const char* p, const char* q)
{
do {
while (*p == '_') p += 1; // ignore '_'
while (*q == '_') q += 1; // ignore '_'
int diff = toupper(*(byte*)p) - toupper(*(byte*)q);
if (diff != 0) {
return diff;
}
q += 1;
} while (*p++ != 0);
return 0;
}
void proceed_file(char* file_name, bool recursive = false)
{
#ifdef _WIN32
HANDLE dir;
char dir_path[MAX_PATH];
WIN32_FIND_DATA file_data;
if (recursive != 0) {
sprintf(dir_path, "%s\\*", file_name);
if ((dir=FindFirstFile(dir_path, &file_data)) != INVALID_HANDLE_VALUE)
{
file_name = dir_path;
}
} else {
if (strcmp(file_name, "..") == 0 || strcmp(file_name, ".") == 0) {
proceed_file(file_name, 1);
return;
}
if ((dir=FindFirstFile(file_name, &file_data)) == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Failed to open file '%s'\n", file_name);
return;
}
}
if (dir != INVALID_HANDLE_VALUE) {
do {
if (!recursive || *file_data.cFileName != '.') {
char file_path[MAX_PATH];
char* file_dir = strrchr(file_name, '\\');
char* file_name_with_path;
if (file_dir != NULL) {
int dir_len = file_dir - file_name + 1;
memcpy(file_path, file_name, dir_len);
strcpy(file_path+dir_len, file_data.cFileName);
file_name_with_path = file_path;
} else {
file_name_with_path = file_data.cFileName;
}
proceed_file(file_name_with_path, recursive+1);
}
} while (FindNextFile(dir, &file_data));
FindClose(dir);
return;
}
#else
DIR* dir = opendir(file_name);
if (dir != NULL) {
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
if (*entry->d_name != '.') {
char full_name[MAX_PATH];
sprintf(full_name, "%s/%s", file_name, entry->d_name);
proceed_file(full_name, 2);
}
}
closedir(dir);
return;
}
#endif
// TODO: recursive vs bool comparison (true or false)
if (recursive >= 2) {
int len = strlen(file_name);
if (len < 6 || stricmp(file_name + len - 6, ".class") != 0) {
return;
}
}
FILE* f = fopen(file_name, "rb");
if (f == NULL) {
fprintf(stderr, "Failed to open file '%s'\n", file_name);
return;
}
char* suf = file_name + strlen(file_name) - 4;
if (suf > file_name && (stricmp(suf,".zip")==0 || stricmp(suf,".jar")==0)){
// extract files from ZIP archive
byte hdr[ECREC_SIZE+4];
if (fseek(f, 0, SEEK_END) != 0
|| ftell(f) < ECREC_SIZE+4
|| fseek(f, -(ECREC_SIZE+4), SEEK_CUR) != 0
|| fread(hdr, ECREC_SIZE+4, 1, f) != 1)
{
fprintf(stderr, "Bad format of ZIP file '%s'\n", file_name);
fclose(f);
return;
}
int count = unpack2_le(&hdr[TOTAL_ENTRIES_CENTRAL_DIR]);
int dir_size = unpack4_le(&hdr[SIZE_CENTRAL_DIRECTORY]);
byte* directory = new byte[dir_size];
if (fseek(f, -(dir_size+ECREC_SIZE+4), SEEK_CUR) != 0
|| fread(directory, dir_size, 1, f) != 1)
{
fprintf(stderr, "Bad format of ZIP file '%s'\n", file_name);
delete[] directory;
fclose(f);
return;
}
byte* dp = directory;
while (--count >= 0) {
int compression_method = unpack2_le(&dp[4+C_COMPRESSION_METHOD]);
int compressed_size = unpack4_le(&dp[4+C_COMPRESSED_SIZE]);
int uncompressed_size = unpack4_le(&dp[4+C_UNCOMPRESSED_SIZE]);
int filename_length = unpack2_le(&dp[4+C_FILENAME_LENGTH]);
int cextra_length = unpack2_le(&dp[4+C_EXTRA_FIELD_LENGTH]);
if ((dp-directory)+filename_length+CREC_SIZE+4 > dir_size) {
fprintf(stderr, "Bad format of ZIP file '%s'\n", file_name);
break;
}
byte local_rec_buffer[LREC_SIZE+4];
int local_header_offset =
unpack4_le(&dp[4+C_RELATIVE_OFFSET_LOCAL_HEADER]);
if (fseek(f, local_header_offset, SEEK_SET) != 0
|| fread(local_rec_buffer, sizeof local_rec_buffer, 1, f) != 1
|| memcmp(&local_rec_buffer[1], LOCAL_HDR_SIG, 3) != 0)
{
fprintf(stderr, "Bad format of ZIP file '%s'\n", file_name);
break;
}
int file_start = local_header_offset + (LREC_SIZE+4) +
unpack2_le(&local_rec_buffer[4+L_FILENAME_LENGTH]) +
unpack2_le(&local_rec_buffer[4+L_EXTRA_FIELD_LENGTH]);
int filename_offset = CREC_SIZE+4;
if (compression_method == C_UNCOMPRESSED)
{
if (uncompressed_size != 0) {
byte* buffer = new byte[uncompressed_size];
if (fseek(f, file_start, SEEK_SET) != 0
|| fread(buffer, uncompressed_size, 1, f) != 1)
{
fprintf(stderr, "Failed to read ZIP file '%s'\n",
file_name);
delete[] buffer;
fclose(f);
return;
} else {
if (verbose) {
fprintf(stderr, "Verify file '%.*s'\n",
filename_length, dp + filename_offset);
}
if (!parse_class_file(buffer)) {
fprintf(stderr, "File '%.*s' isn't correct Java class "
"file\n", filename_length, dp+filename_offset);
}
}
delete[] buffer;
}
}
else if (compression_method == C_DEFLATE) {
if ((uncompressed_size != 0) && (compressed_size != 0)) {
byte* uncbuffer = new byte[uncompressed_size];
byte* cbuffer = new byte[compressed_size];
if (fseek(f, file_start, SEEK_SET) != 0
|| fread(cbuffer, compressed_size, 1, f) != 1)
{
fprintf(stderr, "Failed to read ZIP file '%s'\n",
file_name);
fclose(f);
delete[] uncbuffer;
delete[] cbuffer;
return;
} else {
z_stream c_stream;
c_stream.zalloc = (alloc_func) 0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
int err = inflateInit2(&c_stream, -MAX_WBITS);
if (err != Z_OK)
{
fprintf(stderr, "Error %s in inflateInit while uncompressing %.*s.\n",
c_stream.msg, filename_length, dp+filename_offset);
}
else
{
c_stream.next_in = cbuffer;
c_stream.next_out = uncbuffer;
c_stream.avail_in = compressed_size;
c_stream.avail_out = uncompressed_size;
c_stream.total_in = 0;
c_stream.total_out = 0;
while ((err=inflate(&c_stream, Z_SYNC_FLUSH)) == Z_OK);
if ((err != Z_STREAM_END) && (err != Z_OK))
{
fprintf(stderr, "Error %s in inflate while uncompressing %.*s.\n",
c_stream.msg, filename_length, dp+filename_offset);
}
else
{
inflateEnd(&c_stream);
if (verbose) {
fprintf(stderr, "Verify file '%.*s'\n",
filename_length, dp + filename_offset);
}
if (!parse_class_file(uncbuffer)) {
fprintf(stderr, "File '%.*s' isn't correct Java class "
"file\n", filename_length, dp+filename_offset);
}
}
}
}
delete[] cbuffer;
delete[] uncbuffer;
}
}
dp += filename_offset + filename_length + cextra_length;
}
delete[] directory;
} else {
fseek(f, 0, SEEK_END);
size_t file_size = ftell(f);
fseek(f, 0, SEEK_SET);
byte* buffer = new byte[file_size];
if (fread(buffer, file_size, 1, f) != 1) {
fprintf(stderr, "Failed to read file '%s'\n", file_name);
} else {
if (verbose) {
fprintf(stderr, "Verify file '%s'\n", file_name);
}
if (!parse_class_file(buffer)) {
fprintf(stderr, "File '%s' isn't correct Java class file\n",
file_name);
}
}
delete[] buffer;
}
fclose(f);
}
//
// Main function: parse command line
//
msg_select_category_option msg_category_option[] = {
{cat_synchronization, "synchronization",
"detect synchronizational problems"},
{cat_deadlock, "deadlock",
"possible deadlock detection"},
{cat_race_condition, "race_condition",
"possible race condition detection"},
{cat_wait_nosync, "wait_nosync",
"wait or notify is called from non-synchronized method"},
{cat_inheritance, "inheritance",
"detect problems with class/interface inheritance"},
{cat_super_finalize, "super_finalize",
"super.finalize() is not called from finalize method"},
{cat_not_overridden, "not_overridden",
"methods with the same names and different profiles"},
{cat_field_redefined, "field_redefined",
"instance or static variable is redefined in derived class"},
{cat_shadow_local, "shadow_local",
"local variable shadows one in exterior scope"},
{cat_data_flow, "data_flow",
"perform data flow analysis to detect suspicious operations"},
{cat_null_reference, "null_reference",
"detect access to variables with possible NULL value"},
{cat_zero_operand, "zero_operand",
"one of the operands of a binary integer operation is zero"},
{cat_zero_result, "zero_result",
"result of operation is always zero"},
{cat_redundant, "redundant",
"operation always produces the same result"},
{cat_overflow, "overflow",
"detect incorrect handling of operation overflow"},
{cat_incomp_case, "incomp_case",
"switch case label can't be produced by switch expression"},
{cat_short_char_cmp, "short_char_cmp",
"expression of char type is compared with one of short type"},
{cat_string_cmp, "string_cmp",
"two strings are compared as object references"},
{cat_weak_cmp, "weak_cmp",
"using of inequality comparison where equality can be checked"},
{cat_domain, "domain",
"operands doesn't belong to the domain of operator"},
{cat_truncation, "truncation",
"data can be lost as a result of type conversion"},
{cat_bounds, "bounds",
"array index or length is out of range"},
{cat_done, "done",
"notification about verification completion"},
{cat_all, "all",
"produce messages of all categories"}
};
void usage()
{
fprintf(stderr, "\
Usage: jlint {option|java_class_file}\n\
Options:\n\
-help : print this text\n\
-source <source_path> : path to directory with .java files\n\
-history <file_name> : use history file to avoid repeated messages\n\
-max_shown_paths <number> : max. shown paths between two sync. methods\n\
(+|-)verbose : output more/less information about program activity\n\
(+|-)message_category : select categories of messages to be reported\n\
(+|-)message_code : enable/disable concrete message\n\
Message categories:\n");
message_category group = msg_category_option[0].msg_cat;
for (unsigned i = 0; i < items(msg_category_option); i++) {
message_category msg_cat = msg_category_option[i].msg_cat;
if ((msg_cat & ~group) != 0) {
group = msg_cat;
fprintf(stderr, "\n");
}
fprintf(stderr, " %s : %s\n",
msg_category_option[i].cat_name,
msg_category_option[i].cat_desc);
}
if (verbose) {
fprintf(stderr, "\nMessages: (category:code: \"text\")\n");
for (int i = 0; i < msg_last; i++) {
for (unsigned j = 0; j < items(msg_category_option); j++) {
if (msg_table[i].category == msg_category_option[j].msg_cat) {
fprintf(stderr, " %s:%s: \"%s\"\n",
msg_category_option[j].cat_name,
msg_table[i].name, msg_table[i].format);
break;
}
}