Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

暗黙的cast する ノード にマッチする matcher を作成 #36

Merged
merged 12 commits into from
Jan 16, 2023
Merged
Prev Previous commit
Next Next commit
move: downcastバグファイルを移動
  • Loading branch information
Ran350 committed Jan 14, 2023
commit e5a6d04b4fcdb69b3c9bdcbc56f6556feacd9dfe
19 changes: 9 additions & 10 deletions clang-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
## read 系 バグ

```sh
clang-query -f match-read-bug test.c --
cd read-bug/
clang-query -f read-bug.matcher read-bug.c --
```

## 明示的ダウンキャスト バグ
## ダウンキャスト バグ

```sh
clang-query -f match-exp-downcast-bug test.c --
```

## 暗黙的ダウンキャスト バグ

```sh
clang-query -f match-imp-downcast-bug test.c --
cd downcast-bug/
clang-query -f exp-downcast-bug.matcher exp-downcast-bug.c --
clang-query -f assign-imp-downcast.matcher assign-imp-downcast.c --
clang-query -f return-imp-downcast.matcher return-imp-downcast.c --
```

## write 系 バグ

```sh
clang-query -f match-write-bug test.c --
cd write-bug/
clang-query -f write-bug.matcher write-bug.c --
```
27 changes: 27 additions & 0 deletions clang-query/downcast-bug/assign-imp-downcast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <limits.h>
#include <stdio.h>
#include <time.h>

time_t time_func(int n) { return (time_t)INT_MAX + n; }

void assignDirectly() {
time_t t = time_func(1); // -Winteger-overflow
// printf("assigned directly: time_t: %ld\n", t);
int i = t; // -Wshorten-64-to-32
printf("assigned directly: int: %d\n", i);
}

int assignWithArithmetic() {
time_t t = time_func(3602);
int i = t - (t % 3600); // -Wshorten-64-to-32
if (i < 0) {
puts("overflowed !!!");
}
}

int main(void) {
assignDirectly();
assignWithArithmetic();

return 0;
}
25 changes: 25 additions & 0 deletions clang-query/downcast-bug/exp-downcast-bug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <limits.h>
#include <sys/stat.h>
#include <time.h>

void explicitlyDowncast() {
time_t t = INT_MAX + 1;
// 本来はここに範囲チェックを入れべき
int i = (int)t; // エラーにならない
printf("%d\n", i);
// -2147483648
}

void downcastSttime() {
struct stat stat_buf;
if (stat("a.c", &stat_buf) == 0) {
printf("%d\n", stat_buf.st_atime);
printf("%d\n", stat_buf.st_atime);
}
}

int main(void) {
explicitlyDowncast();
// downcastSttime();
return 0;
}
27 changes: 27 additions & 0 deletions clang-query/downcast-bug/return-imp-downcast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>

time_t timestamp_2038 = (time_t)INT_MAX + 1;

int f1() {
// 本来はここに範囲チェックを入れべき
return timestamp_2038;
}

int f2() {
time_t t = timestamp_2038 + 100;
// 本来はここに範囲チェックを入れべき
return t - (t % 2);
}

int main(void) {
printf("%d\n", f1());
printf("%d\n", f2());

return 0;
}
4 changes: 4 additions & 0 deletions clang-query/downcast-bug/return-imp-downcast.matcher
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# set traversal IgnoreUnlessSpelledInSource
set bind-root true
set print-matcher true
enable output dump
50 changes: 0 additions & 50 deletions clang-query/test.c

This file was deleted.