Skip to content

[BUG] CustomPoint has potential cast exception at runtime #1522

@josxha

Description

@josxha

What is the bug?

While working on greensopinion/flutter-vector-map-tiles#149 i discovered that the current CustomPoint implementation lead to a cast exception at runtime.

This behaviour is documented in the parent class math.Point but isn't mentioned in CustomPoint:

/// Important Note: This function accepts a num as its argument only so
/// that you can scale Point<double> objects by an int factor. Because the
/// * operator always returns the same type of Point as it is called on,
/// passing in a double [factor] on a Point<int> causes a
/// runtime error.

How can we reproduce it?

final pDouble = CustomPoint<double>(1, 2);
final pInt = CustomPoint<int>(3,4);
var point;

point = pInt + pDouble; // runtime exception
point = pInt + pDouble; // runtime exception
point = pInt * pDouble; // runtime exception
point = pInt / pDouble; // runtime exception

Potentially this would throw an exception too:

point = CustomPoint<int, int>(5, 5) / CustomPoint<int, int>(2, 2); // runtime exception

Do you have a potential solution?

The issue is that other may be a num or double and turns the result into a double while the function casts it to an int.

For example here:

  /// Create new [CustomPoint] where [x] and [y] values are added to [other]
  /// point [x] and [y] values
  @override
  CustomPoint<T> operator +(math.Point other) {
    return CustomPoint<T>((x + other.x) as T, (y + other.y) as T);
  }

Possible solutions:

  • Add documentation to the dangerous functions like it's done in math.Point. This could lead to potential errors for users but might not be that problematic since CustomPoint is mostly used internally or by plugin developers.
  • force other to be the same type as the CustomPoint instance. This requires transforming every working double/int compination to be converted to a double/double.
  • Use named functions like:
CustomPoint<T> multiplyWithInt(CustomPoint<int> other) {} // int/int, double/int
CustomPoint<double> multiplyWithDouble(CustomPoint<double> other) {} // double/double, int/double

Platforms

tested on android but should

Severity

Erroneous: Prevents normal functioning and causes errors in the console

Metadata

Metadata

Assignees

No one assigned

    Labels

    P: 2 (soon™?)bugThis issue reports broken functionality or another error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions