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][bidi] Underlying local/remote script number as double #15301

Merged
merged 2 commits into from
Feb 19, 2025

Conversation

nvborisenko
Copy link
Member

@nvborisenko nvborisenko commented Feb 18, 2025

User description

Motivation and Context

Numbers are not only int, it should be double in .NET

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

Bug fix


Description

  • Changed Number type from long to double in LocalValue and RemoteValue.

  • Updated explicit and implicit operators to handle double type.

  • Ensured consistency in handling numeric values across BiDi modules.


Changes walkthrough 📝

Relevant files
Bug fix
LocalValue.cs
Update `Number` type to `double` in `LocalValue`                 

dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs

  • Changed Number type from long to double.
  • Updated explicit operator to accept double.
  • +2/-2     
    RemoteValue.cs
    Update `Number` type to `double` in `RemoteValue`               

    dotnet/src/webdriver/BiDi/Modules/Script/RemoteValue.cs

  • Changed Number type from long to double.
  • Adjusted implicit operator to handle double to long conversion.
  • +2/-2     

    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

    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

    Potential Precision Loss

    The implicit conversion from double to int/long could result in data loss or precision issues when handling decimal numbers. Consider adding validation or explicit conversion handling.

    public static implicit operator int(RemoteValue remoteValue) => (int)((Number)remoteValue).Value;
    public static implicit operator long(RemoteValue remoteValue) => (long)((Number)remoteValue).Value;

    @nvborisenko nvborisenko changed the title [dotnet][bidi] Underlying locacl/remote script number as double [dotnet][bidi] Underlying local/remote script number as double Feb 18, 2025
    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 18, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Prevent numeric overflow in conversions

    Add explicit bounds checking when casting from double to long/int to prevent
    silent overflow or precision loss

    dotnet/src/webdriver/BiDi/Modules/Script/RemoteValue.cs [58-59]

    -public static implicit operator int(RemoteValue remoteValue) => (int)((Number)remoteValue).Value;
    -public static implicit operator long(RemoteValue remoteValue) => (long)((Number)remoteValue).Value;
    +public static implicit operator int(RemoteValue remoteValue)
    +{
    +    var value = ((Number)remoteValue).Value;
    +    if (value < int.MinValue || value > int.MaxValue)
    +        throw new OverflowException("Number value is outside the range of Int32");
    +    return (int)value;
    +}
    +public static implicit operator long(RemoteValue remoteValue)
    +{
    +    var value = ((Number)remoteValue).Value;
    +    if (value < long.MinValue || value > long.MaxValue)
    +        throw new OverflowException("Number value is outside the range of Int64");
    +    return (long)value;
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    __

    Why: The suggestion adds critical bounds checking to prevent silent overflow when converting from double to int/long, which could lead to data corruption or unexpected behavior. This is especially important since the PR changes the Number type from long to double.

    High
    Learned
    best practice
    Add parameter validation in conversion operators to prevent null reference exceptions and provide clear error messages

    The implicit operators for converting RemoteValue to int/long should validate
    that the input parameter is not null and that it can be cast to Number type
    before accessing the Value property, to prevent potential
    NullReferenceException.

    dotnet/src/webdriver/BiDi/Modules/Script/RemoteValue.cs [58-59]

    -public static implicit operator int(RemoteValue remoteValue) => (int)((Number)remoteValue).Value;
    -public static implicit operator long(RemoteValue remoteValue) => (long)((Number)remoteValue).Value;
    +public static implicit operator int(RemoteValue remoteValue)
    +{
    +    ArgumentNullException.ThrowIfNull(remoteValue);
    +    if (remoteValue is not Number number)
    +        throw new InvalidCastException($"Cannot convert {remoteValue.GetType()} to Number");
    +    return (int)number.Value;
    +}
    +public static implicit operator long(RemoteValue remoteValue)
    +{
    +    ArgumentNullException.ThrowIfNull(remoteValue);
    +    if (remoteValue is not Number number)
    +        throw new InvalidCastException($"Cannot convert {remoteValue.GetType()} to Number");
    +    return (long)number.Value;
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 6
    Low
    • Update

    @nvborisenko nvborisenko merged commit 029ee43 into SeleniumHQ:trunk Feb 19, 2025
    9 of 10 checks passed
    @nvborisenko nvborisenko deleted the bidi-number-double branch February 20, 2025 10:38
    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.

    1 participant