Skip to content

Commit

Permalink
Added coccinelle rule to find strlcpy on NameData
Browse files Browse the repository at this point in the history
NameData is a fixed-size type of 64 bytes. Using strlcpy to copy data
into a NameData struct can cause problems because any data that follows
the initial null-terminated string will also be part of the data.
  • Loading branch information
jnidzwetzki committed Feb 20, 2023
1 parent 8a51a76 commit 46a6ada
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions coccinelle/namedata.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// NameData is a fixed-size type of 64 bytes. Using strlcpy to copy data into a
// NameData struct can cause problems because any data that follows the initial
// null-terminated string will also be part of the data.

@rule_var_decl_struct@
symbol NAMEDATALEN;
identifier I1, I2;
@@
struct I1
{
...
- char I2[NAMEDATALEN];
+ /* You are declaring a char of length NAMEDATALEN, please consider using NameData instead. */
+ NameData I2;
...
}

@rule_namedata_strlcpy@
expression E1, E2;
symbol NAMEDATALEN;
@@
- strlcpy(E1, E2, NAMEDATALEN);
+ /* You are using strlcpy with NAMEDATALEN, please consider using NameData and namestrcpy instead. */
+ namestrcpy(E1, E2);
2 changes: 1 addition & 1 deletion scripts/coccinelle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ FAILED=false
true > coccinelle.diff

for f in "${SCRIPT_DIR}"/../coccinelle/*.cocci; do
find "${SCRIPT_DIR}"/.. -name '*.c' -exec spatch --very-quiet -sp_file "$f" {} + | tee -a coccinelle.diff
spatch --very-quiet --include-headers --sp-file "$f" --dir "${SCRIPT_DIR}"/.. | tee -a coccinelle.diff
rc=$?
if [ $rc -ne 0 ]; then
FAILED=true
Expand Down

0 comments on commit 46a6ada

Please sign in to comment.