Issue 242733
Summary DiffFilesWithTolerance fails a number that is a textual prefix of the other, whatever the tolerance
Labels new issue
Assignees
Reporter hamidelmaazouz
    Hi all,

I ran into a corner of `llvm::DiffFilesWithTolerance` (in `llvm/lib/Support/FileUtilities.cpp`) that looks like a long-standing bug. When two numbers differ in their number of digits and the shorter one is followed by whitespace, the comparison fails even though the tolerance admits the difference. `llvm/utils/fpcmp` and bugpoint both go through this routine, so both see it.

Here is a small reproduction with `llvm/utils/fpcmp`, built from main at 0212d77fa269 against LLVM 22. The two versions of `FileUtilities.cpp` differ only in `FilePermissionsApplier::apply`.

```console
$ printf '0.25\n' > a; printf '0.2500\n' > b
$ fpcmp -r 1 a b; echo $?
fpcmp: FP Comparison failed, not a numeric difference between ' ' and '0'
1
$ printf '0.25\n' > a; printf '0.25000000000001\n' > b
$ fpcmp -r 1e-6 a b; echo $?
fpcmp: FP Comparison failed, not a numeric difference between ' ' and '0'
1
$ printf '3.\n' > a; printf '3\n' > b
$ fpcmp -r 1 a b; echo $?
fpcmp: FP Comparison failed, not a numeric difference between '3' and ' '
1
```

Interestingly, the same pair passes when it sits at the very end of the file, so `printf '0.25'` against `printf '0.2500'` exits 0. So does `0.25` against `0.2600`, where both streams stop inside a number.

As far as I can tell, the cause is in how the two streams are realigned. The scan stops at the first differing character, and `BackupNumber` moves a stream back to the start of its number only if that character is itself part of a number. The stream whose number has already ended stays on the whitespace after it, while the other moves back to the start of its own number. `CompareNumbers` then skips the whitespace and parses from different positions in the two files, or finds no number at all. The end-of-file path already steps back one character for exactly this case, which explains why the pair at the end of a file passes.

The copy of this code in llvm-test-suite had the same problem. It was reported as bz15620 (#15992) and fixed there in 2013 by 0e3504ec4, "fpcmp catching different number of digits". llvm-test-suite later replaced the backtracking with forward parsing in D99619 (8a84a507d, 2021). It seems neither change made its way into `llvm/lib/Support`.

Two possible fixes come to mind. One is to bring over the 2013 change, stepping a stream back by one when it stopped on whitespace just after a number. The other is to port the forward parsing from D99619. I'd be glad to help with either if that's useful.

Thanks for taking a look!
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to