xref: /freebsd/sys/contrib/openzfs/.github/codeql/custom-queries/cpp/deprecatedFunctionUsage.ql (revision e1e636193db45630c7881246d25902e57c43d24e)
1/**
2 * @name Deprecated function usage detection
3 * @description Detects functions whose usage is banned from the OpenZFS
4 *              codebase due to QA concerns.
5 * @kind problem
6 * @severity error
7 * @id cpp/deprecated-function-usage
8*/
9
10import cpp
11
12predicate isDeprecatedFunction(Function f) {
13  f.getName() = "strtok" or
14  f.getName() = "__xpg_basename" or
15  f.getName() = "basename" or
16  f.getName() = "dirname" or
17  f.getName() = "bcopy" or
18  f.getName() = "bcmp" or
19  f.getName() = "bzero" or
20  f.getName() = "asctime" or
21  f.getName() = "asctime_r" or
22  f.getName() = "gmtime" or
23  f.getName() = "localtime" or
24  f.getName() = "strncpy"
25
26}
27
28string getReplacementMessage(Function f) {
29  if f.getName() = "strtok" then
30    result = "Use strtok_r(3) instead!"
31  else if f.getName() = "__xpg_basename" then
32    result = "basename(3) is underspecified. Use zfs_basename() instead!"
33  else if f.getName() = "basename" then
34    result = "basename(3) is underspecified. Use zfs_basename() instead!"
35  else if f.getName() = "dirname" then
36    result = "dirname(3) is underspecified. Use zfs_dirnamelen() instead!"
37  else if f.getName() = "bcopy" then
38    result = "bcopy(3) is deprecated. Use memcpy(3)/memmove(3) instead!"
39  else if f.getName() = "bcmp" then
40    result = "bcmp(3) is deprecated. Use memcmp(3) instead!"
41  else if f.getName() = "bzero" then
42    result = "bzero(3) is deprecated. Use memset(3) instead!"
43  else if f.getName() = "asctime" then
44    result = "Use strftime(3) instead!"
45  else if f.getName() = "asctime_r" then
46    result = "Use strftime(3) instead!"
47  else if f.getName() = "gmtime" then
48    result = "gmtime(3) isn't thread-safe. Use gmtime_r(3) instead!"
49  else if f.getName() = "localtime" then
50    result = "localtime(3) isn't thread-safe. Use localtime_r(3) instead!"
51  else
52    result = "strncpy(3) is deprecated. Use strlcpy(3) instead!"
53}
54
55from FunctionCall fc, Function f
56where
57  fc.getTarget() = f and
58  isDeprecatedFunction(f)
59select fc, getReplacementMessage(f)
60