From 813b3bd6d0f24cb8ac4410e0da0160b39748d9fb Mon Sep 17 00:00:00 2001 From: Florian Westphal Date: Sat, 3 Mar 2018 10:57:54 +0100 Subject: src: datatype: prefer sscanf, avoid strncpy similar to previous patch, but replace strncpy+atoi by sscanf. Signed-off-by: Florian Westphal --- src/datatype.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src/datatype.c') diff --git a/src/datatype.c b/src/datatype.c index 324ac802..446bde9f 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -854,21 +854,20 @@ enum { SECS = (1 << 3), }; -static uint32_t str2int(char *tmp, const char *c, int k) +static uint32_t str2int(const char *str) { - if (k == 0) - return 0; + int ret, number; - strncpy(tmp, c-k, k+1); - return atoi(tmp); + ret = sscanf(str, "%d", &number); + return ret == 1 ? number : 0; } struct error_record *time_parse(const struct location *loc, const char *str, uint64_t *res) { + unsigned int max_digits = strlen("12345678"); int i, len; unsigned int k = 0; - char tmp[8]; const char *c; uint64_t d = 0, h = 0, m = 0, s = 0; uint32_t mask = 0; @@ -882,7 +881,7 @@ struct error_record *time_parse(const struct location *loc, const char *str, return error(loc, "Day has been specified twice"); - d = str2int(tmp, c, k); + d = str2int(c - k); k = 0; mask |= DAY; break; @@ -891,7 +890,7 @@ struct error_record *time_parse(const struct location *loc, const char *str, return error(loc, "Hour has been specified twice"); - h = str2int(tmp, c, k); + h = str2int(c - k); k = 0; mask |= HOUR; break; @@ -900,7 +899,7 @@ struct error_record *time_parse(const struct location *loc, const char *str, return error(loc, "Minute has been specified twice"); - m = str2int(tmp, c, k); + m = str2int(c - k); k = 0; mask |= MIN; break; @@ -909,7 +908,7 @@ struct error_record *time_parse(const struct location *loc, const char *str, return error(loc, "Second has been specified twice"); - s = str2int(tmp, c, k); + s = str2int(c - k); k = 0; mask |= SECS; break; @@ -917,7 +916,7 @@ struct error_record *time_parse(const struct location *loc, const char *str, if (!isdigit(*c)) return error(loc, "wrong time format"); - if (k++ >= array_size(tmp)) + if (k++ >= max_digits) return error(loc, "value too large"); break; } -- cgit v1.2.3