Check for unsafe global or static objects. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.
(filename, clean_lines, linenum, error)
| 4917 | |
| 4918 | |
| 4919 | def CheckGlobalStatic(filename, clean_lines, linenum, error): |
| 4920 | """Check for unsafe global or static objects. |
| 4921 | |
| 4922 | Args: |
| 4923 | filename: The name of the current file. |
| 4924 | clean_lines: A CleansedLines instance containing the file. |
| 4925 | linenum: The number of the line to check. |
| 4926 | error: The function to call with any errors found. |
| 4927 | """ |
| 4928 | line = clean_lines.elided[linenum] |
| 4929 | |
| 4930 | # Match two lines at a time to support multiline declarations |
| 4931 | if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line): |
| 4932 | line += clean_lines.elided[linenum + 1].strip() |
| 4933 | |
| 4934 | # Check for people declaring static/global STL strings at the top level. |
| 4935 | # This is dangerous because the C++ language does not guarantee that |
| 4936 | # globals with constructors are initialized before the first access. |
| 4937 | match = Match( |
| 4938 | r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)', |
| 4939 | line) |
| 4940 | |
| 4941 | # Remove false positives: |
| 4942 | # - String pointers (as opposed to values). |
| 4943 | # string *pointer |
| 4944 | # const string *pointer |
| 4945 | # string const *pointer |
| 4946 | # string *const pointer |
| 4947 | # |
| 4948 | # - Functions and template specializations. |
| 4949 | # string Function<Type>(... |
| 4950 | # string Class<Type>::Method(... |
| 4951 | # |
| 4952 | # - Operators. These are matched separately because operator names |
| 4953 | # cross non-word boundaries, and trying to match both operators |
| 4954 | # and functions at the same time would decrease accuracy of |
| 4955 | # matching identifiers. |
| 4956 | # string Class::operator*() |
| 4957 | if (match and |
| 4958 | not Search(r'\bstring\b(\s+const)?\s*\*\s*(const\s+)?\w', line) and |
| 4959 | not Search(r'\boperator\W', line) and |
| 4960 | not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(3))): |
| 4961 | error(filename, linenum, 'runtime/string', 4, |
| 4962 | 'For a static/global string constant, use a C style string instead: ' |
| 4963 | '"%schar %s[]".' % |
| 4964 | (match.group(1), match.group(2))) |
| 4965 | |
| 4966 | if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line): |
| 4967 | error(filename, linenum, 'runtime/init', 4, |
| 4968 | 'You seem to be initializing a member variable with itself.') |
| 4969 | |
| 4970 | |
| 4971 | def CheckPrintf(filename, clean_lines, linenum, error): |
no test coverage detected