Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Annotate nullability on FirefoxDriverService #15220

Merged
merged 4 commits into from
Feb 3, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 3, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Annotate nullability on FirefoxDriverService

Motivation and Context

Contributes to #14640

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement


Description

  • Annotated nullability for properties and parameters in FirefoxDriverService.

  • Simplified property definitions using auto-implemented properties.

  • Enabled nullable reference types with #nullable enable.

  • Refactored command-line argument generation for better readability.


Changes walkthrough 📝

Relevant files
Enhancement
FirefoxDriverService.cs
Annotated nullability and refactored `FirefoxDriverService`.

dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

  • Enabled nullable reference types with #nullable enable.
  • Annotated properties and parameters with nullable types.
  • Replaced backing fields with auto-implemented properties.
  • Refactored logic for command-line argument generation.
  • +29/-59 

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    qodo-merge-pro bot commented Feb 3, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Null Reference

    The GetDirectoryName call on line 203 assumes the path has a directory component and uses the null-forgiving operator (!), but this assumption should be validated to prevent runtime errors.

    driverPath = Path.GetDirectoryName(driverPath)!;

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 3, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    ✅ Add null check for path
    Suggestion Impact:The commit implemented a null check for Path.GetDirectoryName() using null-coalescing operator with throw, achieving the same goal as the suggestion but with slightly different syntax

    code diff:

    -                driverPath = Path.GetDirectoryName(driverPath)!;
    +                driverPath = Path.GetDirectoryName(driverPath) ?? throw new ArgumentException("Driver file exists but parent directory is unreachable", nameof(driverPath));

    Add null check for Path.GetDirectoryName() result since it can return null for
    invalid paths. The current forced null-forgiving operator (!) could lead to
    NullReferenceException.

    dotnet/src/webdriver/Firefox/FirefoxDriverService.cs [203]

    -driverPath = Path.GetDirectoryName(driverPath)!;
    +var dirPath = Path.GetDirectoryName(driverPath);
    +if (dirPath == null) throw new ArgumentException("Invalid driver path provided", nameof(driverPath));
    +driverPath = dirPath;
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a potential null reference exception by properly handling the case where Path.GetDirectoryName returns null, which is a critical safety check for path operations.

    8
    General
    Validate port number range

    Add input validation for BrowserCommunicationPort to ensure it's within valid
    port range (1-65535) when setting a positive value.

    dotnet/src/webdriver/Firefox/FirefoxDriverService.cs [64]

    -public int BrowserCommunicationPort { get; set; } = -1;
    +private int _browserCommunicationPort = -1;
    +public int BrowserCommunicationPort
    +{
    +    get => _browserCommunicationPort;
    +    set
    +    {
    +        if (value > 0 && (value < 1 || value > 65535))
    +            throw new ArgumentOutOfRangeException(nameof(value), "Port must be between 1 and 65535");
    +        _browserCommunicationPort = value;
    +    }
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion adds important validation to ensure port numbers are within valid range (1-65535), preventing potential runtime errors from invalid port assignments.

    7

    @RenderMichael
    Copy link
    Contributor Author

    Failures are not related to changes in this PR:

    //java/test/org/openqa/selenium/mobile:NetworkConnectionTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest-firefox-beta

    @RenderMichael RenderMichael merged commit 773f9e0 into SeleniumHQ:trunk Feb 3, 2025
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the ff-driver-service branch February 3, 2025 20:13
    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Mar 23, 2025
    …5220)
    
    * [dotnet] Annotate nullability on `FirefoxDriverService`
    
    * Replace null suppress with exception
    
    * Bring back null suppression
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants