Skip to content

Ordering[os.Path] impl is broken. Trivial fix included. #44

@ohamel-softwaresecure

Description

@ohamel-softwaresecure
implicit val pathOrdering: Ordering[Path] = new Ordering[Path]{
    def compare(x: Path, y: Path): Int = {
      val xSegCount = x.segmentCount
      val ySegCount = y.segmentCount
      if (xSegCount < ySegCount) -1
      else if (xSegCount > ySegCount) 1
      else if (xSegCount == 0 && ySegCount == 0) 0
      else{
        var xSeg = ""
        var ySeg = ""
        var i = -1                      // FIX: `i = 0`    (`xSegCount != 0` due to checks above)
        do{
          i += 1                        // broke here.@  `i == xSegCount == x.segmentCount` 
          xSeg = x.getSegment(i)        // `x.getSegment(x.segmentCount)`  -> out-of-bounds exception
          ySeg = y.getSegment(i)
                                        // FIX: `i += 1` here
        } while (i < xSegCount && xSeg == ySeg)
        if (i == xSegCount) 0
        else Ordering.String.compare(xSeg, ySeg)
      }
    }
  }

Or for { i <- 0 until xSegCount } { val cmp = x.getSegment(i) compare y.getSegment(i); if (cmp != 0) return cmp }.
EDIT: Or use the java.lang.Comparable of java.nio.file.Path...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions