Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 10, 2025

Addresses potential KeyError when accessing msix_info["table_size"] if the dictionary is None, malformed, or missing the key.

Changes

  • src/device_clone/msix.py: Added comprehensive validation before dictionary access:

    # Before
    if msix_info["table_size"] > 0:
    
    # After
    if (
        msix_info is not None
        and isinstance(msix_info, dict)
        and "table_size" in msix_info
        and msix_info["table_size"] > 0
    ):
  • Import fixes: Corrected from string_utils import to from src.string_utils import in 4 files (vfio_handler.py, option_rom_manager.py, post_build_validator.py, vivado_utils.py) that were blocking test execution

  • Test coverage: Added 3 unit tests validating behavior when msix_info is None, missing keys, or has zero table_size

Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"Potential KeyError if 'table_size' key is missing from msix_info. Should check if msix_info is not None and contains the key before accessing.","fixFiles":[{"filePath":"src/device_clone/msix.py","diff":"diff --git a/src/device_clone/msix.py b/src/device_clone/msix.py\n--- a/src/device_clone/msix.py\n+++ b/src/device_clone/msix.py\n@@ -186,7 +186,12 @@\n             config_space_hex = config_space_bytes.hex()\n             msix_info = parse_msix_capability(config_space_hex)\n \n-            if msix_info[\"table_size\"] > 0:\n+            if (\n+                msix_info is not None\n+                and isinstance(msix_info, dict)\n+                and \"table_size\" in msix_info\n+                and msix_info[\"table_size\"] > 0\n+            ):\n                 log_info_safe(\n                     self.logger,\n                     safe_format(\n"}]}]

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 10, 2025 06:46
…idator, and vivado_utils

Co-authored-by: ramseymcgrath <2006510+ramseymcgrath@users.noreply.github.com>
Co-authored-by: ramseymcgrath <2006510+ramseymcgrath@users.noreply.github.com>
Co-authored-by: ramseymcgrath <2006510+ramseymcgrath@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix potential KeyError in msix.py for table_size Add defensive checks for msix_info dictionary access Nov 10, 2025
Copilot finished work on behalf of ramseymcgrath November 10, 2025 06:57
Copilot AI requested a review from ramseymcgrath November 10, 2025 06:57
@ramseymcgrath
Copy link
Collaborator

@copilot rebase

Copy link
Contributor Author

Copilot AI commented Nov 16, 2025

@copilot rebase

I'm unable to perform a git rebase as my tooling doesn't support force-pushing, which is required for rebasing. The branch is currently up-to-date with origin/copilot/fix-keyerror-msix-info.

If there have been updates to the base branch that need to be incorporated, you may need to:

  1. Manually rebase the branch, or
  2. Let me know if you'd like me to recreate the changes on top of the latest base branch instead

Copilot finished work on behalf of ramseymcgrath November 16, 2025 05:32
@ramseymcgrath ramseymcgrath marked this pull request as ready for review November 21, 2025 07:43
Copilot AI review requested due to automatic review settings November 21, 2025 07:43
Copilot finished reviewing on behalf of ramseymcgrath November 21, 2025 07:45
Copy link
Contributor

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

This PR adds defensive programming to prevent potential KeyError exceptions when accessing MSI-X capability information, along with import path corrections and comprehensive test coverage.

Key Changes

  • Added multi-layered defensive checks before accessing msix_info["table_size"] in the preload_data method to handle None values, non-dict types, missing keys, and zero table sizes
  • Corrected import statements from from string_utils import to from src.string_utils import across 4 files to fix import errors
  • Added 3 unit tests covering edge cases: None msix_info, missing table_size key, and zero table_size

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/device_clone/msix.py Added comprehensive defensive checks (None, type, key existence, value validation) before accessing msix_info dictionary
src/cli/vfio_handler.py Corrected import path to use from src.string_utils import
src/file_management/option_rom_manager.py Corrected import path to use from src.string_utils import
src/utils/post_build_validator.py Corrected import path to use from src.string_utils import
src/vivado_handling/vivado_utils.py Corrected import path to use from src.string_utils import
tests/test_msix_manager.py Added 3 unit tests validating defensive behavior for None, missing keys, and zero table_size scenarios
Comments suppressed due to low confidence (1)

src/file_management/option_rom_manager.py:22

  • Import of 'log_debug_safe' is not used.
from src.string_utils import (
    log_debug_safe,
    log_error_safe,
    log_info_safe,
    log_warning_safe,
    safe_format,
)

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

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