Repository: bridgecrewio/checkov

Issue:
Checkov gets stuck in an infinite loop
**Describe the issue**
We're running Checkov in our Gitlab Pipeline using this command:
```
checkov -d . -o junitxml --skip-framework terraform_plan --external-checks-gi https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/REDACTED.git//tests/terraform | tee checkov-report.xml
```

This results in Checkov getting stuck in an a seemingly infinite loop.

**Exception Trace**
```sh
Process ForkProcess-9:9:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 297, in json_object
    value, end = scan_once(s, end)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 155, in scan_once
    return _scan_once(string, idx)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 150, in _scan_once
    raise StopIteration(idx)
StopIteration: 158
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parallelizer/parallel_runner.py", line 29, in func_wrapper
    result = original_func(item)
  File "/usr/local/lib/python3.10/site-packages/checkov/json_doc/runner.py", line 18, in <lambda>
    results = parallel_runner.run_function(lambda f: (f, parse(f)), files_to_load)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/__init__.py", line 34, in parse
    (template, template_lines) = load(filename, allow_nulls)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/__init__.py", line 27, in load
    return (json.loads(content, cls=Decoder, allow_nulls=allow_nulls), file_lines)
  File "/usr/local/lib/python3.10/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 224, in decode
    obj = super().decode(s)
  File "/usr/local/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 123, in _scan_once
    return parse_object(
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 297, in json_object
    value, end = scan_once(s, end)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 155, in scan_once
    return _scan_once(string, idx)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 127, in _scan_once
    return parse_array((string, idx + 1), _scan_once)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 229, in json_array
    values, end = JSONArray(s_and_end, scan_once, **kwargs)
  File "/usr/local/lib/python3.10/json/decoder.py", line 230, in JSONArray
    value, end = scan_once(s, end)
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 123, in _scan_once
    return parse_object(
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/decoder.py", line 299, in json_object
    raise DecodeError('Expecting value', s, str(err))
  File "/usr/local/lib/python3.10/site-packages/checkov/common/parsers/json/errors.py", line 24, in __init__
    lineno = doc.count('\n', 0, pos) + 1
TypeError: slice indices must be integers or None or have an __index__ method
```

Running this locally its output ends with:

```sh
2022-02-03 09:21:40,928 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_AZURE_4
2022-02-03 09:21:40,928 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_AZURE_19
2022-02-03 09:21:40,928 [ThreadPoolEx] [DEBUG]  Running graph check: CKV_AZURE_24
2022-02-03 09:21:40,928 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_AZURE_8
2022-02-03 09:21:40,929 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_GCP_3
2022-02-03 09:21:40,929 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_GCP_5
2022-02-03 09:21:40,929 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_GCP_1
2022-02-03 09:21:40,929 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_GCP_7
2022-02-03 09:21:40,930 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_GCP_6
2022-02-03 09:21:40,930 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_GCP_4
2022-02-03 09:21:40,930 [ThreadPoolEx] [DEBUG]  Running graph check: CKV2_GCP_2
```

* If I disable the custom checks it works fine.
* If I change the command to only check for `--framework terraform` with the external checks it works fine.

These external checks have worked fine previously.

**Desktop (please complete the following information):**
 - OS: Checkov official docker container
 - Checkov Version: 2.0.782

**Additional context**
Add any other context about the problem here (e.g. code snippets).



Discussion:
@syphernl any chance to share with us the sources so we will be able to reproduce?
> @syphernl any chance to share with us the sources so we will be able to reproduce?

@schosterbarak Do you mean the sources of the external checks?
No need, I have the reason for this. PR incoming

Resolving patch:
diff --git a/checkov/common/parsers/json/decoder.py b/checkov/common/parsers/json/decoder.py
--- a/checkov/common/parsers/json/decoder.py
+++ b/checkov/common/parsers/json/decoder.py
@@ -1,3 +1,4 @@
+import logging
 from json import JSONDecoder
 from json.decoder import WHITESPACE, WHITESPACE_STR, BACKSLASH, STRINGCHUNK, JSONArray
 from typing import List
@@ -296,7 +297,8 @@ def json_object(self, s_and_end, strict, scan_once, object_hook, object_pairs_ho
             try:
                 value, end = scan_once(s, end)
             except StopIteration as err:
-                raise DecodeError('Expecting value', s, str(err))
+                logging.error(err)
+                raise DecodeError('Expecting value', s, end_mark.line)
             key_str = StrNode(key, beg_mark, end_mark)
             pairs_append((key_str, value))
             try:


Test patch: