diff options
author | Nick Mathewson <nickm@torproject.org> | 2015-01-07 10:06:50 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2015-01-07 10:06:50 -0500 |
commit | cb54cd6745eaf15716aa357fc253f43f26c051a6 (patch) | |
tree | 8637fbe20d722885eb295aee4f5f0ac86683854f /src/common | |
parent | 49dca8b1beae9c2dade2c3193fa7f77f5c15d869 (diff) | |
parent | 7984fc153112baa5c370215f2205025a7648d7b4 (diff) | |
download | tor-cb54cd6745eaf15716aa357fc253f43f26c051a6.tar.gz tor-cb54cd6745eaf15716aa357fc253f43f26c051a6.zip |
Merge branch 'bug9286_v3_squashed'
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.c | 23 | ||||
-rw-r--r-- | src/common/util.h | 1 |
2 files changed, 19 insertions, 5 deletions
diff --git a/src/common/util.c b/src/common/util.c index 1de5edc52c..e5dec99707 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1704,15 +1704,18 @@ format_iso_time_nospace_usec(char *buf, const struct timeval *tv) /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>, * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on - * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from - * the end of the time string. */ + * failure. Ignore extraneous stuff in <b>cp</b> after the end of the time + * string, unless <b>strict</b> is set. */ int -parse_iso_time(const char *cp, time_t *t) +parse_iso_time_(const char *cp, time_t *t, int strict) { struct tm st_tm; unsigned int year=0, month=0, day=0, hour=0, minute=0, second=0; - if (tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u", &year, &month, - &day, &hour, &minute, &second) < 6) { + int n_fields; + char extra_char; + n_fields = tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u%c", &year, &month, + &day, &hour, &minute, &second, &extra_char); + if (strict ? (n_fields != 6) : (n_fields < 6)) { char *esc = esc_for_log(cp); log_warn(LD_GENERAL, "ISO time %s was unparseable", esc); tor_free(esc); @@ -1741,6 +1744,16 @@ parse_iso_time(const char *cp, time_t *t) return tor_timegm(&st_tm, t); } +/** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>, + * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on + * failure. Reject the string if any characters are present after the time. + */ +int +parse_iso_time(const char *cp, time_t *t) +{ + return parse_iso_time_(cp, t, 1); +} + /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh), * parse it into <b>tm</b>. Return 0 on success, negative on failure. */ int diff --git a/src/common/util.h b/src/common/util.h index a1da53890e..ee40949b61 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -274,6 +274,7 @@ void format_local_iso_time(char *buf, time_t t); void format_iso_time(char *buf, time_t t); void format_iso_time_nospace(char *buf, time_t t); void format_iso_time_nospace_usec(char *buf, const struct timeval *tv); +int parse_iso_time_(const char *cp, time_t *t, int strict); int parse_iso_time(const char *buf, time_t *t); int parse_http_time(const char *buf, struct tm *tm); int format_time_interval(char *out, size_t out_len, long interval); |