Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: fix timestamp validation #4356

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from

Conversation

eggert
Copy link

@eggert eggert commented May 23, 2024

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

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: gluster#4355

Signed-off-by: Paul Eggert <[email protected]>
@gluster-ant
Copy link
Collaborator

Can one of the admins verify this patch?

2 similar comments
@gluster-ant
Copy link
Collaborator

Can one of the admins verify this patch?

@gluster-ant
Copy link
Collaborator

Can one of the admins verify this patch?

@gluster-ant
Copy link
Collaborator

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);

Adjust patch to pass clang-format.

Fixes: gluster#4355

Signed-off-by: Paul Eggert <[email protected]>
Don't tell mktime that standard time is in effect.
This fixes a bug where the timestamp is off by
an hour when daylight saving time is in effect.
(The issue is also present in the devel branch,
which uses strftime %s and which therefore
has the same bug.)
@eggert
Copy link
Author

eggert commented May 24, 2024

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 444a949 to fix this extra bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

time parsing issues in cli-cmd-parser.c config_parse
2 participants