Skip to content

Commit cfb5c25

Browse files
committed
synchronize inline cpp snippets with checked in cpp files
1 parent 9f46c83 commit cfb5c25

13 files changed

+116
-79
lines changed

docs/sanitizers/examples-alloc-dealloc-mismatch.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This functionality is off by default for Windows. To enable, `set ASAN_OPTIONS=a
1414

1515
```cpp
1616
#include <stdio.h>
17+
1718
#include <stdlib.h>
1819

1920
int main(int argc,char *argv[] ) {
@@ -23,7 +24,7 @@ int main(int argc,char *argv[] ) {
2324
switch (atoi(argv[1])) {
2425

2526
case 1:
26-
delete [] (new int[10]);
27+
delete [] (new int[10]);
2728
break;
2829
case 2:
2930
delete (new int[10]); // Boom!
@@ -35,6 +36,7 @@ int main(int argc,char *argv[] ) {
3536

3637
return 0;
3738
}
39+
3840
```
3941
4042
From a **Developer Command Prompt**:

docs/sanitizers/examples-calloc-overflow.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ int main() {
2727

2828
return 0;
2929
}
30+
3031
```
3132

3233
From a **Developer Command Prompt**:

docs/sanitizers/examples-double-free.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ In `C`, you can call `free()` erroneously. In `C++`, you can call delete more th
1313
## Example C++ - double operator delete
1414

1515
```cpp
16+
1617
int main() {
18+
1719
int *x = new int[42];
1820
delete [] x;
1921

@@ -22,6 +24,7 @@ int main() {
2224
delete [] x;
2325
return 0;
2426
}
27+
2528
```
2629

2730
From a **Developer Command Prompt**:
@@ -37,6 +40,7 @@ From a **Developer Command Prompt**:
3740
## Example 'C' - double fre()
3841

3942
```cpp
43+
4044
#include <stdlib.h>
4145
#include <string.h>
4246

@@ -52,6 +56,8 @@ int main(int argc, char **argv) {
5256
free(x + argc - 1); // Boom!
5357
return res;
5458
}
59+
60+
5561
```
5662
5763
From a **Developer Command Prompt**:

docs/sanitizers/examples-dynamic-stack-buffer-overflow.md

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ Sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/
1414

1515
```cpp
1616

17+
18+
1719
#include <malloc.h>
1820

19-
__declspec(noinline)
2021

22+
__declspec(noinline)
2123
void foo(int index, int len) {
2224

2325
volatile char *str = (volatile char *)_alloca(len);
24-
26+
27+
// reinterpret_cast<long>(str) & 31L;
28+
2529
str[index] = '1'; // Boom !
2630
}
2731

@@ -30,6 +34,7 @@ int main(int argc, char **argv) {
3034
foo(33, 10);
3135
return 0;
3236
}
37+
3338
```
3439
3540
From a **Developer Command Prompt**:
@@ -45,11 +50,13 @@ From a **Developer Command Prompt**:
4550
## example - alloca overflow (left)
4651
4752
```cpp
53+
4854
#include <malloc.h>
4955
5056
__declspec(noinline)
5157
5258
void foo(int index, int len) {
59+
5360
volatile char *str = (volatile char *)_alloca(len);
5461
5562
str[index] = '1'; // Boom!
@@ -59,6 +66,7 @@ int main(int argc, char **argv) {
5966
foo(-1, 10);
6067
return 0;
6168
}
69+
6270
```
6371

6472
From a **Developer Command Prompt**:
@@ -88,66 +96,66 @@ int tmp;
8896

8997
void main()
9098
{
91-
int *cc;
92-
int i;
93-
int k = 17;
99+
int *cc;
100+
int i;
101+
int k = 17;
94102
__try{
95-
tmp = k;
96-
aa = (int*)_alloca(SIZE*sizeof(int));
97-
if (((int)aa) & 0x3)
98-
fail = 1;
99-
for (i=0;i<SIZE;i++){
100-
aa[i] = x+1+i;
101-
}
102-
bb = (int*)_alloca(x*sizeof(int));
103-
if (((int)bb) & 0x3)
104-
fail = 1;
105-
106-
for (i=0;i<x;i++){
107-
bb[i] = 7;
108-
bb[i] = bb[i]+i;
109-
}
110-
{
111-
int s = 112728283;
112-
int ar[8];
113-
for (i = 0; i<8;i++)
114-
ar[i] = s * 17*i;
115-
}
116-
117-
cc = (int*)_alloca(x);
118-
if (((int)cc) & 0x3)
119-
fail = 1;
120-
121-
cc[0] = 0;
122-
cc[1] = 1;
123-
cc[2] = 2;
124-
cc[3] = 3; // <--- Boom!
125-
for (i=0;i<x;i++)
126-
if (bb[i] != (7+i))
127-
fail = 1;
128-
if (tmp != k)
129-
fail = 1;
130-
if (fail){
131-
printf("fail\n");
132-
exit(7);
133-
}
134-
printf("%d\n",(*cc)/y);
135-
printf("fail\n");
136-
exit(7);
103+
tmp = k;
104+
aa = (int*)_alloca(SIZE*sizeof(int));
105+
if (((int)aa) & 0x3)
106+
fail = 1;
107+
for (i=0;i<SIZE;i++){
108+
aa[i] = x+1+i;
109+
}
110+
bb = (int*)_alloca(x*sizeof(int));
111+
if (((int)bb) & 0x3)
112+
fail = 1;
113+
114+
for (i=0;i<x;i++){
115+
bb[i] = 7;
116+
bb[i] = bb[i]+i;
117+
}
118+
{
119+
int s = 112728283;
120+
int ar[8];
121+
for (i = 0; i<8;i++)
122+
ar[i] = s * 17*i;
123+
}
124+
125+
cc = (int*)_alloca(x);
126+
if (((int)cc) & 0x3)
127+
fail = 1;
128+
129+
cc[0] = 0;
130+
cc[1] = 1;
131+
cc[2] = 2;
132+
cc[3] = 3; // <--- Boom!
133+
for (i=0;i<x;i++)
134+
if (bb[i] != (7+i))
135+
fail = 1;
136+
if (tmp != k)
137+
fail = 1;
138+
if (fail){
139+
printf("fail\n");
140+
exit(7);
141+
}
142+
printf("%d\n",(*cc)/y);
143+
printf("fail\n");
144+
exit(7);
137145
} __except (1)
138146

139147
{
140-
141-
for (i=0;i<SIZE;i++)
142-
if (aa[i] != (x+i+1))
143-
fail = 1;
144-
if (fail){
145-
printf("fail\n");
146-
exit(7);
147-
}
148-
printf("pass\n");
149-
exit(0);
150-
}
148+
149+
for (i=0;i<SIZE;i++)
150+
if (aa[i] != (x+i+1))
151+
fail = 1;
152+
if (fail){
153+
printf("fail\n");
154+
exit(7);
155+
}
156+
printf("pass\n");
157+
exit(0);
158+
}
151159
}
152160

153161
```

docs/sanitizers/examples-global-overflow.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ char* x;
3232
float* x[3];
3333

3434
// file: example1-main.c
35+
3536
double x[5];
3637

3738
int main() {
@@ -54,7 +55,9 @@ From a **Developer Command Prompt**:
5455

5556
```cpp
5657
#include <string.h>
57-
int main(int argc, char **argv) {
58+
59+
int
60+
main(int argc, char **argv) {
5861

5962
static char XXX[10];
6063
static char YYY[10];
@@ -67,6 +70,7 @@ int main(int argc, char **argv) {
6770
res += XXX[argc] + ZZZ[argc];
6871
return res;
6972
}
73+
7074
```
7175
7276
From a **Developer Command Prompt**:
@@ -102,7 +106,9 @@ int global[10];
102106
int C::array[10];
103107
104108
int main(int argc, char **argv) {
109+
105110
int one = argc - 1;
111+
106112
switch (argv[1][1]) {
107113
case 'g': return global[one * 11]; //Boom! simple global
108114
case 'c': return C::array[one * 11]; //Boom! class static
@@ -118,6 +124,7 @@ int main(int argc, char **argv) {
118124
}
119125
return 0;
120126
}
127+
121128
```
122129

123130
From a **Developer Command Prompt**:

docs/sanitizers/examples-heap-buffer-overflow.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ Examples sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm
1717
#include <string.h>
1818

1919
int main(int argc, char **argv) {
20+
2021
char *x = (char*)malloc(10 * sizeof(char));
2122
memset(x, 0, 10);
2223
int res = x[argc * 10]; // Boom!
2324

2425
free(x);
2526
return res;
2627
}
28+
2729
```
2830
2931
From a **Developer Command Prompt**:
@@ -51,11 +53,12 @@ class Child : public Parent {
5153
5254
int main(void) {
5355
Parent *p = new Parent;
54-
Child *c = (Child*)p; // Boom!
56+
Child *c = (Child*)p; // Intentional error here!
5557
c->extra_field = 42;
5658
5759
return 0;
5860
}
61+
5962
```
6063

6164
From a **Developer Command Prompt**:
@@ -84,6 +87,7 @@ int main(int argc, char **argv) {
8487

8588
return short_buffer[8];
8689
}
90+
8791
```
8892
8993
From a **Developer Command Prompt**:

docs/sanitizers/examples-heap-use-after-free.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ int main() {
4949
buffer[0] = 42; // Boom!
5050
return 0;
5151
}
52-
5352
```
5453

5554
From a **Developer Command Prompt**:

docs/sanitizers/examples-memcpy-param-overlap.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/
1717
## Example
1818

1919
```cpp
20-
#include "defines.h"
20+
2121
#include <string.h>
2222

2323
__declspec(noinline) void bad_function() {
@@ -30,6 +30,7 @@ int main(int argc, char **argv) {
3030
bad_function();
3131
return 0;
3232
}
33+
3334
```
3435
3536
From a **Developer Command Prompt**:

docs/sanitizers/examples-stack-buffer-overflow.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ From a **Developer Command Prompt**:
6969
## Example - improper down cast on stack
7070

7171
```cpp
72+
73+
7274
class Parent {
7375
public:
7476
int field;
@@ -81,12 +83,13 @@ class Child : public Parent {
8183

8284
int main(void) {
8385

84-
Parent *p = new Parent;
85-
Child *c = (Child*)p; // Boom!
86+
Parent p;
87+
Child *c = (Child*)&p; // Boom !
8688
c->extra_field = 42;
8789

8890
return 0;
8991
}
92+
9093
```
9194
9295
From a **Developer Command Prompt**:

0 commit comments

Comments
 (0)