date icon indicating copy to clipboard operation
date copied to clipboard

Undefined behavior

Open Roman-Koshelev opened this issue 4 years ago • 2 comments

const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12] This line is UB since -9 is converted to unsigned? Or am I wrong?

Roman-Koshelev avatar Aug 31 '21 08:08 Roman-Koshelev

I presume you're referring to civil_from_days. I should change that to match what is actually in date.h:

https://github.com/HowardHinnant/date/blob/master/include/date/date.h#L3101

HowardHinnant avatar Aug 31 '21 13:08 HowardHinnant

I just ran this:

int
main()
{
    for (auto m = 1; m <= 12; ++m)
    {
        for (auto d = 1; d <= last_day_of_month_common_year(m); ++d)
        {
            auto z = days_from_civil(2021, m, d);
            auto [y1, m1, d1] = civil_from_days(z);
            std::cout << y1 << '-' << m1 << '-' << d1 << '\n';
        }
    }
}

Using:

clang++ test.cpp -std=c++17 -I../date/include -Wall -fsanitize=undefined

And it ran with no errors or warnings. I'm concluding that there is no undefined behavior on this line.

HowardHinnant avatar Sep 01 '21 00:09 HowardHinnant