Skip to contentSkip to conversation

coccinelle: misc: Check for hard-coded constants

PATCH In review Mohammed Billoo 2 participants 4 messages Aug 12, 2020 – Aug 13, 2020 1 message not in the archive Coccinelle/Semantic Patches (SmPL)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coccinelle: misc: Check for hard-coded constants

This semantic patch looks for variables that are initialized to constants, arrays that are both declared and indexed with constants. A false positive will occur when a variable is initialized to 0, which must happen for auto variables. This will be resolved in a future patch.

The patch was tested against the following snippet:

int main()

{
    int iarr[54]; /* instance 1 */
    int j = 0;    /* instance 2 */
    int i = 1;    /* instance 3 */
    iarr[0] = 3;  /* instance 4 */
    return 0;
}

and it correctly identified instances 1, 3, and 4. It incorrectly identified instance 2, which will be addressed in a future patch.

---
 scripts/coccinelle/misc/magic_numbers.cocci | 45 +++++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100644 scripts/coccinelle/misc/magic_numbers.cocci
scripts/coccinelle/misc/magic_numbers.cocci new file +45 -0
new file mode 100644
index 000000000000..be6df33d28e4
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Capture and instances of CWE-547 (https://cwe.mitre.org/data/definitions/547.html)
+///
+//# This attempts to capture instances of magic numbers and report them
+
+virtual report
+
+@r1 depends on report@
+type T;
+constant C;
+identifier var;
+position p;
+@@
+* T var@p = C;
+
+@script:python depends on report@
+p << r1.p;
+@@
+coccilib.report.print_report(p[0], "Hard-coded constant, consider using #define")
+
+@r2 depends on report@
+type T;
+identifier var;
+constant C;
+position p;
+@@
+* T var[C];
+
+@script:python depends on report@
+p << r2.p;
+@@
+coccilib.report.print_report(p[0], "Hard-coded constant, consider using #define")
+
+@r3 depends on report@
+type T;
+constant C;
+position p;
+T[] E;
+@@
+* E[C]@p = ... ;
+
+@script:python depends on report@
+p << r3.p;
+@@
+coccilib.report.print_report(p[0], "Hard-coded constant, consider using #define")
Signature · 1 line
2.17.1
#1 lore raw

Hi Mohammed,

Thanks for your patch.

Quoted 16 lines · Mohammed Billoo

This semantic patch looks for variables that are initialized to constants, arrays that are both declared and indexed with constants. A false positive will occur when a variable is initialized to 0, which must happen for auto variables. This will be resolved in a future patch.

The patch was tested against the following snippet:

int main()

{
     int iarr[54]; /* instance 1 */
     int j = 0;    /* instance 2 */
     int i = 1;    /* instance 3 */
     iarr[0] = 3;  /* instance 4 */
     return 0;
}
Quoted 2 lines

and it correctly identified instances 1, 3, and 4. It incorrectly identified instance 2, which will be addressed in a future patch.

Please include the output from the tool that corresponds to your changes to the script in the commit log on a kernel file.

Also I see 3 patches with incremental changes to the script. Please make this a patch series which will make it easier for reviewers.

thanks,
-- Shuah

#2 lore raw

Shuah,

Apologies for the spam. I didn't format the initial correctly and needed two more tries to get it right (according to the kernel standard/best-practice). I can resubmit this patch.

Thanks

Quoted 33 lines · Shuah Khan

Hi Mohammed,

Thanks for your patch.

Quoted 16 lines · Mohammed Billoo

This semantic patch looks for variables that are initialized to constants, arrays that are both declared and indexed with constants. A false positive will occur when a variable is initialized to 0, which must happen for auto variables. This will be resolved in a future patch.

The patch was tested against the following snippet:

int main()

{
     int iarr[54]; /* instance 1 */
     int j = 0;    /* instance 2 */
     int i = 1;    /* instance 3 */
     iarr[0] = 3;  /* instance 4 */
     return 0;
}
Quoted 2 lines

and it correctly identified instances 1, 3, and 4. It incorrectly identified instance 2, which will be addressed in a future patch.

Please include the output from the tool that corresponds to your changes to the script in the commit log on a kernel file.

Also I see 3 patches with incremental changes to the script. Please make this a patch series which will make it easier for reviewers.

thanks,
-- Shuah

Signature · 5 lines
Mohammed A Billoo
Founder
MAB Labs, LLC
www.mab-labs.com
201-338-2022

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

I also had a few more questions regarding the overall format:
1. Is the header format in the semantic patch acceptable (i.e. referencing the CWE that this particular semantic patch aims to address)?
2. Should we create a separate directory for ELISA within coccinelle?

Thanks
Mohammed

Quoted 55 lines · Mohammed Billoo via lists.elisa.tech

Shuah,

Apologies for the spam. I didn't format the initial correctly and needed two more tries to get it right (according to the kernel standard/best-practice). I can resubmit this patch.

Thanks

Quoted 24 lines · Shuah Khan

Hi Mohammed,

Thanks for your patch.

Quoted 16 lines · Mohammed Billoo

This semantic patch looks for variables that are initialized to constants, arrays that are both declared and indexed with constants. A false positive will occur when a variable is initialized to 0, which must happen for auto variables. This will be resolved in a future patch.

The patch was tested against the following snippet:

int main()

{
     int iarr[54]; /* instance 1 */
     int j = 0;    /* instance 2 */
     int i = 1;    /* instance 3 */
     iarr[0] = 3;  /* instance 4 */
     return 0;
}
Quoted 2 lines

and it correctly identified instances 1, 3, and 4. It incorrectly identified instance 2, which will be addressed in a future patch.

Quoted 9 lines

Please include the output from the tool that corresponds to your changes to the script in the commit log on a kernel file.

Also I see 3 patches with incremental changes to the script. Please make this a patch series which will make it easier for reviewers.

thanks,
-- Shuah

--
Mohammed A Billoo
Founder
MAB Labs, LLC
www.mab-labs.com
201-338-2022

Signature · 5 lines
Mohammed A Billoo
Founder
MAB Labs, LLC
www.mab-labs.com
201-338-2022