python.lang.correctness.unchecked-returns.unchecked-subprocess-call

Community Favorite
profile photo of semgrepsemgrep
Author
46,948
Download Count*

This is not checking the return value of this subprocess call; if it fails no exception will be raised. Consider subprocess.check_call() instead

Run Locally

Run in CI

Defintion

rules:
  - id: unchecked-subprocess-call
    patterns:
      - pattern: subprocess.$CALL(...)
      - pattern-not-inside: $S = subprocess.call(...)
      - pattern-not-inside: subprocess.call(...) == $X
      - pattern-not-inside: return subprocess.call(...)
      - metavariable-pattern:
          metavariable: $CALL
          pattern: call
      - focus-metavariable: $CALL
    fix: check_call
    message: This is not checking the return value of this subprocess call; if it
      fails no exception will be raised. Consider subprocess.check_call()
      instead
    languages:
      - python
    severity: WARNING
    metadata:
      references:
        - https://docs.python.org/3/library/subprocess.html#subprocess.check_call
      category: correctness
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

unchecked-returns.py

import subprocess as sub
import subprocess

# ok: unchecked-subprocess-call
x = sub.call('foo')

# ruleid: unchecked-subprocess-call
sub.call('foo')

# OK: unchecked-subprocess-call
sub.check_call('foo')

# OK: unchecked-subprocess-call
sub.check_call('foo ') == 0

def foo():
    # ok
    return subprocess.call(['ls', '--no'])

def foo():
    # ruleid: unchecked-subprocess-call
    subprocess.call(['ls', '--no'])
    return True

def foo():
    # ruleid: unchecked-subprocess-call
    subprocess.call(['ls', '--no'])
    return True

def foo2():
    return subprocess.call(['ls', '--no']) == 0