From 09e061aef77e3fa88a281ae6afa671e726a8671b Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Thu, 6 Feb 2020 12:51:24 -0800 Subject: [PATCH] make ISO8601 duration parsing case-insensitive for robustness --- lib/isccfg/parser.c | 21 +++++++++++---------- lib/isccfg/tests/duration_test.c | 18 +++++++++--------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/isccfg/parser.c b/lib/isccfg/parser.c index e114fb984a..db3c327768 100644 --- a/lib/isccfg/parser.c +++ b/lib/isccfg/parser.c @@ -11,6 +11,7 @@ /*! \file */ +#include #include #include #include @@ -1180,16 +1181,16 @@ duration_fromtext(isc_textregion_t *source, cfg_duration_t *duration) { } /* Every duration starts with 'P' */ - P = strchr(str, 'P'); + P = strpbrk(str, "Pp"); if (P == NULL) { return (ISC_R_BADNUMBER); } /* Record the time indicator. */ - T = strchr(str, 'T'); + T = strpbrk(str, "Tt"); /* Record years. */ - X = strchr(str, 'Y'); + X = strpbrk(str, "Yy"); if (X != NULL) { duration->parts[0] = atoi(str+1); str = X; @@ -1197,7 +1198,7 @@ duration_fromtext(isc_textregion_t *source, cfg_duration_t *duration) { } /* Record months. */ - X = strchr(str, 'M'); + X = strpbrk(str, "Mm"); /* * M could be months or minutes. This is months if there is no time @@ -1210,7 +1211,7 @@ duration_fromtext(isc_textregion_t *source, cfg_duration_t *duration) { } /* Record days. */ - X = strchr(str, 'D'); + X = strpbrk(str, "Dd"); if (X != NULL) { duration->parts[3] = atoi(str+1); str = X; @@ -1224,7 +1225,7 @@ duration_fromtext(isc_textregion_t *source, cfg_duration_t *duration) { } /* Record hours. */ - X = strchr(str, 'H'); + X = strpbrk(str, "Hh"); if (X != NULL && T != NULL) { duration->parts[4] = atoi(str+1); str = X; @@ -1232,7 +1233,7 @@ duration_fromtext(isc_textregion_t *source, cfg_duration_t *duration) { } /* Record minutes. */ - X = strrchr(str, 'M'); + X = strpbrk(str, "Mm"); /* * M could be months or minutes. This is minutes if there is a time @@ -1245,7 +1246,7 @@ duration_fromtext(isc_textregion_t *source, cfg_duration_t *duration) { } /* Record seconds. */ - X = strchr(str, 'S'); + X = strpbrk(str, "Ss"); if (X != NULL && T != NULL) { duration->parts[6] = atoi(str+1); str = X; @@ -1253,7 +1254,7 @@ duration_fromtext(isc_textregion_t *source, cfg_duration_t *duration) { } /* Or is the duration configured in weeks? */ - W = strchr(buf, 'W'); + W = strpbrk(buf, "Ww"); if (W != NULL) { if (not_weeks) { /* Mix of weeks and other indicators is not allowed */ @@ -1280,7 +1281,7 @@ parse_duration(cfg_parser_t *pctx, cfg_obj_t **ret) { duration.unlimited = false; - if (TOKEN_STRING(pctx)[0] == 'P') { + if (toupper(TOKEN_STRING(pctx)[0]) == 'P') { result = duration_fromtext(&pctx->token.value.as_textregion, &duration); duration.iso8601 = true; diff --git a/lib/isccfg/tests/duration_test.c b/lib/isccfg/tests/duration_test.c index 2e0249bb07..a70d4697f3 100644 --- a/lib/isccfg/tests/duration_test.c +++ b/lib/isccfg/tests/duration_test.c @@ -110,24 +110,24 @@ cfg_obj_asduration_test(void **state) { duration_conf_t durations[] = { { .string = "PT0S", .time = 0 }, { .string = "PT42S", .time = 42 }, - { .string = "PT10M", .time = 600 }, - { .string = "PT10M4S", .time = 604 }, - { .string = "PT2H", .time = 7200 }, - { .string = "PT2H3S", .time = 7203 }, - { .string = "PT2H1M3S", .time = 7263 }, - { .string = "P7D", .time = 604800 }, - { .string = "P7DT2H", .time = 612000 }, + { .string = "PT10m", .time = 600 }, + { .string = "PT10m4S", .time = 604 }, + { .string = "pT2H", .time = 7200 }, + { .string = "Pt2H3S", .time = 7203 }, + { .string = "PT2h1m3s", .time = 7263 }, + { .string = "p7d", .time = 604800 }, + { .string = "P7DT2h", .time = 612000 }, { .string = "P2W", .time = 1209600 }, { .string = "P3M", .time = 8035200 }, { .string = "P3MT10M", .time = 8035800 }, - { .string = "P5Y", .time = 157680000 }, + { .string = "p5y", .time = 157680000 }, { .string = "P5YT2H", .time = 157687200 }, { .string = "P1Y1M1DT1H1M1S", .time = 34304461 }, { .string = "0", .time = 0 }, { .string = "30", .time = 30 }, { .string = "42s", .time = 42 }, { .string = "10m", .time = 600 }, - { .string = "2h", .time = 7200 }, + { .string = "2H", .time = 7200 }, { .string = "7d", .time = 604800 }, { .string = "2w", .time = 1209600 }, };