forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
1108 lines (903 loc) · 27.4 KB
/
tests.cpp
File metadata and controls
1108 lines (903 loc) · 27.4 KB
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
//semmle-extractor-options: --edg --target --edg linux_x86_64
// Semmle test cases for rule CWE-119.
// library types, functions etc
typedef unsigned long size_t;
void *malloc(size_t size);
void free(void *ptr);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memset(void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
int printf(const char *format, ...);
size_t strlen(const char *s);
char *strncpy(char *s1, const char *s2, size_t n);
void test1()
{
char smallbuffer[10];
char bigbuffer[20];
memcpy(bigbuffer, smallbuffer, sizeof(smallbuffer)); // GOOD
memcpy(bigbuffer, smallbuffer, sizeof(bigbuffer)); // BAD: over-read
memcpy(smallbuffer, bigbuffer, sizeof(smallbuffer)); // GOOD
memcpy(smallbuffer, bigbuffer, sizeof(bigbuffer)); // BAD: over-write
}
void test2()
{
char *smallbuffer = (char *)malloc(sizeof(char) * 10);
char *bigbuffer = (char *)malloc(sizeof(char) * 20);
memcpy(bigbuffer, smallbuffer, 10); // GOOD
memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read
memcpy(smallbuffer, bigbuffer, 10); // GOOD
memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write
free(bigbuffer);
free(smallbuffer);
}
void test3()
{
char *smallbuffer, *bigbuffer;
smallbuffer = new char[10];
bigbuffer = new char[20];
memcpy(bigbuffer, smallbuffer, 10); // GOOD
memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read
memcpy(smallbuffer, bigbuffer, 10); // GOOD
memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write
delete [] bigbuffer;
delete [] smallbuffer;
}
void test4(int unbounded)
{
int bounded = 100;
char buffer1[100], buffer2[100];
memmove(buffer1, buffer2, bounded); // GOOD
memmove(buffer1, buffer2, unbounded); // BAD: may over-write [NOT DETECTED]
}
void test5(int unbounded)
{
int a, b, c, d, e;
char buffer[100];
buffer[unbounded] = 'x'; // BAD: may under- or over-write [NOT DETECTED]
buffer[0] = buffer[unbounded]; // BAD: may under- or over-read [NOT DETECTED]
a = unbounded;
buffer[a] = 'x'; // BAD: may under- or over-write [NOT DETECTED]
buffer[0] = buffer[a]; // BAD: may under- or over-read [NOT DETECTED]
b = unbounded;
if (b < 0) {b = 0;}
buffer[b] = 'x'; // BAD: may over-write [NOT DETECTED]
buffer[0] = buffer[b]; // BAD: may over-read [NOT DETECTED]
c = unbounded;
if (c > 99) {c = 99;}
buffer[c] = 'x'; // BAD: may under-write [NOT DETECTED]
buffer[0] = buffer[c]; // BAD: may under-read [NOT DETECTED]
d = unbounded;
if (d < 0) {d = 0;}
if (d > 99) {d = 99;}
buffer[d] = 'x'; // GOOD
buffer[0] = buffer[d]; // GOOD
e = unbounded;
e = 50;
buffer[e] = 'x'; // GOOD
buffer[0] = buffer[e]; // GOOD
}
void test6(bool cond)
{
int a, b, c, d, e, f, g, h, i, j, k;
char buffer[100];
char ch;
a = -1;
buffer[a] = 'x'; // BAD: under-write [NOT DETECTED]
ch = buffer[a]; // BAD: under-read [NOT DETECTED]
b = 0;
buffer[b] = 'x'; // GOOD
ch = buffer[b]; // GOOD
c = 100;
buffer[c] = 'x'; // BAD: over-write [NOT DETECTED]
ch = buffer[c]; // BAD: over-read [NOT DETECTED]
d = 0;
d = 1000;
buffer[d] = 'x'; // BAD: over-write [NOT DETECTED]
ch = buffer[d]; // BAD: over-read [NOT DETECTED]
e = 1000;
e = 0;
buffer[e] = 'x'; // GOOD
ch = buffer[e]; // GOOD
f = 0;
if (cond) {f = 1000;}
buffer[f] = 'x'; // BAD: may over-write [NOT DETECTED]
ch = buffer[f]; // BAD: may over-read [NOT DETECTED]
g = 1000;
if (cond) {g = 0;}
buffer[g] = 'x'; // BAD: may over-write [NOT DETECTED]
ch = buffer[g]; // BAD: may over-read [NOT DETECTED]
h = 1000;
if (cond)
{
h = 10;
} else {
h = 20;
}
buffer[h] = 'x'; // GOOD
ch = buffer[h]; // GOOD
for (i = 0; i < 100; i++)
{
buffer[i] = 'x'; // GOOD
ch = buffer[i]; // GOOD
}
for (j = -1; j < 100; j++)
{
buffer[j] = 'x'; // BAD: under-write [NOT DETECTED]
ch = buffer[j]; // BAD: under-read [NOT DETECTED]
}
for (k = 0; k <= 100; k++)
{
buffer[k] = 'x'; // BAD: over-write
ch = buffer[k]; // BAD: over-read
}
}
void test7()
{
char *names[] = {"tom", "dick", "harry"};
printf("name: %s\n", names[-1]); // BAD: under-read
printf("name: %s\n", names[0]); // GOOD
printf("name: %s\n", names[1]); // GOOD
printf("name: %s\n", names[2]); // GOOD
printf("name: %s\n", names[3]); // BAD: over-read
}
void test8(int unbounded)
{
char buffer[128];
int v1 = 128;
int v2 = 256;
int i;
for (i = 0; i < v1; i++)
{
buffer[i] = 0; // GOOD
}
for (i = 0; i < v2; i++)
{
buffer[i] = 0; // BAD: over-write [NOT DETECTED]
}
for (i = 0; i < unbounded; i++)
{
buffer[i] = 0; // BAD: may over-write [NOT DETECTED]
}
unbounded = 128;
for (i = 0; i < unbounded; i++)
{
buffer[i] = 0; // GOOD
}
}
char global_array_5[] = {1, 2, 3, 4, 5};
char global_array_6[] = {1, 2, 3, 4, 5, 6};
void test9(int param)
{
{
char buffer1[32];
char *buffer2 = new char[32];
void *buffer3;
void *buffer4;
buffer3 = malloc(32);
buffer4 = buffer3;
memset(buffer1, 0, 32); // GOOD
memset(buffer1, 0, 33); // BAD: overrun write of buffer1
memset(buffer2, 0, 32); // GOOD
memset(buffer2, 0, 33); // BAD: overrun write of buffer2
memset(buffer3, 0, 32); // GOOD
memset(buffer3, 0, 33); // BAD: overrun write of buffer3
memset(buffer4, 0, 32); // GOOD
memset(buffer4, 0, 33); // BAD: overrun write of buffer4 (buffer3)
memcmp(buffer1, buffer2, 32); // GOOD
memcmp(buffer1, buffer2, 33); // BAD: overrun read of buffer1, buffer2
}
{
char *str1 = "1234567";
char *str2 = "abcdefgh";
strncpy(str1, str2, strlen(str1) + 1); // GOOD
strncpy(str1, str2, strlen(str2) + 1); // BAD: overrun write of str1
strncpy(str2, str1, strlen(str1) + 1); // DUBIOUS (detected)
strncpy(str2, str1, strlen(str2) + 1); // BAD: overrun read of str1 [NOT REPORTED]
}
memmove(global_array_6, global_array_5, 6); // BAD: overrun read of global_array_5
memmove(global_array_5, global_array_6, 6); // BAD: overrun write of global_array_5
if (param > 0)
{
void *buffer = malloc(param);
memset(buffer, 0xFF, param); // GOOD
memset(buffer, 0xFF, param * 2); // BAD: overrun write of buffer [NOT REPORTED]
}
}
wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n);
void test10()
{
wchar_t buffer1[32];
char buffer2[32];
wmemset(buffer1, 0, 32); // GOOD
wmemset(buffer1, 0, 33); // BAD: overrun write of buffer1
wmemset((wchar_t *)buffer2, 0, 32); // BAD: overrun write of buffer2
}
void test11()
{
{
char *string = "Hello, world!";
memset(string, 0, 14); // GOOD
memset(string, 0, 15); // BAD: overrun write of string
}
{
char *buffer = new char[128];
memset(buffer, 0, 128);
buffer = new char[64];
memset(buffer, 0, 128); // BAD: overrun write of buffer
}
{
char array[10] = "123";
memset(array, 0, 10); // GOOD
memset(array, 0, 11); // BAD: overrun write of array
}
}
struct myStruct
{
char buffer[16];
int field;
};
myStruct myVar;
void test12()
{
char buf[16];
char *dbuf;
dbuf = new char[16];
memset(&myVar, 0, sizeof(myVar)); // GOOD
memset(&myVar, 0, sizeof(myVar) + 1); // BAD: overrun write of myVar
memset(myVar.buffer, 0, 16); // GOOD
memset(myVar.buffer, 0, 17); // DUBIOUS: overrun write of myVar.buffer, but not out of myVar itself [NOT DETECTED]
memset(&(myVar.field), 0, sizeof(int)); // GOOD
memset(&(myVar.field), 0, sizeof(int) * 2); // BAD: overrun write of myVar.field
memset(buf + 8, 0, 8); // GOOD
memset(buf + 8, 0, 9); // BAD: overrun write of buf [NOT DETECTED]
memset(dbuf + 8, 0, 8); // GOOD
memset(dbuf + 8, 0, 9); // BAD: overrun write of dbuf [NOT DETECTED]
{
myStruct *myPtr1 = &myVar;
myStruct *myPtr2;
myPtr2 = myPtr1;
memset(myPtr1, 0, sizeof(myStruct)); // GOOD
memset(myPtr1, 0, sizeof(myStruct) + 1); // BAD: overrun write of myVar
memset(myPtr2, 0, sizeof(myStruct)); // GOOD
memset(myPtr2, 0, sizeof(myStruct) + 1); // BAD: overrun write of myVar
}
{
void *myPtr3 = (void *)(&myVar);
memset(myPtr3, 0, sizeof(myStruct)); // GOOD
memset(myPtr3, 0, sizeof(myStruct) + 1); // BAD: overrun write of myVar
}
}
void test13(char *argArray)
{
char charArray[10];
int intArray[10];
myStruct structArray[10];
char *ptrArray = charArray;
char *ptrArrayOffset = charArray + 1;
charArray[-1] = 1; // BAD: underrun write
charArray[0] = 1; // GOOD
charArray[9] = 1; // GOOD
charArray[10] = 1; // BAD: overrun write
charArray[5] = charArray[10]; // BAD: overrun read
intArray[-1] = 1; // BAD: underrun write
intArray[0] = 1; // GOOD
intArray[9] = 1; // GOOD
intArray[10] = 1; // BAD: overrun write
intArray[5] = intArray[10]; // BAD: overrun read
structArray[-1].field = 1; // BAD: underrun write
structArray[0].field = 1; // GOOD
structArray[9].field = 1; // GOOD
structArray[10].field = 1; // BAD: overrun write
structArray[5].field = structArray[10].field; // BAD: overrun read
charArray[9] = (char)intArray[9]; // GOOD
charArray[9] = (char)intArray[10]; // BAD: overrun read
ptrArray[-2] = 1; // BAD: underrun write
ptrArray[-1] = 1; // BAD: underrun write
ptrArray[0] = 1; // GOOD
ptrArray[8] = 1; // GOOD
ptrArray[9] = 1; // GOOD
ptrArray[10] = 1; // BAD: overrun write
ptrArrayOffset[-2] = 1; // BAD: underrun write [NOT DETECTED]
ptrArrayOffset[-1] = 1; // GOOD (there is room for this)
ptrArrayOffset[0] = 1; // GOOD
ptrArrayOffset[8] = 1; // GOOD
ptrArrayOffset[9] = 1; // BAD: overrun write [NOT DETECTED]
ptrArrayOffset[10] = 1; // BAD: overrun write [NOT DETECTED]
argArray[-1] = 1; // BAD: underrun write [NOT DETECTED]
argArray[0] = 1; // GOOD
argArray[1] = 1; // GOOD (we can't tell the length of this array)
argArray[999] = 1; // GOOD (we can't tell the length of this array)
{
unsigned short *buffer1 = (unsigned short *)malloc(sizeof(short) * 50);
unsigned short *buffer2 = (unsigned short *)malloc(101); // 50.5 shorts
buffer1[0] = 0xFFFF;
buffer1[49] = 0xFFFF;
buffer1[50] = 0xFFFF; // BAD: overrun write
buffer2[0] = 0xFFFF;
buffer2[49] = 0xFFFF;
buffer2[50] = 0xFFFF; // BAD: overrun write
}
}
int strncmp(const char *s1, const char *s2, size_t n);
const char *get_buffer();
void get_line(const char **ptr);
void test14()
{
const char *buf;
buf = get_buffer();
if (buf != 0 && memcmp(buf, "mydata", 6) == 0) // GOOD
{
// ...
}
}
void test15()
{
const char *ptr = get_buffer();
int i;
for (i = 0; ; i++)
{
get_line(&ptr);
if (strncmp(ptr, "token", 5) == 0)
{
if (ptr[5] == ' ') // GOOD
{
break;
}
}
}
}
typedef struct mystruct16
{
int data;
struct mystruct16 *next;
};
void test16()
{
int arr1[10];
int *ptr1_1 = &arr1[9]; // GOOD
int *ptr1_2 = &arr1[10]; // GOOD: valid as long as we don't dereference it
int *ptr1_3 = &arr1[11]; // BAD: not guaranteed to be valid [NOT DETECTED - considered outside the scope of CWE-119]
int x = *--ptr1_2; // GOOD
mystruct16 arr2[10];
mystruct16 *ptr2_1 = &arr2[9]; // GOOD
mystruct16 *ptr2_2 = &arr2[10]; // GOOD: valid as long as we don't dereference it
mystruct16 *ptr2_3 = &arr2[11]; // BAD: not guaranteed to be valid [NOT DETECTED - considered outside the scope of CWE-119]
(--ptr2_2)->data = 1; // GOOD
}
void test17(long long *longArray)
{
longArray[-1] = -1; // BAD: underrun write [NOT DETECTED]
{
int intArray[5];
((char *)intArray)[-3] = 0; // BAD: underrun write
}
{
int multi[10][10];
multi[5][5] = 0; // GOOD
multi[-5][5] = 0; // BAD: underrun write [INCORRECT MESSAGE]
multi[5][-5] = 0; // DUBIOUS: underrun write (this one is still within the bounds of the whole array)
multi[-5][-5] = 0; // BAD: underrun write [INCORRECT MESSAGE]
multi[0][-5] = 0; // BAD: underrun write [NOT DETECTED]
multi[15][5] = 0; // BAD: overrun write
multi[5][15] = 0; // DUBIOUS: overrun write (this one is still within the bounds of the whole array)
multi[15][15] = 0; // BAD: overrun write
}
}
char *update(char *ptr);
void test18()
{
char buffer[128];
char *p1 = buffer;
char *p2 = buffer;
char *p3 = buffer;
char *p4 = (char *)malloc(128);
char *p5 = (char *)malloc(128);
p1[-1] = 0; // BAD: underrun write
p2[-1] = 0; // BAD: underrun write
p2++;
p2[-1] = 0; // GOOD
p3[-1] = 0; // BAD
while (*p3 != 0) {
p3 = update(p3);
}
p3[-1] = 0; // GOOD
p4[-1] = 0; // BAD: underrun write
p4++;
p4[-1] = 0; // GOOD
p5[-1] = 0; // BAD
while (*p5 != 0) {
p5 = update(p5);
}
p5[-1] = 0; // GOOD
}
void test19(bool b)
{
char *p1, *p2, *p3;
p1 = (char *)malloc(10);
p2 = (char *)malloc(10);
p3 = (char *)malloc(20);
// ...
if (b)
{
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);
p3 = (char *)malloc(20);
}
// ...
if (b)
{
memset(p1, 0, 20); // BAD
memset(p2, 0, 20); // GOOD
memset(p3, 0, 20); // GOOD
}
}
typedef struct {} FILE;
FILE *fileSource;
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
void test20()
{
char charBuffer[100];
int intBuffer[100];
int num;
if (fread(charBuffer, sizeof(char), 100, fileSource) > 0) // GOOD
{
// ...
}
if (fread(charBuffer, sizeof(char), 101, fileSource) > 0) // BAD
{
// ...
}
if (fread(charBuffer, sizeof(int), 100, fileSource) > 0) // BAD
{
// ...
}
if (fread(intBuffer, sizeof(int), 100, fileSource) > 0) // GOOD
{
// ...
}
num = 101;
if (fread(intBuffer, sizeof(int), num, fileSource) > 0) // BAD [NOT DETECTED]
{
// ...
}
}
void test21(bool cond)
{
char buffer[100];
char *ptr;
int i;
if (buffer[-1] == 0) { return; } // BAD: accesses buffer[-1]
ptr = buffer;
if (cond)
{
ptr++;
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[0]
} else {
if (ptr[-1] == 0) { return; } // BAD: accesses buffer[-1]
}
if (ptr[-1] == 0) { return; } // BAD: accesses buffer[-1] or buffer[0] [NOT DETECTED]
ptr = buffer;
for (i = 0; i < 2; i++)
{
ptr++;
}
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[1]
}
void test22(bool b, const char* source) {
char buffer[16];
int k;
for (k = 0; k <= 100; k++) {
if(k < 16) {
buffer[k] = 'x'; // GOOD
}
}
char dest[128];
int n = b ? 1024 : 132;
if (n >= 128) {
return;
}
memcpy(dest, source, n); // GOOD
}
int test23() {
char buffer[100];
return sizeof(buffer) / sizeof(buffer[101]); // GOOD
}
char* strcpy(char *, const char *);
void test24(char* source) {
char buffer[100];
strcpy(buffer, source); // BAD
}
struct my_struct {
char* home;
};
void test25(char* source) {
my_struct s;
s.home = source;
char buf[100];
strcpy(buf, s.home); // BAD
}
void test26(bool cond)
{
char buffer[100];
char *ptr;
int i;
if (buffer[-1] == 0) { return; } // BAD: accesses buffer[-1]
ptr = buffer;
if (cond)
{
ptr += 1;
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[0]
} else {
if (ptr[-1] == 0) { return; } // BAD: accesses buffer[-1]
}
if (ptr[-1] == 0) { return; } // BAD: accesses buffer[-1] or buffer[0] [NOT DETECTED]
ptr = buffer;
for (i = 0; i < 2; i++)
{
ptr += 1;
}
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[1]
}
#define IND 100
#define MAX_SIZE 100
void test27(){
char *src = "";
char *dest = "abcdefgh";
int ind = 100;
char buffer[MAX_SIZE];
strncpy(dest, src, 8); // GOOD, strncpy will not read past null terminator of source
if(IND < MAX_SIZE){
buffer[IND] = 0; // GOOD: out of bounds, but inaccessible code
}
}
typedef struct _MYSTRUCT {
unsigned long a;
unsigned short b;
unsigned char z[ 100 ];
} MYSTRUCT;
const MYSTRUCT _myStruct = { 0 };
typedef const MYSTRUCT& MYSTRUCTREF;
// False positive case due to use of typedefs
int test28(MYSTRUCTREF g)
{
return memcmp(&g, &_myStruct, sizeof(MYSTRUCT)); // GOOD
}
#define offsetof(s, m) __builtin_offsetof(s, m)
struct HasSomeFields {
unsigned long a;
unsigned long b;
unsigned long c;
void test29() {
memset(&a, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, a)); // GOOD
};
void test30() {
memset(&b, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, b)); // GOOD
};
void test31() {
memset(&c, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, c)); // GOOD
};
void test32() {
memset(&c, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, a)); // BAD
};
void test33() {
memset(&c, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, b)); // BAD
};
void test34() {
memset(&b, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, a)); // BAD
};
void test35() {
memset(&b, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, b) - sizeof(unsigned long)); // GOOD
};
};
void test36() {
HasSomeFields hsf;
memset(&hsf.a, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, a)); // GOOD
memset(&hsf.c, 0, sizeof(HasSomeFields) - offsetof(HasSomeFields, a)); // BAD
}
struct AnonUnionInStruct
{
union {
struct {
unsigned int a_1;
unsigned int b_1;
unsigned int c_1;
};
struct {
unsigned int a_2;
unsigned int b_2;
};
};
unsigned int d;
void test37() {
memset(&a_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_1)); // GOOD
memset(&a_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_1)); // GOOD
memset(&a_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, c_1)); // GOOD
memset(&a_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_2)); // GOOD
memset(&a_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_2)); // GOOD
memset(&a_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, d)); // GOOD
memset(&b_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_1)); // BAD
memset(&b_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_1)); // GOOD
memset(&b_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, c_1)); // GOOD
memset(&b_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_2)); // BAD
memset(&b_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_2)); // GOOD
memset(&b_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, d)); // GOOD
memset(&c_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_1)); // BAD
memset(&c_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_1)); // BAD
memset(&c_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, c_1)); // GOOD
memset(&c_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_2)); // BAD
memset(&c_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_2)); // GOOD
memset(&c_1, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, d)); // GOOD
memset(&a_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_1)); // GOOD
memset(&a_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_1)); // GOOD
memset(&a_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, c_1)); // GOOD
memset(&a_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_2)); // GOOD
memset(&a_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_2)); // GOOD
memset(&a_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, d)); // GOOD
memset(&b_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_1)); // BAD
memset(&b_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_1)); // GOOD
memset(&b_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, c_1)); // GOOD
memset(&b_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, a_2)); // BAD
memset(&b_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, b_2)); // GOOD
memset(&b_2, 0, sizeof(AnonUnionInStruct) - offsetof(AnonUnionInStruct, d)); // GOOD
};
};
struct UnionWithoutStruct
{
union
{
unsigned int a;
};
unsigned int b;
void test37() {
memset(&a, 0, sizeof(UnionWithoutStruct) - offsetof(UnionWithoutStruct, a)); // GOOD
memset(&a, 0, sizeof(UnionWithoutStruct) - offsetof(UnionWithoutStruct, b)); // GOOD
memset(&b, 0, sizeof(UnionWithoutStruct) - offsetof(UnionWithoutStruct, a)); // BAD
};
};
struct ThreeUInts {
unsigned int a;
unsigned int b;
unsigned int c;
};
struct FourUInts {
ThreeUInts inner;
unsigned int x;
};
struct S2 {
FourUInts f;
unsigned u;
void test38() {
memset(&f.inner.a, 0, sizeof(S2) - offsetof(ThreeUInts, a)); // GOOD
memset(&f.inner.a, 0, sizeof(S2) - offsetof(ThreeUInts, b)); // GOOD
memset(&f.inner.a, 0, sizeof(S2) - offsetof(ThreeUInts, c)); // GOOD
memset(&f.inner.a, 0, sizeof(S2) - offsetof(FourUInts, inner)); // GOOD
memset(&f.inner.a, 0, sizeof(S2) - offsetof(FourUInts, x)); // GOOD
memset(&f.inner.a, 0, sizeof(S2) - offsetof(S2, f)); // GOOD
memset(&f.inner.a, 0, sizeof(S2) - offsetof(S2, u)); // GOOD
memset(&f.inner.b, 0, sizeof(S2) - offsetof(ThreeUInts, a)); // BAD
memset(&f.inner.b, 0, sizeof(S2) - offsetof(ThreeUInts, b)); // GOOD
memset(&f.inner.b, 0, sizeof(S2) - offsetof(ThreeUInts, c)); // GOOD
memset(&f.inner.b, 0, sizeof(S2) - offsetof(FourUInts, inner)); // BAD
memset(&f.inner.b, 0, sizeof(S2) - offsetof(FourUInts, x)); // GOOD
memset(&f.inner.b, 0, sizeof(S2) - offsetof(S2, f)); // BAD
memset(&f.inner.b, 0, sizeof(S2) - offsetof(S2, u)); // GOOD
memset(&f.inner.c, 0, sizeof(S2) - offsetof(ThreeUInts, a)); // BAD
memset(&f.inner.c, 0, sizeof(S2) - offsetof(ThreeUInts, b)); // BAD
memset(&f.inner.c, 0, sizeof(S2) - offsetof(ThreeUInts, c)); // GOOD
memset(&f.inner.c, 0, sizeof(S2) - offsetof(FourUInts, inner)); // BAD
memset(&f.inner.c, 0, sizeof(S2) - offsetof(FourUInts, x)); // GOOD
memset(&f.inner.c, 0, sizeof(S2) - offsetof(S2, f)); // BAD
memset(&f.inner.c, 0, sizeof(S2) - offsetof(S2, u)); // GOOD
memset(&f.inner, 0, sizeof(S2) - offsetof(ThreeUInts, a)); // GOOD
memset(&f.inner, 0, sizeof(S2) - offsetof(ThreeUInts, b)); // GOOD
memset(&f.inner, 0, sizeof(S2) - offsetof(ThreeUInts, c)); // GOOD
memset(&f.inner, 0, sizeof(S2) - offsetof(FourUInts, inner)); // GOOD
memset(&f.inner, 0, sizeof(S2) - offsetof(FourUInts, x)); // GOOD
memset(&f.inner, 0, sizeof(S2) - offsetof(S2, f)); // GOOD
memset(&f.inner, 0, sizeof(S2) - offsetof(S2, u)); // GOOD
memset(&f.x, 0, sizeof(S2) - offsetof(ThreeUInts, a)); // BAD
memset(&f.x, 0, sizeof(S2) - offsetof(ThreeUInts, b)); // BAD
memset(&f.x, 0, sizeof(S2) - offsetof(ThreeUInts, c)); // BAD
memset(&f.x, 0, sizeof(S2) - offsetof(FourUInts, inner)); // BAD
memset(&f.x, 0, sizeof(S2) - offsetof(FourUInts, x)); // GOOD
memset(&f.x, 0, sizeof(S2) - offsetof(S2, f)); // GOOD
memset(&f.x, 0, sizeof(S2) - offsetof(S2, u)); // GOOD
memset(&f, 0, sizeof(S2) - offsetof(ThreeUInts, a)); // GOOD
memset(&f, 0, sizeof(S2) - offsetof(ThreeUInts, b)); // GOOD
memset(&f, 0, sizeof(S2) - offsetof(ThreeUInts, c)); // GOOD
memset(&f, 0, sizeof(S2) - offsetof(FourUInts, inner)); // GOOD
memset(&f, 0, sizeof(S2) - offsetof(FourUInts, x)); // GOOD
memset(&f, 0, sizeof(S2) - offsetof(S2, f)); // GOOD
memset(&f, 0, sizeof(S2) - offsetof(S2, u)); // GOOD
memset(&u, 0, sizeof(S2) - offsetof(ThreeUInts, a)); // BAD
memset(&u, 0, sizeof(S2) - offsetof(ThreeUInts, b)); // BAD
memset(&u, 0, sizeof(S2) - offsetof(ThreeUInts, c)); // BAD
memset(&u, 0, sizeof(S2) - offsetof(FourUInts, inner)); // BAD
memset(&u, 0, sizeof(S2) - offsetof(FourUInts, x)); // BAD
memset(&u, 0, sizeof(S2) - offsetof(S2, f)); // BAD
memset(&u, 0, sizeof(S2) - offsetof(S2, u)); // GOOD
}
};
typedef int MyArray[10];
typedef struct _MyArrayArray {
struct {
int as[10];
} bs[10];
union {
int i;
char cs[4];
} ds[10];
struct {
MyArray xs;
} ys[10];
} MyArrayArray;
void test26() {
MyArrayArray maa;
maa.bs[0].as[-1] = 0; // BAD: underrun write [NOT DETECTED]
maa.bs[0].as[0] = 0; // GOOD
maa.bs[0].as[99] = 0; // GOOD (overflows into bs[9])
maa.bs[0].as[100] = 0; // BAD: overrun write [NOT DETECTED]
maa.bs[1].as[-1] = 0; // GOOD (underflows into bs[0])
maa.bs[1].as[0] = 0; // GOOD
maa.bs[1].as[99] = 0; // BAD: overrun write [NOT DETECTED]
maa.bs[1].as[100] = 0; // BAD: overrun write[ NOT DETECTED]
maa.ds[0].i = 0; // GOOD
maa.ds[9].i = 0; // GOOD
maa.ds[10].i = 0; // BAD: overrun write [NOT DETECTED]
maa.ds[0].cs[0] = 0; // GOOD
maa.ds[0].cs[3] = 0; // GOOD
maa.ds[0].cs[4] = 0; // GOOD (overflows into vs[1])
maa.ds[0].cs[39] = 0; // GOOD (overflows into vs[9])
maa.ds[0].cs[40] = 0; // BAD: overrun write [NOT DETECTED]
maa.ds[9].cs[0] = 0; // GOOD
maa.ds[9].cs[3] = 0; // GOOD
maa.ds[9].cs[4] = 0; // BAD: overrun write [NOT DETECTED]
maa.ys[0].xs[-1] = 0; // BAD: underrun write [NOT DETECTED]
maa.ys[0].xs[0] = 0; // GOOD
maa.ys[0].xs[99] = 0; // GOOD (overflows into bs[9])
maa.ys[0].xs[100] = 0; // BAD: overrun write [NOT DETECTED]
maa.ys[1].xs[-1] = 0; // GOOD (underflows into ys[0])
maa.ys[1].xs[0] = 0; // GOOD
maa.ys[1].xs[99] = 0; // BAD: overrun write [NOT DETECTED]
maa.ys[1].xs[100] = 0; // BAD: overrun write [NOT DETECTED]
char zs[2][2];
zs[0][-1] = 0; // BAD: underrun write [NOT DETECTED]
zs[0][0] = 0; // GOOD
zs[0][1] = 0; // GOOD
zs[0][2] = 0; // GOOD
zs[0][3] = 0; // GOOD
zs[0][4] = 0; // BAD: overrun write [NOT DETECTED]
zs[1][-3] = 0; // BAD: underrun write [NOT DETECTED]
zs[1][-2] = 0; // GOOD
zs[1][-1] = 0; // GOOD
zs[1][0] = 0; // GOOD
zs[1][1] = 0; // GOOD
zs[1][2] = 0; // BAD: overrun write [NOT DETECTED]
}
struct Array10 {
int values[10];
};
void test27(size_t s) {
Array10 arr;
if (s < sizeof(arr.values[10])) { // GOOD (harmless)
// ...
}
if (s < offsetof(Array10, values[10])) { // GOOD (harmless)
// ...
}
if (s < &(arr.values[10]) - &(arr.values[0])) { // GOOD (harmless)
// ...
}
}
bool cond();
void test28() {
int arr[10];
int *ptr1 = arr;
ptr1[-1] = 0; // BAD: underrun write
ptr1++;
ptr1[-1] = 0; // GOOD
int *ptr2 = arr;
ptr2[-1] = 0; // BAD: underrun write
*ptr2++;
ptr2[-1] = 0; // GOOD
int *ptr3 = arr;
ptr3[-1] = 0; // BAD: underrun write
if (cond()) {
ptr3++;
}
ptr3[-1] = 0; // GOOD (depending what cond() does)
int *ptr4 = arr;