Skip to content

Fix unreachable JSON validation code in validate_input_path#4782

Open
codewithfourtix wants to merge 3 commits intoaboutcode-org:developfrom
codewithfourtix:fix-dead-code-json-validation
Open

Fix unreachable JSON validation code in validate_input_path#4782
codewithfourtix wants to merge 3 commits intoaboutcode-org:developfrom
codewithfourtix:fix-dead-code-json-validation

Conversation

@codewithfourtix
Copy link

In src/scancode/cli.py, the validate_input_path function contained a premature raise statement that made the JSON validation checks below it dead/unreachable code. As a result, two important validations were silently never executed:

  • Checking that the JSON input file has a .json extension
  • Checking that the JSON file is well-formed (starts with {)

Root Cause

The raise click.BadParameter(...) for "not a file" was placed before the extension and content checks, causing Python to exit the block immediately and skip the remaining validations entirely.

Before (Broken)

if from_json and not is_file(location=inp, follow_symlinks=True):
    raise click.BadParameter(f"JSON input: {inp!r} is not a file")
    if not inp.lower().endswith(".json"):   # ← never reached
        raise click.BadParameter(f"JSON input: {inp!r} is not a JSON file with a .json extension")
    with open(inp) as js:
        start = js.read(100).strip()
    if not start.startswith("{"):           # ← never reached
        raise click.BadParameter(f"JSON input: {inp!r} is not a well formed JSON file")

After (Fixed)

if from_json and not is_file(location=inp, follow_symlinks=True):
    raise click.BadParameter(f"JSON input: {inp!r} is not a file")

if from_json and is_file(location=inp, follow_symlinks=True):
    if not inp.lower().endswith(".json"):
        raise click.BadParameter(f"JSON input: {inp!r} is not a JSON file with a .json extension")
    with open(inp) as js:
        start = js.read(100).strip()
    if not start.startswith("{"):
        raise click.BadParameter(f"JSON input: {inp!r} is not a well formed JSON file")

Impact

Without this fix, users could pass a non-.json file or a malformed JSON file as --from-json input and ScanCode would proceed without raising an appropriate error, likely causing a confusing failure downstream.


Tasks

Copilot AI review requested due to automatic review settings March 1, 2026 14:32
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the JSON-specific validation path in validate_input_path() so that JSON extension/content checks can run (instead of being placed after an unconditional raise).

Changes:

  • Splits the --from-json validation into “not a file” vs “is a file” branches.
  • Restores execution of .json extension and “looks like JSON” ({ prefix) checks for file inputs.
Comments suppressed due to low confidence (1)

src/scancode/cli.py:202

  • open(inp) uses the platform default text encoding and the read() can raise UnicodeDecodeError for binary/non-UTF-8 inputs, which would surface as an unhandled exception during CLI argument validation. Handle decode errors explicitly (e.g., specify an encoding and catch UnicodeDecodeError) and convert them to a click.BadParameter so users get a consistent, friendly error.
            with open(inp) as js:
                start = js.read(100).strip()
            if not start.startswith("{"):
                raise click.BadParameter(f"JSON input: {inp!r} is not a well formed JSON file")

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

codewithfourtix and others added 2 commits March 1, 2026 21:53
The .json extension check and JSON well-formedness check after
the 'raise click.BadParameter' were unreachable dead code. Move
these validations into a separate conditional block that runs
when from_json is True and the input is a valid file.

Signed-off-by: codewithfourtix <codewithfourtix@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: codewithfourtix <codewithfourtix@gmail.com>
@codewithfourtix codewithfourtix force-pushed the fix-dead-code-json-validation branch from 79803f6 to ba29630 Compare March 1, 2026 16:54
The JSON extension and well-formedness checks were unreachable dead
code because they appeared after an unconditional raise statement.
Restructure into if/elif blocks so both checks are actually executed.
Also add explicit UTF-8 encoding and handle UnicodeDecodeError when
reading the JSON file header to give users a clear error message.

Signed-off-by: codewithfourtix codewithfourtix@gmail.com
Signed-off-by: codewithfourtix <codewithfourtix@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants