touch: make invalid date an error and fix uninitialised value

reproduce: touch -t 12345678 mytralala

would only give a warning and use uninitialised time values. The proper
way is to treat it as an error. Clear the "struct tm" and kill some lines
just in case.
This commit is contained in:
Hiltjo Posthuma 2016-04-15 20:44:32 +02:00 committed by sin
parent d89cd48118
commit 8d96afd4fb

View File

@ -48,7 +48,7 @@ touch(const char *file)
static time_t static time_t
parsetime(char *str, time_t current) parsetime(char *str, time_t current)
{ {
struct tm *cur, t; struct tm *cur, t = { 0 };
int zulu = 0; int zulu = 0;
char *format; char *format;
size_t len = strlen(str); size_t len = strlen(str);
@ -59,12 +59,10 @@ parsetime(char *str, time_t current)
switch (len) { switch (len) {
/* -t flag argument */ /* -t flag argument */
case 8: case 8:
t.tm_sec = 0;
t.tm_year = cur->tm_year; t.tm_year = cur->tm_year;
format = "%m%d%H%M"; format = "%m%d%H%M";
break; break;
case 10: case 10:
t.tm_sec = 0;
format = "%y%m%d%H%M"; format = "%y%m%d%H%M";
break; break;
case 11: case 11:
@ -72,7 +70,6 @@ parsetime(char *str, time_t current)
format = "%m%d%H%M.%S"; format = "%m%d%H%M.%S";
break; break;
case 12: case 12:
t.tm_sec = 0;
format = "%Y%m%d%H%M"; format = "%Y%m%d%H%M";
break; break;
case 13: case 13:
@ -98,7 +95,7 @@ parsetime(char *str, time_t current)
} }
if (!strptime(str, format, &t)) if (!strptime(str, format, &t))
weprintf("strptime %s: Invalid date format\n", str); eprintf("strptime %s: Invalid date format\n", str);
if (zulu) { if (zulu) {
t.tm_hour += t.tm_gmtoff / 60; t.tm_hour += t.tm_gmtoff / 60;
t.tm_gmtoff = 0; t.tm_gmtoff = 0;