]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
make ISO8601 duration parsing case-insensitive for robustness
authorEvan Hunt <each@isc.org>
Thu, 6 Feb 2020 20:51:24 +0000 (12:51 -0800)
committerMatthijs Mekking <github@pletterpet.nl>
Fri, 7 Feb 2020 18:17:05 +0000 (19:17 +0100)
lib/isccfg/parser.c
lib/isccfg/tests/duration_test.c

index e114fb984a60fc1ba6b79eb4df699fe91bbd4f92..db3c327768d32bb3f90689c3f9adef7469b46f2d 100644 (file)
@@ -11,6 +11,7 @@
 
 /*! \file */
 
+#include <ctype.h>
 #include <inttypes.h>
 #include <stdbool.h>
 #include <stdlib.h>
@@ -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;
index 2e0249bb0703ad417508bc72c3624a6b9a68d151..a70d4697f3c7571f6b8063d78b3bf27eab1a3520 100644 (file)
@@ -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 },
        };