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)
| 6337 | |
| 6338 | |
| 6339 | def CheckPrintf(filename, clean_lines, linenum, error): |
| 6340 | """Check for printf related issues. |
| 6341 | |
| 6342 | Args: |
| 6343 | filename: The name of the current file. |
| 6344 | clean_lines: A CleansedLines instance containing the file. |
| 6345 | linenum: The number of the line to check. |
| 6346 | error: The function to call with any errors found. |
| 6347 | """ |
| 6348 | line = clean_lines.elided[linenum] |
| 6349 | |
| 6350 | # When snprintf is used, the second argument shouldn't be a literal. |
| 6351 | match = re.search(r"snprintf\s*\(([^,]*),\s*([0-9]*)\s*,", line) |
| 6352 | if match and match.group(2) != "0": |
| 6353 | # If 2nd arg is zero, snprintf is used to calculate size. |
| 6354 | error( |
| 6355 | filename, |
| 6356 | linenum, |
| 6357 | "runtime/printf", |
| 6358 | 3, |
| 6359 | "If you can, use" |
| 6360 | f" sizeof({match.group(1)}) instead of {match.group(2)}" |
| 6361 | " as the 2nd arg to snprintf.", |
| 6362 | ) |
| 6363 | |
| 6364 | # Check if some verboten C functions are being used. |
| 6365 | if re.search(r"\bsprintf\s*\(", line): |
| 6366 | error(filename, linenum, "runtime/printf", 5, "Never use sprintf. Use snprintf instead.") |
| 6367 | match = re.search(r"\b(strcpy|strcat)\s*\(", line) |
| 6368 | if match: |
| 6369 | error( |
| 6370 | filename, |
| 6371 | linenum, |
| 6372 | "runtime/printf", |
| 6373 | 4, |
| 6374 | f"Almost always, snprintf is better than {match.group(1)}", |
| 6375 | ) |
| 6376 | |
| 6377 | |
| 6378 | def IsDerivedFunction(clean_lines, linenum): |
no test coverage detected
searching dependent graphs…