# Buggy code
def check_minions(
    self, expr, expr_form="glob", delimiter=DEFAULT_TARGET_DELIM, greedy=True
):
    """
    Check the passed regex against the available minions' public keys
    stored for authentication. This should return a set of ids which
    match the regex, this will then be used to parse the returns to
    make sure everyone has checked back in.
    """
    try:
        check_func = getattr(self, "_check_{0}_minions".format(expr_form), None)
        if expr_form in (
            "grain",
            "grain_pcre",
            "pillar",
            "pillar_pcre",
            "pillar_exact",
            "compound",
            "compound_pillar_exact",
        ):
            minions = check_func(expr, delimiter, greedy)
        else:
            minions = check_func(expr, greedy)
    except Exception:
        log.exception(
            "Failed matching available minions with {0} pattern: {1}".format(
                expr_form, expr
            )
        )
        minions = []
    return minions


# Traceback (TypeError)
[root@salt salt]# salt-run cache.grains

[ERROR   ] Failed matching available minions with glob pattern: None
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/utils/minions.py", line 639, in check_minions
minions = check_func(expr, greedy)
File "/usr/lib/python2.7/site-packages/salt/utils/minions.py", line 194, in _check_glob_minions
return fnmatch.filter(self._pki_minions(), expr)
File "/usr/lib64/python2.7/fnmatch.py", line 51, in filter
res = translate(pat)
File "/usr/lib64/python2.7/fnmatch.py", line 87, in translate
i, n = 0, len(pat)
TypeError: object of type 'NoneType' has no len()
Exception occurred in runner cache.grains: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/client/mixins.py", line 395, in _low
data['return'] = self.functions[fun](*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/runners/cache.py", line 38, in grains
cached_grains = pillar_util.get_minion_grains()
File "/usr/lib/python2.7/site-packages/salt/utils/master.py", line 290, in get_minion_grains
cached_minion_grains, cached_minion_pillars = self._get_cached_minion_data(*minion_ids)
File "/usr/lib/python2.7/site-packages/salt/utils/master.py", line 135, in _get_cached_minion_data
if 'grains' in mdata:
TypeError: argument of type 'NoneType' is not iterable

# Fixed code
def check_minions(
    self, expr, expr_form="glob", delimiter=DEFAULT_TARGET_DELIM, greedy=True
):
    """
    Check the passed regex against the available minions' public keys
    stored for authentication. This should return a set of ids which
    match the regex, this will then be used to parse the returns to
    make sure everyone has checked back in.
    """
    try:
        if expr is None:
            expr = ""
        check_func = getattr(self, "_check_{0}_minions".format(expr_form), None)
        if expr_form in (
            "grain",
            "grain_pcre",
            "pillar",
            "pillar_pcre",
            "pillar_exact",
            "compound",
            "compound_pillar_exact",
        ):
            minions = check_func(expr, delimiter, greedy)
        else:
            minions = check_func(expr, greedy)
    except Exception:
        log.exception(
            "Failed matching available minions with {0} pattern: {1}".format(
                expr_form, expr
            )
        )
        minions = []
    return minions