From 0c3d5ed8036253115df0f101195dc5ce152cbc52 Mon Sep 17 00:00:00 2001 From: Ivan K Date: Thu, 29 May 2025 00:42:17 +0300 Subject: [PATCH 1/2] Fix malloc/calloc/realloc cast linter Tested by manually calling spatch on an older version of data.table --- .ci/linters/cocci/malloc_return_value_cast.cocci | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.ci/linters/cocci/malloc_return_value_cast.cocci b/.ci/linters/cocci/malloc_return_value_cast.cocci index 7373697743..f88e0ee674 100644 --- a/.ci/linters/cocci/malloc_return_value_cast.cocci +++ b/.ci/linters/cocci/malloc_return_value_cast.cocci @@ -5,8 +5,10 @@ expression E; - (T) malloc(E) +@calloc_realloc_return_value_cast expression@ +type T; +expression E1, E2; +identifier alloc =~ "^(c|re)alloc$"; +@@ - (T) - calloc(_, E) - -- (T) - realloc(_, E) + alloc(E1, E2) From cbc3606dfb550b3f9d5ffc686f5ef8e5a6f382bf Mon Sep 17 00:00:00 2001 From: Ivan K Date: Thu, 29 May 2025 00:50:00 +0300 Subject: [PATCH 2/2] 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. --- .ci/linters/c/cocci_linter.R | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/.ci/linters/c/cocci_linter.R b/.ci/linters/c/cocci_linter.R index cc2bcfb5ad..a2502f43b9 100644 --- a/.ci/linters/c/cocci_linter.R +++ b/.ci/linters/c/cocci_linter.R @@ -1,20 +1,23 @@ cocci_linter = if (!nzchar(Sys.which("spatch"))) function(...) {} else function(c_obj) { - bad <- FALSE + bad = FALSE + tmp = tempfile(fileext = '.c') + on.exit(unlink(tmp)) + writeLines(c_obj$preprocessed, tmp) for (spfile in list.files(".ci/linters/cocci", full.names = TRUE)) { - # Coccinelle parser gets confused sometimes, so ignore stderr and the exit code - out = suppressWarnings(system2( + out = system2( "spatch", - shQuote(c( - "--sp-file", spfile, c_obj$path, "--recursive-includes", - "-I", R.home("include"), "-I", "src" - )), + shQuote(c("--sp-file", spfile, tmp)), stdout = TRUE, stderr = FALSE - )) + ) if (length(out) > 0) { - cat(sprintf("In file '%s', Coccinelle patch '%s' recommends the following changes:\n", c_obj$path, spfile)) + cat(sprintf("In file '%s', Coccinelle linter '%s' located the following problems:\n", c_obj$path, spfile)) writeLines(out) - bad <- TRUE + bad = TRUE + } + if (!is.null(status <- attr(out, 'status'))) { + cat(sprintf("While working on file '%s', Coccinelle linter '%s' failed with exit code %d:\n", c_obj$path, spfile, status)) + bad = TRUE } } - if (bad) stop("Please apply the changes above or fix the linter") + if (bad) stop("Please investigate the problems above.") }