CodeQL documentation

An assert statement has a side-effect

ID: py/side-effect-in-assert
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - correctness
Query 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 from subprocess.call() is checked against 0, but the entire expression is called from within an assert 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 of subprocess.call() to a temporary variable, and to assert that variable to be 0.

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

References

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy