Skip to content

Commit 59b8b36

Browse files
authored
Fix Coccinelle allocation cast linter (#7039)
* Fix malloc/calloc/realloc cast linter Tested by manually calling spatch on an older version of data.table * Coccinelle linter: look at preprocessed source Otherwise major sources of potential problems fail to parse altogether and get skipped. Make sure to report non-zero exit codes in addition to non-empty diffs. Don't suggest applying the diffs; that won't work on preprocessed source.
1 parent 8454965 commit 59b8b36

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

.ci/linters/c/cocci_linter.R

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
cocci_linter = if (!nzchar(Sys.which("spatch"))) function(...) {} else function(c_obj) {
2-
bad <- FALSE
2+
bad = FALSE
3+
tmp = tempfile(fileext = '.c')
4+
on.exit(unlink(tmp))
5+
writeLines(c_obj$preprocessed, tmp)
36
for (spfile in list.files(".ci/linters/cocci", full.names = TRUE)) {
4-
# Coccinelle parser gets confused sometimes, so ignore stderr and the exit code
5-
out = suppressWarnings(system2(
7+
out = system2(
68
"spatch",
7-
shQuote(c(
8-
"--sp-file", spfile, c_obj$path, "--recursive-includes",
9-
"-I", R.home("include"), "-I", "src"
10-
)),
9+
shQuote(c("--sp-file", spfile, tmp)),
1110
stdout = TRUE, stderr = FALSE
12-
))
11+
)
1312
if (length(out) > 0) {
14-
cat(sprintf("In file '%s', Coccinelle patch '%s' recommends the following changes:\n", c_obj$path, spfile))
13+
cat(sprintf("In file '%s', Coccinelle linter '%s' located the following problems:\n", c_obj$path, spfile))
1514
writeLines(out)
16-
bad <- TRUE
15+
bad = TRUE
16+
}
17+
if (!is.null(status <- attr(out, 'status'))) {
18+
cat(sprintf("While working on file '%s', Coccinelle linter '%s' failed with exit code %d:\n", c_obj$path, spfile, status))
19+
bad = TRUE
1720
}
1821
}
19-
if (bad) stop("Please apply the changes above or fix the linter")
22+
if (bad) stop("Please investigate the problems above.")
2023
}

.ci/linters/cocci/malloc_return_value_cast.cocci

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ expression E;
55
- (T)
66
malloc(E)
77

8+
@calloc_realloc_return_value_cast expression@
9+
type T;
10+
expression E1, E2;
11+
identifier alloc =~ "^(c|re)alloc$";
12+
@@
813
- (T)
9-
calloc(_, E)
10-
11-
- (T)
12-
realloc(_, E)
14+
alloc(E1, E2)

0 commit comments

Comments
 (0)