Bug #4457

Time#strftime で %z 指定子などに大きな幅を指定した際の不具合

Added by tadayoshi funaba about 3 years ago. Updated over 2 years ago.

[ruby-dev:43285]
Status:Closed
Priority:Normal
Assignee:Akira Tanaka
Category:core
Target version:1.9.3
ruby -v:- Backport:

Description

=begin
大きな幅を指定すると結果が空になる事があります。

$ ruby -e "p Time.now.strftime('%100000z')"
""
$ ruby -e "p Time.now.strftime('%10z')"
"+000000900"

=end


Related issues

Related to ruby-trunk - Bug #4456: Time#strftime で %F 指定子に大きな幅を指定した際の不具合 Closed 03/02/2011

Associated revisions

Revision 32885
Added by Motohiro KOSAKI over 2 years ago

  • time.c (rbstrftimealloc): raise ERANGE if width is too large.
    Patch by Nobuyoshi Nakada. [Bug #4457]

  • test/ruby/test_time.rb (class TestTime): add a test for the
    above change.

History

#1 Updated by Yui NARUSE almost 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Akira Tanaka
  • Target version set to 1.9.3

#2 Updated by Motohiro KOSAKI almost 3 years ago

  • Category set to core

こちら、状況いかがでしょうか?

#3 Updated by Nobuyoshi Nakada almost 3 years ago

  • ruby -v changed from ruby 1.9.3dev (2011-03-02) [i686-linux] to -

なかだです。

At Thu, 21 Jul 2011 13:57:30 +0900,
Motohiro KOSAKI wrote in :

こちら、状況いかがでしょうか?

できなければ例外、ですかねぇ。

diff --git a/test/ruby/testtime.rb b/test/ruby/testtime.rb
index 38e567a..1f07a82 100644
--- a/test/ruby/testtime.rb
+++ b/test/ruby/test
time.rb
@@ -650,6 +650,9 @@ class TestTime < Test::Unit::TestCase

  # 
  assert_equal("3000000000", Time.at(3000000000).strftime('%s'))

+
+ bug4457 = ''
+ assert_raise(Errno::ERANGE, bug4457) {Time.now.strftime('%8192z')}
end

def test_delegate
Modified time.c

diff --git a/time.c b/time.c
index dd846a6..6817c75 100644
--- a/time.c
+++ b/time.c
@@ -4325,8 +4325,12 @@ rbstrftimealloc(char *buf, const char *format,
* if the buffer is 1024 times bigger than the length of the
* format string, it's not failing for lack of room.
*/
- if (len > 0 || size >= 1024 * flen) break;
+ if (len > 0) break;
xfree(
buf);
+ if (size >= 1024 * flen) {
+ rbsysfail(format);
+ break;
+ }
}
return len;
}

--
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
中田 伸悦

#4 Updated by Motohiro KOSAKI over 2 years ago

akrさん、nakadaさんのパッチはコミットしてしまっていいのでしょうか?

#5 Updated by Akira Tanaka over 2 years ago

とりあえずいいような気がします。

しかし、考えてみると strftime の API が腐っているという問題があるわけで、
そして、Ruby 1.9 では OS の strftime を使っていないのだから、
その API につきあう必要はないわけです。

というわけで、バッファが足りなかったときには必要な長さを返すようにするとか
まともな API を用意して使うのがいい気がしますね。

#6 Updated by Motohiro KOSAKI over 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r32885.
tadayoshi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • time.c (rbstrftimealloc): raise ERANGE if width is too large.
    Patch by Nobuyoshi Nakada. [Bug #4457]

  • test/ruby/test_time.rb (class TestTime): add a test for the
    above change.

Also available in: Atom PDF