Check for printf related issues. 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)
| 5443 | |
| 5444 | |
| 5445 | def CheckPrintf(filename, clean_lines, linenum, error): |
| 5446 | """Check for printf related issues. |
| 5447 | |
| 5448 | Args: |
| 5449 | filename: The name of the current file. |
| 5450 | clean_lines: A CleansedLines instance containing the file. |
| 5451 | linenum: The number of the line to check. |
| 5452 | error: The function to call with any errors found. |
| 5453 | """ |
| 5454 | line = clean_lines.elided[linenum] |
| 5455 | |
| 5456 | # When snprintf is used, the second argument shouldn't be a literal. |
| 5457 | match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line) |
| 5458 | if match and match.group(2) != '0': |
| 5459 | # If 2nd arg is zero, snprintf is used to calculate size. |
| 5460 | error(filename, linenum, 'runtime/printf', 3, |
| 5461 | 'If you can, use sizeof(%s) instead of %s as the 2nd arg ' |
| 5462 | 'to snprintf.' % (match.group(1), match.group(2))) |
| 5463 | |
| 5464 | # Check if some verboten C functions are being used. |
| 5465 | if Search(r'\bsprintf\s*\(', line): |
| 5466 | error(filename, linenum, 'runtime/printf', 5, |
| 5467 | 'Never use sprintf. Use snprintf instead.') |
| 5468 | match = Search(r'\b(strcpy|strcat)\s*\(', line) |
| 5469 | if match: |
| 5470 | error(filename, linenum, 'runtime/printf', 4, |
| 5471 | 'Almost always, snprintf is better than %s' % match.group(1)) |
| 5472 | |
| 5473 | |
| 5474 | def IsDerivedFunction(clean_lines, linenum): |
no test coverage detected