Skip to content

Commit

Permalink
Check for exact function in unwanted function check
Browse files Browse the repository at this point in the history
check-unwanted-cpp-functions.py has false positives. The script reports
`asprintf` as usage of `sprintf`.

To prevent issues like this, modify the regex for checking functions so
that we only report exact matches.

BUG=b:320231979
TEST=`devtools/check-unwanted-cpp-functions.py --check_all_files` and
check that there are no `sprintf` reports.

Change-Id: Ieed8121b525e3e84ab3cc4414acd3db1535aad9c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5248447
Commit-Queue: Baili Deng <bailideng@google.com>
Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com <chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com>
Reviewed-by: Li-Yu Yu <aaronyu@google.com>
  • Loading branch information
baili0411 authored and Chromeos LUCI committed Jan 31, 2024
1 parent 3936bdb commit 81ad4d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
20 changes: 10 additions & 10 deletions devtools/blocked_functions.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
{
"blocked_functions": [
{
"regex": "atoi",
"function": "atoi",
"replacement": "strtol"
},
{
"regex": "atof",
"function": "atof",
"replacement": "strtof"
},
{
"regex": "sprintf",
"function": "sprintf",
"replacement": "snprintf or scnprintf if using the return value"
},
{
"regex": "strcpy",
"function": "strcpy",
"replacement": "strlcpy"
},
{
"regex": "strncpy",
"function": "strncpy",
"replacement": "strlcpy"
},
{
"regex": "strlen",
"function": "strlen",
"replacement": "strnlen"
},
{
"regex": "strdup",
"function": "strdup",
"replacement": "strndup"
},
{
"regex": "strstr",
"function": "strstr",
"replacement": "strnstr"
},
{
"regex": "strchr",
"function": "strchr",
"replacement": "strnchr"
},
{
"regex": "strdupa",
"function": "strdupa",
"replacement": "strndupa"
}
]
Expand Down
5 changes: 3 additions & 2 deletions devtools/check-unwanted-cpp-functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ def _check_line(line):
return None

for function in blocked_functions:
match = re.search(function["regex"], line)
regex = r"(?<![a-zA-Z])" + function["function"] + r"\("
match = re.search(regex, line)
if match:
return f'Found usage of "{function["regex"]}", please replace it with "{function["replacement"]}"'
return f'Found usage of "{function["function"]}", please replace it with "{function["replacement"]}"'
return None

error_list = []
Expand Down

0 comments on commit 81ad4d5

Please sign in to comment.