From 754595ea2d9aee4e789d6fc0f295f64f0e2312d6 Mon Sep 17 00:00:00 2001 From: Colin Vidal Date: Tue, 14 Oct 2025 12:45:21 +0200 Subject: [PATCH 1/3] add spatch to detect implicit bool/int/result cast Detection of implicit cast from a boolean into an int, or an isc_result_t into a boolean (either in an assignement or return position). If such pattern is found, a warning comment is added into the code (and the CI will fails) so the error can be spotted and manually fixed. --- cocci/implicit_casts_warning.spatch | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cocci/implicit_casts_warning.spatch diff --git a/cocci/implicit_casts_warning.spatch b/cocci/implicit_casts_warning.spatch new file mode 100644 index 0000000000..1f2e10cc91 --- /dev/null +++ b/cocci/implicit_casts_warning.spatch @@ -0,0 +1,56 @@ +@bool_into_int_warning@ +position p; +int i; +bool b; +@@ +i = b@p; + +@script:python@ +p << bool_into_int_warning.p; +@@ +report = coccilib.report.build_report(p[0], "WARNING: implicit bool->int cast on assignment") +sys.stderr.write(report) + + + +@return_isc_result_t_into_bool_warning@ +position p; +isc_result_t result; +identifier fn; +identifier literalresult =~ "ISC_R_.*|DNS_R_.*|DST_R_.*|ISCCC_R_.*"; +@@ +bool fn(...) { +<... +( +return result@p; +| +return literalresult@p; +) +...> +} + +@script:python@ +p << return_isc_result_t_into_bool_warning.p; +@@ +report = coccilib.report.build_report(p[0], "WARNING: implicit isc_result_t->bool cast on return value") +sys.stderr.write(report) + + + +@isc_result_t_into_bool_warning@ +position p; +isc_result_t result; +identifier literalresult =~ "ISC_R_.*|DNS_R_.*|DST_R_.*|ISCCC_R_.*"; +bool b; +@@ +( +b = result@p; +| +b = literalresult@p; +) + +@script:python@ +p << isc_result_t_into_bool_warning.p; +@@ +report = coccilib.report.build_report(p[0], "WARNING: implicit isc_result_t->bool cast on assignment") +sys.stderr.write(report) From 25f303f0466ffc821b87ca9aced5935dcaa0be21 Mon Sep 17 00:00:00 2001 From: Colin Vidal Date: Tue, 14 Oct 2025 13:31:44 +0200 Subject: [PATCH 2/3] mdig: fix implicit bool to int cast The `display_rrcomments` is a tri-state (-1, 0, 1) which is (in some cases) initialized with `state`, a boolean, through an implicit cast. This was spot by Coccinelle. Remove the implcit cast by explicitly assigning 0 or 1 to `display_rrcomments` based on `state` value. --- bin/tools/mdig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index 339b75644a..ddfddee46d 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -1594,7 +1594,7 @@ plus_option(char *option, struct query *query, bool global) { FULLCHECK("yaml"); yaml = state; if (state) { - display_rrcomments = state; + display_rrcomments = 1; } break; case 'z': /* zflag */ From 3b3bb3eb80d62ce3a226bb675268ff92b4b54ef4 Mon Sep 17 00:00:00 2001 From: Colin Vidal Date: Wed, 15 Oct 2025 15:35:59 +0200 Subject: [PATCH 3/3] check-cocci fails in WARNING is found on stderr As the implicit cast check print "WARNING: ..." on stderr, add a pattern to make sure that check-cocci would fails if such warning is found on stderr. This is generic (not specific like the existing "parse error") so it should be able to support future Coccinelle spatch warnings. --- util/check-cocci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/check-cocci.sh b/util/check-cocci.sh index 09783ea9af..f9f7acbbbb 100755 --- a/util/check-cocci.sh +++ b/util/check-cocci.sh @@ -23,7 +23,7 @@ run_spatch() { echo "Applying semantic patch $spatch..." spatch --jobs "${TEST_PARALLEL_JOBS:-1}" --sp-file "$spatch" --use-gitgrep --dir "." --include-headers $spatchargs >>"$patch" 2>cocci.stderr cat cocci.stderr - if grep -q -e "parse error" -e "EXN: Failure" cocci.stderr; then + if grep -q -e "parse error" -e "EXN: Failure" -e "WARNING" cocci.stderr; then ret=1 fi if [ "$(wc <"$patch" -l)" -gt "0" ]; then