Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

An assert statement has a side-effect

ID: py/side-effect-in-assertKind: problemSecurity severity: Severity: errorPrecision: highTags:   - quality   - reliability   - correctnessQuery suites:   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

All code defined in assert statements is ignored when optimization is requested, that is, the program is run with the-O flag. If an assert statement has any side-effects then the behavior of the program changes when optimization is requested.

Recommendation

Move all expressions with side-effects out of assert statements.

Example

In the example, the exit code fromsubprocess.call() is checked against 0, but the entire expression is called from within anassert statement. If the code is ever run, then the not only the assertion itself, but also the external call, will be discarded. It is better to save the result ofsubprocess.call() to a temporary variable, and to assert that variable to be 0.

assertsubprocess.call(['run-backup'])==0

References


[8]ページ先頭

©2009-2025 Movatter.jp