[llvm-bugs] [Bug 47125] New: year_month_day_last::day() has undefined behavior and should not

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 11 17:38:11 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=47125

            Bug ID: 47125
           Summary: year_month_day_last::day() has undefined behavior and
                    should not
           Product: libc++
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: howard.hinnant at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

Convenience link: http://eel.is/c++draft/time.cal.ymdlast.members#15

Example program:

    #include <chrono>

    int
    main()
    {
        using namespace std::chrono;
        auto constexpr ymdl = 2021y/13/last;
        auto constexpr d = ymdl.day();
    }

Expected result:  To compile.

Actual result:

Jade-2:~/Development/cljunk> clang++ -std=c++2a test.cpp -O3
-I../libcxx/include -nostdinc++
test.cpp:8:20: error: constexpr variable 'd' must be initialized by a constant
expression
    auto constexpr d = ymdl.day();
                   ^   ~~~~~~~~~~
../libcxx/include/chrono:2457:9: note: read of dereferenced one-past-the-end
pointer is not allowed in a constant expression
        __d[static_cast<unsigned>(month()) - 1] : chrono::day{29};
        ^
../libcxx/include/chrono:2457:9: note: in call to 'day(__d[12])'
test.cpp:8:29: note: in call to '&ymdl->day()'
    auto constexpr d = ymdl.day();
                            ^
1 error generated.

----

Cause of error:  The year_month_day_last contains a month that is out of range
of the internal array that is indexed by month.  This is not a user error. 
This should compile and return an unspecified day.

I see two plausible directions for a fix:

1.  Maximal performance:  If !this->ok(), take the branch that does not index
and return day{29}.

2.  Maximal debugability:  If !this->ok() return a day such that !d.ok(). 
Maybe day{0} or day{32}.

The example implementation at https://github.com/HowardHinnant/date chose (1). 
But I think either of these choices are reasonable.  Field experience with the
example implementation has not provided a definitely preferred answer.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200812/6403819e/attachment.html>


More information about the llvm-bugs mailing list