HowardHinnant / date

A date and time library based on the C++11/14/17 <chrono> header
Other
3.08k stars 670 forks source link

Undefined behavior #694

Open Roman-Koshelev opened 2 years ago

Roman-Koshelev commented 2 years ago

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

HowardHinnant commented 2 years ago

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 commented 2 years ago

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.