ash: Expand here-documents in the current shell environment
[oweals/busybox.git] / libbb / time.c
index 82e6cb172db04efe81a48ba2f840308ab0da8ed9..e66a9cba8438746722ff43e9eecdc9a721095205 100644 (file)
@@ -184,6 +184,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
                        ptm->tm_year -= 1900; /* Adjust years */
                        ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
                } else {
+ err:
                        bb_error_msg_and_die(bb_msg_invalid_date, date_str);
                }
                ptm->tm_sec = 0; /* assume zero if [.SS] is not given */
@@ -194,6 +195,19 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
                                end = '\0';
                        /* else end != NUL and we error out */
                }
+               /* Users were confused by "date -s 20180923"
+                * working (not in the way they were expecting).
+                * It was interpreted as MMDDhhmm, and not bothered by
+                * "month #20" in the least. Prevent such cases:
+                */
+               if (ptm->tm_sec > 60 /* allow "23:60" leap second */
+                || ptm->tm_min > 59
+                || ptm->tm_hour > 23
+                || ptm->tm_mday > 31
+                || ptm->tm_mon > 11 /* month# is 0..11, not 1..12 */
+               ) {
+                       goto err;
+               }
        }
        if (end != '\0') {
                bb_error_msg_and_die(bb_msg_invalid_date, date_str);
@@ -239,12 +253,10 @@ char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp)
 #define CLOCK_MONOTONIC 1
 #endif
 
-/* libc has incredibly messy way of doing this,
- * typically requiring -lrt. We just skip all this mess */
 static void get_mono(struct timespec *ts)
 {
-       if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
-               bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
+       if (clock_gettime(CLOCK_MONOTONIC, ts))
+               bb_simple_error_msg_and_die("clock_gettime(MONOTONIC) failed");
 }
 unsigned long long FAST_FUNC monotonic_ns(void)
 {