From 039932900ba8cef1fbbc1bf8e589326e89a46c64 Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Thu, 1 Oct 2015 22:13:01 +0200 Subject: pacemaker: rewrite_snprintf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- pacemaker/rewrite_snprintf.alter.cocci | 62 +++++++++++++++ pacemaker/rewrite_snprintf.cocci | 133 +++++++++++++++++++++++++++++++++ pacemaker/rewrite_snprintf.light.cocci | 66 ++++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 pacemaker/rewrite_snprintf.alter.cocci create mode 100644 pacemaker/rewrite_snprintf.cocci create mode 100644 pacemaker/rewrite_snprintf.light.cocci diff --git a/pacemaker/rewrite_snprintf.alter.cocci b/pacemaker/rewrite_snprintf.alter.cocci new file mode 100644 index 0000000..c683624 --- /dev/null +++ b/pacemaker/rewrite_snprintf.alter.cocci @@ -0,0 +1,62 @@ +// Switch certain class of snprintf to use internal macro crm_snprintf_max +// jpokorny@redhat.com + +@ has_crm_internal @ +@@ + +#include + + +@ snprintf_rewrite @ +identifier fn; +identifier id_str; +identifier id_max; +identifier id_offset; +@@ +fn(...) { +<... +id_offset += +- snprintf(id_str + id_offset, id_max - id_offset ++ crm_snprintf_offset(id_str, id_offset, id_max +, ...); +...> +} + + +@ snprintf_assert depends on snprintf_rewrite forall @ +identifier snprintf_rewrite.fn; +identifier snprintf_rewrite.id_max; +identifier snprintf_rewrite.id_offset; +@@ +fn(...) { +<... +- CRM_LOG_ASSERT(id_offset > 0); ++ CRM_LOG_ASSERT(id_offset > 0 && id_offset < id_max); +...> +} + + +// this is not the best placement, it serves a subsequent manual review +// to denote necessity to add the include at all, and the reviewer will +// (hopefully) move the include to the correct location +@ snprintf_include depends on (snprintf_rewrite && !(has_crm_internal)) exists @ +identifier snprintf_rewrite.fn; +@@ ++ #include /* crm_snprintf_offset */ + +fn(...) { ... } + + +@ snprintf_make_const depends on snprintf_rewrite forall @ +identifier snprintf_rewrite.fn; +identifier snprintf_rewrite.id_max; +identifier snprintf_rewrite.id_offset; +type t_max; +expression E_max; +@@ +fn(...) { +<+... +- static t_max id_max = E_max; ++ static const t_max id_max = E_max; +...+> +} diff --git a/pacemaker/rewrite_snprintf.cocci b/pacemaker/rewrite_snprintf.cocci new file mode 100644 index 0000000..8093636 --- /dev/null +++ b/pacemaker/rewrite_snprintf.cocci @@ -0,0 +1,133 @@ +// Switch certain class of snprintf to use internal macro crm_snprintf_max +// jpokorny@redhat.com + +@ has_crm_internal @ +@@ + +#include + + +@ snprintf_eligible_1 exists @ +expression E1, E2; +identifier fn, id_str, id_max, id_offset; +type t_max, t_offset; +@@ +fn(...) { +... +( +t_offset id_offset = E1; +... +- static t_max id_max = E2; ++ static const t_max id_max = E2; +| +- static t_max id_max = E2; ++ static const t_max id_max = E2; +... +t_offset id_offset = E1; +| +t_offset id_offset = E1; +... +t_max id_max = E2; +| +t_max id_max = E2; +... +t_offset id_offset = E1; +//| +//t_offset id_offset = E1; +//| +//t_offset id_offset = E1, id_max = E2; +//| +//t_offset id_max = E2, id_offset = E1; +) +<+... +id_offset += snprintf(id_str + id_offset, id_max - id_offset, ...); +...+> +} + + +@ snprintf_eligible_2 exists @ +identifier fn, id_str, id_max, id_offset; +type t_max, t_offset; +@@ +fn(..., t_offset id_offset, t_max id_max,...) +{ +<+... +id_offset += snprintf(id_str + id_offset, id_max - id_offset, ...); +...+> +} + + +@ snprintf_rewrite_1 depends on snprintf_eligible_1 forall @ +identifier snprintf_eligible_1.fn; +identifier snprintf_eligible_1.id_str; +identifier snprintf_eligible_1.id_max; +identifier snprintf_eligible_1.id_offset; +@@ +fn(...) { +<... +id_offset += +- snprintf(id_str + id_offset, id_max - id_offset ++ crm_snprintf_offset(id_str, id_offset, id_max +, ...); +...> +} + +@ snprintf_rewrite_2 depends on snprintf_eligible_2 forall @ +identifier snprintf_eligible_2.fn; +identifier snprintf_eligible_2.id_str; +identifier snprintf_eligible_2.id_max; +identifier snprintf_eligible_2.id_offset; +@@ +fn(...) { +<... +id_offset += +- snprintf(id_str + id_offset, id_max - id_offset ++ crm_snprintf_offset(id_str, id_offset, id_max +, ...); +...> +} + + +@ snprintf_assert_1 depends on snprintf_eligible_1 forall @ +identifier snprintf_eligible_1.fn; +identifier snprintf_eligible_1.id_max; +identifier snprintf_eligible_1.id_offset; +@@ +fn(...) { +<... +- CRM_LOG_ASSERT(id_offset > 0); ++ CRM_LOG_ASSERT(id_offset > 0 && id_offset < id_max); +...> +} + + +@ snprintf_assert_2 depends on snprintf_eligible_2 forall @ +identifier snprintf_eligible_2.fn; +identifier snprintf_eligible_2.id_max; +identifier snprintf_eligible_2.id_offset; +@@ +fn(...) { +<... +- CRM_LOG_ASSERT(id_offset > 0); ++ CRM_LOG_ASSERT(id_offset > 0 && id_offset < id_max); +...> +} + + +// this is not the best placement, it serves a subsequent manual review +// to denote necessity to add the include at all, and the reviewer will +// (hopefully) move the include to the correct location +@ snprintf_include_1 depends on (snprintf_eligible_1 && !(has_crm_internal)) exists @ +identifier snprintf_eligible_1.fn; +@@ ++ #include /* crm_snprintf_offset */ + +fn(...) { ... } + + +@ snprintf_include_2 depends on (snprintf_eligible_2 && !(has_crm_internal)) exists @ +identifier snprintf_eligible_2.fn; +@@ ++ #include /* crm_snprintf_offset */ + +fn(...) { ... } diff --git a/pacemaker/rewrite_snprintf.light.cocci b/pacemaker/rewrite_snprintf.light.cocci new file mode 100644 index 0000000..b53a941 --- /dev/null +++ b/pacemaker/rewrite_snprintf.light.cocci @@ -0,0 +1,66 @@ +// Switch certain class of snprintf to use internal macro crm_snprintf_max +// jpokorny@redhat.com + +@ has_crm_internal @ +@@ + +#include + + +//@ snprintf_eligible exists @ +//expression E1, E2; +//identifier fn, id_str, id_max, id_offset; +//type t_max, t_offset; +//@@ +//fn(...) { +//... +//( +//t_offset id_offset = E1; +//... +//- static t_max id_max = E2; +//+ static const t_max id_max = E2; +//| +//- static t_max id_max = E2; +//+ static const t_max id_max = E2; +//... +//t_offset id_offset = E1; +//) +//<... +//id_offset += snprintf(id_str + id_offset, id_max - id_offset, ...); +//...> +//} + + +@ snprintf_rewrite @ +identifier id_str; +identifier fn, id_max, id_offset; +@@ +fn(...) { +<... +id_offset += +- snprintf(id_str + id_offset, id_max - id_offset ++ crm_snprintf_offset(id_str, id_offset, id_max +, ...); +...> +} + + +@ snprintf_assert forall @ +identifier snprintf_rewrite.fn, snprintf_rewrite.id_max, snprintf_rewrite.id_offset; +@@ +fn(...) { +<... +- CRM_LOG_ASSERT(id_offset > 0); ++ CRM_LOG_ASSERT(id_offset > 0 && id_offset < id_max); +...> +} + +// this is not the best placement, it serves a subsequent manual review +// to denote necessity to add the include at all, and the reviewer will +// (hopefully) move the include to the correct location +@ snprintf_include depends on (snprintf_rewrite && !(has_crm_internal)) exists @ +identifier snprintf_rewrite.fn; +@@ ++ #include /* crm_snprintf_offset */ + +fn(...) { ... } -- cgit