Skip to content

[Bug]: SRID 4326 measurements accept out-of-range longitude and latitude #28191

Closed

Description

@Ariznawlll

Description

SRID 4326 的 ST_Distance、ST_Length 和 ST_Area 未验证 longitude/latitude 的有效范围。经度 181°、纬度 91° 会被 S2 球面内核规范化后返回看似有效的距离、长度和面积,而不是拒绝无效地理坐标。

同一代码库中的 ST_Distance_Sphere 已明确要求 longitude [-180,180]、latitude [-90,90],但 SRID 4326 的通用测量路径绕过了该校验。

Environment

  • Branch: main
  • Commit: e7bb0572235ec4bac81eb098eb0ad8900f0065ab
  • Deployment: macOS arm64,单节点 MatrixOne,默认 launch 配置
  • Date: 2026-09-05

Steps to reproduce

SELECT ST_Distance(
  ST_GeomFromText('POINT(181 0)', 4326),
  ST_GeomFromText('POINT(0 0)', 4326)
);

SELECT ST_Distance(
  ST_GeomFromText('POINT(0 91)', 4326),
  ST_GeomFromText('POINT(0 0)', 4326)
);

SELECT ST_Length(ST_GeomFromText('LINESTRING(0 0,181 0)', 4326));

SELECT ST_Area(
  ST_GeomFromText('POLYGON((0 0,1 0,1 91,0 91,0 0))', 4326)
);

-- control: the sibling spherical-distance function rejects the same domains
SELECT ST_Distance_Sphere(ST_GeomFromText('POINT(181 0)'), ST_GeomFromText('POINT(0 0)'));
SELECT ST_Distance_Sphere(ST_GeomFromText('POINT(0 91)'), ST_GeomFromText('POINT(0 0)'));

Actual behavior

distance from POINT(181 0) = 19903919.361802418
distance from POINT(0 91)  = 10118752.301251495
length                     = 19903919.361802388
area                       = 708316943627.4152

ST_Distance_Sphere longitude 181 -> explicit out-of-range error
ST_Distance_Sphere latitude 91   -> explicit out-of-range error

Expected behavior

SRID 4326 的 longitude 必须在 [-180,180],latitude 必须在 [-90,90],且坐标必须有限。越界坐标不应被球面内核隐式折叠到另一个地理位置;构造或进入 geodetic measurement kernel 时应返回明确错误。

Stability and controls

  • 四个越界测量场景各重复 3 次,返回数值稳定。
  • 合法控制:赤道 1° 距离约 111195.08 m;反经线 179° -> -179° 取约 222390.16 m 的短弧;顺/逆时针 1° 方形面积相同。
  • ST_Distance_Sphere 对完全相同的 181°/91° 越界域稳定拒绝,证明已有可复用的范围规则。
  • NaN WKT 被存储校验拒绝,问题集中在“有限但越界”的坐标。

Evidence and code analysis

  • Exact main commit: e7bb057
  • Geodetic area/length wrappers decode and call the S2-backed kernel without coordinate validation:
    // geodeticArea returns the WGS 84 geodesic area (square meters) of a polygon or
    // multipolygon.
    func geodeticArea(payload []byte) (float64, error) {
    typeName, err := geometryTypeNameFromPayload(payload)
    if err != nil {
    return 0, err
    }
    if typeName != "POLYGON" && typeName != "MULTIPOLYGON" {
    return 0, moerr.NewInvalidInputNoCtx("geometry is not a POLYGON or MULTIPOLYGON")
    }
    g, err := decodeGeoGeometry(payload)
    if err != nil {
    return 0, err
    }
    return geo.AreaSquareMeters(g), nil
    }
    // geodeticLength returns the WGS 84 geodesic length (meters) of a linestring or
    // multilinestring.
    func geodeticLength(payload []byte) (float64, error) {
    typeName, err := geometryTypeNameFromPayload(payload)
    if err != nil {
    return 0, err
    }
    if typeName != "LINESTRING" && typeName != "MULTILINESTRING" {
    return 0, moerr.NewInvalidInputNoCtx("geometry is not a LINESTRING or MULTILINESTRING")
    }
    g, err := decodeGeoGeometry(payload)
    if err != nil {
    return 0, err
    }
    return geo.LengthMeters(g), nil
  • SRID 4326 distance dispatch likewise has no coordinate-domain guard:
    // geometryDistanceBySRID computes the distance between two geometries in the
    // coordinate system selected by srid: geodesic meters for SRID 4326, Cartesian
    // otherwise.
    func geometryDistanceBySRID(left, right []byte, srid uint32) (float64, error) {
    if err := validateComputationSRID(srid); err != nil {
    return 0, err
    }
    if srid == geo.SRIDWGS84 {
    return geodeticDistance(left, right)
    }
    return geometryDistance(left, right)
  • ST_Distance_Sphere range validation already implemented:
    return 0, moerr.NewInvalidInputNoCtx("invalid geometry payload")
    }
    return T(d), nil
    }, selectList)
    }
    // validateDistanceSphereGeometry enforces the ST_Distance_Sphere input contract:
    // only POINT and MULTIPOINT geometries are accepted, and every coordinate must
    // lie within longitude [-180, 180] and latitude [-90, 90].
    func validateDistanceSphereGeometry(g geo.Geometry) error {
    checkCoord := func(x, y float64) error {
    if x < -180 || x > 180 {
    return moerr.NewInvalidInputNoCtxf("ST_DISTANCE_SPHERE longitude %v is out of range [-180, 180]", x)
    }
    if y < -90 || y > 90 {
    return moerr.NewInvalidInputNoCtxf("ST_DISTANCE_SPHERE latitude %v is out of range [-90, 90]", y)
    }
    return nil
    }
    switch v := g.(type) {
    case geo.Point:
    if v.IsEmpty {
    return nil
    }
    return checkCoord(v.X, v.Y)
    case geo.MultiPoint:
    for _, p := range v.Points {
    if p.IsEmpty {
    continue
    }
    if err := checkCoord(p.X, p.Y); err != nil {
    return err
    }
    }
    return nil
    default:
    return moerr.NewInvalidInputNoCtx("ST_DISTANCE_SPHERE only supports POINT and MULTIPOINT inputs")
    }

Regression coverage

修复时应覆盖 Point/LineString/Polygon/Multi* 的合法边界、刚越界、NaN/Inf;覆盖 ST_Distance、ST_Length、ST_Area 的类型 SRID 与显式 SRID overload,以及 geography 列、常量、表列和 Prepared Statement 路径。

Activity

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. XuPeng-SH commented on Sep 13, 2026

    @XuPeng-SH
    Contributor

    修复已提交为 PR #28815,commit 5f6603fe2bc6c26b5e8da52d5c935a04f57e8fcb。

    Root cause: SRID 4326 的 ST_Distance、ST_Length、ST_Area 把坐标交给 S2;s2.LatLngFromDegrees 会规范化有限但越界的经纬度,measurement wrapper 之前没有在 S2 前拒绝它们。因此查询会返回看似有效但错误的数值。基线 commit 67a9a4b130388956971888a67fc626784deade48 上,issue 的四个 SQL-evaluator regression 都稳定返回成功而非错误。

    **修复边界:*新增共享经纬度遍历校验,在有效 SRID 为 4326 的 area/length/distance 内核调用前执行;Distance 左右操作数在 containment/零距离 shortcut 前都完整校验。坐标必须有限,longitude 在 [-180,180]、latitude 在 [-90,90]。显式 SRID 仍覆盖列/值类型:SRID 0 仍走 Cartesian;构造/存储不提前拒绝坐标;ST_DISTANCE_SPHERE 继续使用其独立 Point/MultiPoint 契约。覆盖 GEOMETRY32、holes/Multi、NULL/masked/zero-row、empty sentinel、边界和早退反例。

    验证:pkg/geo 全包 UT、pkg/sql/plan/function 全包 CGo UT、go vet、增量 golangci-lint(0 issues)、gofmt 和 diff 检查均通过。临时 M4 microbenchmark 测得校验额外耗时约 point distance 1%、4096 点 line length 5.3%、4096 点 polygon area 2%,额外分配为 0。新增 SQL BVT 覆盖常量、显式 SRID、GEOGRAPHY/GEOGRAPHY32 列及 prepared valid→invalid→valid;没有针对其他版本的本地服务运行 BVT,需由 PR CI 验证。创建 PR 时 CI preflight 已通过,shared build 正在运行,UT/SCA 已排队;尚未等待 CI 结果。

    GPT‑6 medium 设计门和最终精确 diff 源码审查均为 PASS(最终 diff SHA-256 9cdf1bfe3e78670737fea26ff9c8b86285910f62e11ab281a48c3729500f292e;调用配置 gpt-6-astra, medium,审查 agent 不能独立认证运行时变体/effort)。请在 exact-head CI/BVT 通过后再验收并关闭 issue。

  6. added a commit that references this issue on Sep 13, 2026
    3698ade
  7. Ariznawlll commented on Sep 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    原问题验证

    • SRID 4326 度量会拒绝有限但越界的经纬度。

    Happy Path 覆盖

    • 合法赤道 1° distance=111195。

    Unhappy Path 覆盖

    • longitude 181 的 distance/length 与 latitude 91 的 area 均返回 20301。

    白盒验证

    回归测试

    测试证据

    • 独立环境三轮输出字节一致;服务日志未见 panic/hang,测试后已清理服务。

    测试结论

    测试通过。

  8. added 3 commits that reference this issue on Sep 17, 2026
    a5f3e7b
    49de19b
    72824a0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions