Browse Source

[trac929] add more strict check for date and time format (add reverse check)

Naoki Kambe 13 years ago
parent
commit
93a7f7d149

+ 8 - 1
src/lib/config/module_spec.cc

@@ -103,8 +103,15 @@ check_format(ConstElementPtr value, ConstElementPtr format_name) {
     BOOST_FOREACH (const format_types::value_type& f, time_formats) {
         if (format_name->stringValue() == f.first) {
             struct tm tm;
+            char buf[255] = "";
+            memset(&tm, 0, sizeof(tm));
+            // reverse check
             return (strptime(value->stringValue().c_str(),
-                             f.second.c_str(), &tm) != NULL);
+                             f.second.c_str(), &tm) != NULL
+                    && strftime(buf, sizeof(buf),
+                                f.second.c_str(), &tm) != 0
+                    && strcmp(value->stringValue().c_str(),
+                              buf) == 0);
         }
     }
     return (false);

+ 1 - 1
src/lib/config/tests/module_spec_unittests.cc

@@ -287,7 +287,7 @@ TEST(ModuleSpec, CheckFormat) {
     item_default = "\"item_default\": \"2011-05-27\",";
     item_format  = "\"item_format\": \"date\"";
     specs.push_back("," + item_default + item_format);
-    item_default = "\"item_default\": \"19:42:57Z\",";
+    item_default = "\"item_default\": \"19:42:57\",";
     item_format  = "\"item_format\": \"time\"";
     specs.push_back("," + item_default + item_format);
 

+ 4 - 2
src/lib/python/isc/config/module_spec.py

@@ -330,8 +330,10 @@ def _check_format(value, format_name):
     for fmt in time_formats:
         if format_name == fmt:
             try:
-                time.strptime(value, time_formats[fmt])
-                return True
+                # reverse check
+                return value == time.strftime(
+                    time_formats[fmt],
+                    time.strptime(value, time_formats[fmt]))
             except (ValueError, TypeError):
                 break
     return False