This is an archive of the discontinued LLVM Phabricator instance.

Changes to mktime to handle invalid dates, overflow and underflow andcalculating the correct date and thenumber of seconds even if invalid datesare passed as arguments.
ClosedPublic

Authored by rtenneti on Feb 14 2021, 8:57 PM.

Details

Summary

Added tests for invalid dates like the following

Date 1970-01-01 00:00:-1 is treated as 1969-12-31 23:59:59 and seconds
are returned for the modified date.

Tested the code by doing ninja check-libc (and cmake).

Diff Detail

Event Timeline

rtenneti created this revision.Feb 14 2021, 8:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2021, 8:57 PM
rtenneti requested review of this revision.Feb 14 2021, 8:57 PM
rtenneti retitled this revision from Changes to mktime to handle invalid dates, overflow and underflow and calculating the correct date and thenumber of seconds even if invalid dates are passed as arguments. to Changes to mktime to handle invalid dates, overflow and underflow andcalculating the correct date and thenumber of seconds even if invalid datesare passed as arguments..Feb 16 2021, 10:15 AM
rtenneti added a reviewer: stanshebs.
rtenneti added a subscriber: jeffbailey.
sivachandra added inline comments.Feb 16 2021, 12:58 PM
libc/src/time/mktime.cpp
16–17

You can probably move these constants also to the class TimeConstants suggested above..

libc/src/time/mktime.h
17 ↗(On Diff #323662)

The implementation header should not leak any implementation details. These constants here aren't really implementation details, but lets use a different approach:

  1. Create a header only library by name time_utils with a header file time_utils.h. You have done that already so I am essentially suggesting a reorg of the file.
  2. Use a nested namespace __llvm_libc::time_utils.
  3. In that file, create a struct by name __llvm_libc::time_utils::TimeConstants and list these constants as static members. You should do the same with the other constants you have listed in time_utils.cpp.
27 ↗(On Diff #323662)

Likewise, you should make this function static inline and define it in time_utils.h in the nested namespace __llvm_libc::time_utils.

libc/src/time/time_utils.cpp
39 ↗(On Diff #323662)

Is there a reason why this should be in a separate object file? Can this be not be listed as static in mktime.cpp?

libc/test/src/time/mktime_test.cpp
20–21

You can define this constant in time_utils.h in the class TimeConstants as:

namespace __llvm_libc {
namespace time_utils {

struct TimeConstants {
  ...
  // If more functions beyond mktime will use this return value, then don't add the "MkTime" prefix.
  static constexpr time_t MkTimeOutOfRangeReturnValue = -1;
};

} // namespace time_utils
} // namespace __llvm_libc
49

Ideally, we should implement a matcher : https://github.com/llvm/llvm-project/blob/main/libc/utils/UnitTest/LibcTest.h#L55
Example matcher: https://github.com/llvm/llvm-project/blob/main/libc/test/ErrnoSetterMatcher.h#L22.

A matcher will help us show better failure messages.

rtenneti updated this revision to Diff 324538.Feb 17 2021, 11:35 PM
rtenneti marked 6 inline comments as done.

Made all the changes Siva suggested.

rtenneti updated this revision to Diff 324541.Feb 17 2021, 11:50 PM

Made OutOfRange a static inline.

rtenneti updated this revision to Diff 324670.Feb 18 2021, 9:07 AM

Updated the comments for the years computation algorithm.

sivachandra accepted this revision.Feb 19 2021, 8:42 PM

Please run clang-format if you have not already done so.

libc/src/time/mktime.h
27 ↗(On Diff #323662)

Remove this?

libc/test/src/time/TmMatcher.h
18

At other places, we followed this convention:

namespace __llvm_libc {
namespace time_utils {
namespace testing {
}
}
}

It is a bit of verbose nesting but the macro hides the verbosity.

libc/test/src/time/mktime_test.cpp
66

Do we need this helper function? Wouldn't a pattern like this work:

EXPECT_TM_EQ(tm_data, tm{
                                ...
                               });
This revision is now accepted and ready to land.Feb 19 2021, 8:42 PM
rtenneti updated this revision to Diff 325271.Feb 20 2021, 7:24 PM
rtenneti marked 3 inline comments as done.

Fixed comments - Deleted check_mktime function and replaced it
with initializing tm struct and EXPECT_TM_EQ macro.

rtenneti accepted this revision.Feb 20 2021, 7:26 PM

arc diff command ran Lint and clang-format and fixed the formatting.

Lint for libc/test/src/time/mktime_test.cpp:

Auto-Fix  (S&RX) Lint
 clang-format suggested style edits found: