glusterfs icon indicating copy to clipboard operation
glusterfs copied to clipboard

cli: fix timestamp validation

Open eggert opened this issue 1 year ago • 5 comments

cli/src/cli-cmd-parser.c's config_parse was calling strftime with a struct tm that may have tm_year that is out of range for strftime, yielding unspecified and/or undefined behavior. Fix this by using mktime instead of strftime, as mktime's behavior is well-defined for out-of-range values. This also fixes a portability issue with strftime %s and time zones.

Fixes: #4355

eggert avatar May 23 '24 21:05 eggert

Can one of the admins verify this patch?

gluster-ant avatar May 23 '24 21:05 gluster-ant

Can one of the admins verify this patch?

gluster-ant avatar May 23 '24 21:05 gluster-ant

Can one of the admins verify this patch?

gluster-ant avatar May 23 '24 21:05 gluster-ant

CLANG-FORMAT FAILURE: Before merging the patch, this diff needs to be considered for passing clang-format

index e39f60ed8..ef12ab894 100644
--- a/cli/src/cli-cmd-parser.c
+++ b/cli/src/cli-cmd-parser.c
@@ -2594,7 +2594,7 @@ config_parse(const char **words, int wordcount, dict_t *dict, unsigned cmdi,
                 memset(&checkpoint_time, 0, sizeof(struct tm));
                 ret_chkpt = strptime(append_str, "%Y-%m-%d %H:%M:%S",
                                      &checkpoint_time);
-                checkpoint_sec = mktime (&checkpoint_time);
+                checkpoint_sec = mktime(&checkpoint_time);
 
                 if (ret_chkpt == NULL || *ret_chkpt != '\0' ||
                     checkpoint_time.tm_mday == 0) {
@@ -2610,7 +2610,7 @@ config_parse(const char **words, int wordcount, dict_t *dict, unsigned cmdi,
                     ret = -1;
                     goto out;
                 }
-                snprintf(append_str, 300, "%lld", (long long) checkpoint_sec);
+                snprintf(append_str, 300, "%lld", (long long)checkpoint_sec);
             }
 
             ret = dict_set_dynstr(dict, "op_value", append_str);

gluster-ant avatar May 23 '24 21:05 gluster-ant

I found an extra bug in the code while re-reviewing my patch: there's an off-by-an-hour bug when daylight saving is in effect. This bug is also present in the original unmodified code in the devel branch, as strftime %s has the same issue with tm_isdst that mktime does. I pushed commit 444a9497a30574accdc8b2055eee1e81ce3fdc9d to fix this extra bug.

eggert avatar May 24 '24 04:05 eggert

/run regression

amarts avatar Oct 21 '24 18:10 amarts

1 test(s) failed ./tests/00-geo-rep/00-georep-verify-non-root-setup.t

0 test(s) generated core

1 test(s) needed retry ./tests/00-geo-rep/00-georep-verify-non-root-setup.t https://build.gluster.org/job/gh_centos7-regression/3423/

gluster-ant avatar Oct 21 '24 19:10 gluster-ant