From: Robert Spalek Date: Sun, 23 Apr 2006 10:51:54 +0000 (+0200) Subject: conf2: verified and fixed all parsers X-Git-Tag: holmes-import~645^2~11^2~68 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=2cd3ad1b86f0d4d69864b574c0f7102fa619ba37;p=libucw.git conf2: verified and fixed all parsers --- diff --git a/lib/conf2-test.c b/lib/conf2-test.c index 5a38355d..99b9b0a8 100644 --- a/lib/conf2-test.c +++ b/lib/conf2-test.c @@ -68,6 +68,7 @@ static double d1 = -1.1; static struct sub_sect_1 sec1 = { {}, "Charlie", "WBAFC", { 0, -1} }; static struct clist secs; static time_t t1, t2; +static u32 ip; static byte * init_top(void *ptr UNUSED) @@ -112,6 +113,7 @@ static struct cf_section cf_top = { CF_PARSER("SecondTime", &t2, time_parser, 1), CF_SECTION("master", &sec1, &cf_sec_1), CF_LIST("slaves", &secs, &cf_sec_1), + CF_IP("ip", &ip), CF_END } }; diff --git a/lib/conf2.c b/lib/conf2.c index 07513f5d..43529e38 100644 --- a/lib/conf2.c +++ b/lib/conf2.c @@ -396,7 +396,6 @@ lookup_unit(byte *value, byte *end, byte **msg) static char cf_rngerr[] = "Number out of range"; -//FIXME: parsers should handle well empty strings, unwanted suffixes etc. byte * cf_parse_int(byte *str, int *ptr) { @@ -463,12 +462,11 @@ cf_parse_double(byte *str, double *ptr) msg = "Missing number"; else { const struct unit *u; - char *end; - errno = 0; - double x = strtoul(str, &end, 0); - if (errno == ERANGE) - msg = cf_rngerr; - else if (u = lookup_unit(str, end, &msg)) + double x; + uns read_chars; + if (sscanf(str, "%lf%n", &x, &read_chars) != 1) + msg = "Invalid number"; + else if (u = lookup_unit(str, str + read_chars, &msg)) *ptr = x * u->num / u->den; else *ptr = x; @@ -482,11 +480,13 @@ cf_parse_ip(byte *p, u32 *varp) if (!*p) return "Missing IP address"; uns x = 0; + char *p2; if (*p == '0' && p[1] | 32 == 'X') { errno = 0; - x = strtoul(p + 2, NULL, 16); - if (errno == ERANGE || x > 0xffffffff) + x = strtoul(p + 2, &p2, 16); + if (errno == ERANGE || p2 == (char*) (p+2) || x > 0xffffffff) goto error; + p = p2; } else for (uns i = 0; i < 4; i++) { @@ -495,15 +495,14 @@ cf_parse_ip(byte *p, u32 *varp) goto error; } errno = 0; - char *p2; uns y = strtoul(p, &p2, 10); - p = p2; - if (errno == ERANGE || y > 255) + if (errno == ERANGE || p2 == (char*) p || y > 255) goto error; + p = p2; x = (x << 8) + y; } *varp = x; - return NULL; + return *p ? "Trailing characters" : NULL; error: return "Invalid IP address"; } @@ -1279,8 +1278,8 @@ dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type) { switch (type) { case CT_INT: bprintf(fb, "%d ", *(uns*)ptr); break; - case CT_U64: bprintf(fb, "%lld ", *(u64*)ptr); break; - case CT_DOUBLE: bprintf(fb, "%lf ", *(double*)ptr); break; + case CT_U64: bprintf(fb, "%llu ", *(u64*)ptr); break; + case CT_DOUBLE: bprintf(fb, "%lg ", *(double*)ptr); break; case CT_IP: bprintf(fb, "%08x ", *(uns*)ptr); break; case CT_STRING: bprintf(fb, "'%s' ", *(byte**)ptr); break; } diff --git a/lib/conf2.t b/lib/conf2.t index 7bdbb43a..836c9fd0 100644 --- a/lib/conf2.t +++ b/lib/conf2.t @@ -13,11 +13,13 @@ Top { \ the best' "\ " qu'est-ce que c'est? u1 0xbadcafebadbeefc0 - #d1 -1.14e-25 d1 7% + d1 -1.14e-25 firsttime ; secondtime 56 ^top.master:set alice HB8+ slaves:clear + ip 0xa + ip 195.113.31.123 };;;;;; unknown.ignored :-) @@ -35,5 +37,5 @@ top.slaveS:before {level pum}{ } topp.a=15 -top.nr1= 15 +top.nr1= ' 15' a { ;-D }