You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/abalkin'closed_at=<Date2010-10-01.14:23:55.817>created_at=<Date2009-07-30.23:56:47.278>labels= ['extension-modules', 'easy', 'OS-windows', 'type-crash']
title='asctime does not check its input'updated_at=<Date2010-10-01.14:23:55.815>user='https://bugs.python.org/AmirHabibi'
Since there's no good way to disable the assertion (see bpo-4804),
checking the validity of the argument beforehand looks like an option.
The checking that's currently being done in the strftime()
implementation looks useful but it is not enough. The checking in the
MS implementation of asctime() is very strict and validates the entire
date, not just one field at a time. So there's no way to print out
non-existant dates like (2009, 2, 31, 0, 0, 0, 0, 0, 0) -> 'Mon Feb 31
00:00:00 2009'.
I don't know if anybody is relying on that kind of behavior. If not
then the function could be limited to accept only valid dates.
Alternatively we could just not call down to asctime() but instead
provide our own implementation using sprintf/strftime.
abbeyj commented on Aug 2, 2009
abbeyjmannequin
Mannequin
More actions
Further investigation shows that MS asctime() doesn't like leap seconds
and causes an assertion when passing (2008, 12, 31, 23, 59, 60, 2, 366,
-1) -> 'Wed Dec 31 23:59:60 2008'.
Given that and since asctime() is such a simple function I think it
makes more sense to roll our own. Attached is a rough patch which does
this. It pulls the bounds checking used in strftime() out into its own
function so it can be used in both places. Overriding ctime() is
necessary because Windows decided to use " %02d" instead of "%3d" to
print the day of the month. Without overriding ctime() the output of
ctime(t) would be different from asctime(localtime(t)) and cause
test_conversion() to fail. There is a USE_SYSTEM_ASCTIME switch to
decide whether to use the system asctime() or the internal one.
I have untabified James' patch, ran the tests on the result, but did not otherwise review it.
There is a not-so-easy-to-find thread on python-dev discussing this issue under subject "Python 2.6.5".
Here is a relevant quote from Martin v. Löwis:
"""
That would require that Barry actually *can* judge the issue at hand. In
the specific case, I would expect that Barry would defer the specifics
of the Windows issue to Windows experts, and then listen to what they
say.
I'm personally split whether the proposed patch is correct (i.e. whether
asctime really *can* be implemented in a cross-platform manner; any
definite ruling on that would be welcome). In the past, we had rather
taken approaches like disabling runtime assertions "locally"; not sure
whether such approaches would work for asctime as well.
In any case, I feel that the issue is not security-critical at all.
People just don't pass out-of-range values to asctime, but instead
typically pass the result of gmtime/localtime, which will not cause any
problems.
"""
That's what CERT recommends. Their code can be reused as is:
intvalidate_tm(structtm*time) {
/* * The range of valid values of the tm_sec member is [0, 60] * inclusive (to allow for leap seconds). */if (time->tm_sec<0||time->tm_sec>60) return0;
if (time->tm_min<0||time->tm_min >= 60) return0;
if (time->tm_hour<0||time->tm_hour >= 24) return0;
if (time->tm_mday <= 0||time->tm_mday>31) return0;
if (time->tm_mon<0||time->tm_mon >= 12) return0;
/* While other years are legit, they may overflow asctime()'s buffer */if (time->tm_year<-999||time->tm_year>9999) return0;
if (time->tm_wday<0||time->tm_wday >= 7) return0;
if (time->tm_yday<0||time->tm_yday >= 366) return0;
return1;
}
I've just noticed that time_strftime already has the range checks for tm structure fields. These checks can be separated in a function and shared with asctime. Marking this as "easy". See also bpo-897625.
The patch looks good to me. Just a few nitpicks: the local convention seems to be no underscores in helper functions such as gettmarg. I would call is_valid_tm, "checktm" instead. Also, predicate-like naming (is_...) suggests a function without side-effects, while is_valid_tm sets an error when it gets an invalid tm. Also, please add missing comments above gettmarg and is_valid_tm. Finally, please start the function body with a "{" on a new line. Thank you for your submission.
ruseel commented on Sep 24, 2010
ruseelmannequin
Mannequin
More actions
Thank you for a kind guidance.
I uploaded patch with
function name "checktm"
add comment above gettmarg
move comment in "checktm" to (above function signature)
Your patch does not apply cleanly to the py3k branch. Please not that new features can only go to py3k. While some may argue that this issue is a bug, I think it is more likely that it will only be accepted for 3.2. In any case, a py3k patch should be the starting point. Could you post a patch agains the py3k branch?
Sorry for many "Added file" and "Removed file" mails. I am learning customs Trac now.
ruseel commented on Oct 1, 2010
ruseelmannequin
Mannequin
More actions
belopolsky
I am little worried about my words. I made patch against 2.7-maint branch. But I recognized that patch is not following rules, because of your guidance. Then I uploaded another patch against py3k branch again. and bpo-6608-p3k.patch could be applied to py3k branch cleanly. If there were any boldness in my words, I'm sorry.
and I'm ready to address any thing left in this issue. :)
Activity
AmirHabibi commentedon Jul 30, 2009
I use:
Python 2.6.2
(r262:71605, Apr 14 2009, 22:40:02)
[MSC v.1500 32 bit (Intel)] on win32
the 24 is a wrong parameter but it should'n't crash the engine. This may
be the side effect of a more serious problem though. An assertion may
fix it.
avassalotti commentedon Jul 31, 2009
Confirmed. On Windows, the out-of-range value triggers a debugging
assertion in the CRT library.
abbeyj commentedon Aug 1, 2009
Since there's no good way to disable the assertion (see bpo-4804),
checking the validity of the argument beforehand looks like an option.
The checking that's currently being done in the strftime()
implementation looks useful but it is not enough. The checking in the
MS implementation of asctime() is very strict and validates the entire
date, not just one field at a time. So there's no way to print out
non-existant dates like (2009, 2, 31, 0, 0, 0, 0, 0, 0) -> 'Mon Feb 31
00:00:00 2009'.
I don't know if anybody is relying on that kind of behavior. If not
then the function could be limited to accept only valid dates.
Alternatively we could just not call down to asctime() but instead
provide our own implementation using sprintf/strftime.
abbeyj commentedon Aug 2, 2009
Further investigation shows that MS asctime() doesn't like leap seconds
and causes an assertion when passing (2008, 12, 31, 23, 59, 60, 2, 366,
-1) -> 'Wed Dec 31 23:59:60 2008'.
Given that and since asctime() is such a simple function I think it
makes more sense to roll our own. Attached is a rough patch which does
this. It pulls the bounds checking used in strftime() out into its own
function so it can be used in both places. Overriding ctime() is
necessary because Windows decided to use " %02d" instead of "%3d" to
print the day of the month. Without overriding ctime() the output of
ctime(t) would be different from asctime(localtime(t)) and cause
test_conversion() to fail. There is a USE_SYSTEM_ASCTIME switch to
decide whether to use the system asctime() or the internal one.
pitrou commentedon Feb 11, 2010
Retrograding to critical after popular python-dev demand.
abalkin commentedon May 26, 2010
I have untabified James' patch, ran the tests on the result, but did not otherwise review it.
There is a not-so-easy-to-find thread on python-dev discussing this issue under subject "Python 2.6.5".
Here is a relevant quote from Martin v. Löwis:
"""
That would require that Barry actually *can* judge the issue at hand. In
the specific case, I would expect that Barry would defer the specifics
of the Windows issue to Windows experts, and then listen to what they
say.
I'm personally split whether the proposed patch is correct (i.e. whether
asctime really *can* be implemented in a cross-platform manner; any
definite ruling on that would be welcome). In the past, we had rather
taken approaches like disabling runtime assertions "locally"; not sure
whether such approaches would work for asctime as well.
In any case, I feel that the issue is not security-critical at all.
People just don't pass out-of-range values to asctime, but instead
typically pass the result of gmtime/localtime, which will not cause any
problems.
"""
12 remaining items
avassalotti commentedon Jun 11, 2010
How about checking the preconditions before calling asctime()? If the check fails, then we can raise an exception without crashing.
abalkin commentedon Jun 11, 2010
That's what CERT recommends. Their code can be reused as is:
abalkin commentedon Jun 25, 2010
I've just noticed that time_strftime already has the range checks for tm structure fields. These checks can be separated in a function and shared with asctime. Marking this as "easy". See also bpo-897625.
ruseel commentedon Sep 23, 2010
As alexandre.vassalotti pointed in msg107596, I added precondition check.
extracted the range check from time_strftime as "is_valid_tm".
time_asctime, time_strftime call "is_valid_tm"
testcase for both asctime and strftime (abbeyj's work)
I splited patch just for unittest files(Lib/test/test_time.py) and (Modules/timemodule.c)
abalkin commentedon Sep 23, 2010
The patch looks good to me. Just a few nitpicks: the local convention seems to be no underscores in helper functions such as gettmarg. I would call is_valid_tm, "checktm" instead. Also, predicate-like naming (is_...) suggests a function without side-effects, while is_valid_tm sets an error when it gets an invalid tm. Also, please add missing comments above gettmarg and is_valid_tm. Finally, please start the function body with a "{" on a new line. Thank you for your submission.
ruseel commentedon Sep 24, 2010
Thank you for a kind guidance.
I uploaded patch with
abalkin commentedon Sep 24, 2010
MunSic,
Your patch does not apply cleanly to the py3k branch. Please not that new features can only go to py3k. While some may argue that this issue is a bug, I think it is more likely that it will only be accepted for 3.2. In any case, a py3k patch should be the starting point. Could you post a patch agains the py3k branch?
abalkin commentedon Sep 24, 2010
Hmm, it looks like bpo-6608-timemodule-2nd.patch is a patch on top of your previous patches. I'll check if I can apply last three patches in order.
abalkin commentedon Sep 24, 2010
No, it looks like the starting patch is against 2.7. We need a py3k patch. Meanwhile, I've noticed a couple of grammatical mistakes in comments:
"strftime(), asctime() does not" -> "strftime() and asctime() do not"
"fixes bug bpo-897625, bpo-6608" -> "fixes bugs bpo-897625 and bpo-6608"
ruseel commentedon Sep 25, 2010
I uploaded a patch against py3k branch.
Sorry for many "Added file" and "Removed file" mails. I am learning customs Trac now.
ruseel commentedon Oct 1, 2010
belopolsky
I am little worried about my words. I made patch against 2.7-maint branch. But I recognized that patch is not following rules, because of your guidance. Then I uploaded another patch against py3k branch again. and bpo-6608-p3k.patch could be applied to py3k branch cleanly. If there were any boldness in my words, I'm sorry.
and I'm ready to address any thing left in this issue. :)
abalkin commentedon Oct 1, 2010
Committed with minor changes in r85137. Thanks for the patch!