https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129238

            Bug ID: 129238
           Summary: <filesystem>: corrupt NTFS modification time seems to
                    cause over/undeflow in file_time_type
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: namark at disroot dot org
  Target Milestone: ---

I have a removable drive with NTFS filesystem that got bunch of errors due to
sudden power loss, and one of the consequences are bogus modification
timestamps on some files like this:

  File: /media/namark/TOSHIBA
EXT/$RECYCLE.BIN/S-1-5-21-2639692302-3212337132-2512655831-1000/$RX3RLZK/M2U02491.MPG
  Size: 425525248       Blocks: 831104     IO Block: 4096   regular file
Device: 831h/2097d      Inode: 11289       Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/  namark)   Gid: ( 1000/  namark)
Access: 2026-11-14 21:07:12.172403200 +0400
Modify: 1777-03-03 22:10:10.000000000 +0258
Change: 2016-10-07 15:24:51.963176600 +0400
 Birth: -

Testing my homegrown directory sync program that relies on last_write_time to
detect updates on these produced some unexpected conflicts, where this file
that was apparently modified in 1777 was reported newer than a copy of it I
created yesterday on another drive with ext4. I narrowed the issue down to the
following minimal program:

#include <filesystem>
#include <iostream>
#include <chrono> // 128830

int main(int argc, char const* argv[])
{
    auto a = std::filesystem::last_write_time(argv[1]);
    auto b = std::filesystem::last_write_time(argv[2]);
    auto a_count = a.time_since_epoch().count();
    auto b_count = b.time_since_epoch().count();

    auto cmp_str = a > b ? " > " : " <= ";
    std::cout << a << cmp_str <<  b << '\n';
    std::cout << a_count << cmp_str << b_count << '\n';
    return 0;
}

which when run with the bad file as the first argument and a normal file as the
second outputs something like this:
1777-03-03 19:12:10.709551616 > 2026-12-02 12:39:47.581130630
5923997203709551616 > -4641448812418869370

>From what I understand the file_clock's epoch is somewhere 100+ years in the
future, so the positive count suggests that there was an underflow, and I
suppose cout then adds another offset to it and overflows it back to where it
was to output the original 1777 date, or some such confusion. 

Rare edge case I suppose, and not sure how much of this is on the standard and
how much is left for the implementation to decide, but thought to report it
anyway.
  • [Bug libstdc++/129238] New: <fi... namark at disroot dot org via Gcc-bugs

Reply via email to