1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-16 18:13:49 +01:00

shell/rtc: use rtc_tm_normalize() to sanitize input

This commit is contained in:
Benjamin Valentin 2023-01-29 22:18:39 +01:00
parent 49d549cce5
commit 349b8b8367

View File

@ -27,6 +27,7 @@
#include "container.h" #include "container.h"
#include "periph/rtc.h" #include "periph/rtc.h"
#include "rtc_utils.h"
#include "shell.h" #include "shell.h"
static void _alarm_handler(void *arg) static void _alarm_handler(void *arg)
@ -36,19 +37,6 @@ static void _alarm_handler(void *arg)
puts("The alarm rang"); puts("The alarm rang");
} }
static int dow(int year, int month, int day)
{
/* calculate the day of week using Tøndering's algorithm */
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
if (month < 1 || month > (int) ARRAY_SIZE(t)) {
/* This will be a wrong answer, but error handling is not this
* function's task (whereas memory safety is). */
return 7;
}
year -= month < 3;
return (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}
/** Read a ["YYYY-MM-DD", "hh:mm:ss"] formatted value from a string array. /** Read a ["YYYY-MM-DD", "hh:mm:ss"] formatted value from a string array.
* *
* This performs no validation on the entered time -- that'd be trivial on some * This performs no validation on the entered time -- that'd be trivial on some
@ -58,7 +46,7 @@ static int dow(int year, int month, int day)
* *
* Invalid inputs merely lead to out-of-range values inside the time struct. * Invalid inputs merely lead to out-of-range values inside the time struct.
*/ */
static int _parse_time(char **argv, struct tm *time) static void _parse_time(char **argv, struct tm *time)
{ {
short i; short i;
char *end; char *end;
@ -81,10 +69,9 @@ static int _parse_time(char **argv, struct tm *time)
i = strtol(end + 1, &end, 10); i = strtol(end + 1, &end, 10);
time->tm_sec = i; time->tm_sec = i;
time->tm_wday = dow(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday);
time->tm_isdst = -1; /* undefined */ time->tm_isdst = -1; /* undefined */
return 0; rtc_tm_normalize(time);
} }
static int _print_time(struct tm *time) static int _print_time(struct tm *time)
@ -113,14 +100,14 @@ static int _rtc_setalarm(char **argv)
{ {
struct tm now; struct tm now;
if (_parse_time(argv, &now) == 0) { _parse_time(argv, &now);
if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) {
if (rtc_set_alarm(&now, _alarm_handler, NULL) < 0) {
puts("rtc: error setting alarm"); puts("rtc: error setting alarm");
return 1; return 1;
} }
return 0; return 0;
}
return 1;
} }
static int _rtc_gettime(void) static int _rtc_gettime(void)
@ -140,14 +127,14 @@ static int _rtc_settime(char **argv)
{ {
struct tm now; struct tm now;
if (_parse_time(argv, &now) == 0) { _parse_time(argv, &now);
if (rtc_set_time(&now) == -1) {
if (rtc_set_time(&now) < 0) {
puts("rtc: error setting time"); puts("rtc: error setting time");
return 1; return 1;
} }
return 0; return 0;
}
return 1;
} }
static int _rtc_usage(void) static int _rtc_usage(void)