[#25936] [Bug:1.9] [rubygems] $LOAD_PATH includes bin directory — Nobuyoshi Nakada <nobu@...>

Hi,

10 messages 2009/10/05

[#25943] Disabling tainting — Tony Arcieri <tony@...>

Would it make sense to have a flag passed to the interpreter on startup that

16 messages 2009/10/05

[#26028] [Bug #2189] Math.atanh(1) & Math.atanh(-1) should not raise an error — Marc-Andre Lafortune <redmine@...>

Bug #2189: Math.atanh(1) & Math.atanh(-1) should not raise an error

14 messages 2009/10/10

[#26222] [Bug #2250] IO::for_fd() objects' finalization dangerously closes underlying fds — Mike Pomraning <redmine@...>

Bug #2250: IO::for_fd() objects' finalization dangerously closes underlying fds

11 messages 2009/10/22

[#26244] [Bug #2258] Kernel#require inside rb_require() inside rb_protect() inside SysV context fails — Suraj Kurapati <redmine@...>

Bug #2258: Kernel#require inside rb_require() inside rb_protect() inside SysV context fails

24 messages 2009/10/22

[#26361] [Feature #2294] [PATCH] ruby_bind_stack() to embed Ruby in coroutine — Suraj Kurapati <redmine@...>

Feature #2294: [PATCH] ruby_bind_stack() to embed Ruby in coroutine

42 messages 2009/10/27

[#26371] [Bug #2295] segmentation faults — tomer doron <redmine@...>

Bug #2295: segmentation faults

16 messages 2009/10/27

[ruby-core:26353] Re: [Bug #2283] Ruby 1.9.1p243 spinning with 100% CPU; perhaps rb_str_slice_bang-related

From: "Martin J. Dürst" <duerst@...>
Date: 2009-10-27 05:45:41 UTC
List: ruby-core #26353
To make sure you don't fall into the trap of not making the string 
shorter with slice!(-1), what about:

cur_length = mystring.bytesize
old_length = cur_length + 1

while cur_length > MAX_SIZE
   reduce_length = cur_length<old_length ? -1 : reduce_length-1
   mystring.slice!(reduce_length)
   old_length = cur_length
   cur_length = mystring.bytesize
end

This may get you around the problem, or it may not (in the case that 
"counting from the back" doesn't advance at all for some cases of 
garbage in some encoding).

It would really be good if you could give us examples of data (and its 
encoding) for which the problem is happening. That would make it easy to 
find bugs in case they are encoding-specific.


On the other hand, for a long string, chopping off one character at a 
time may be very inefficient. What about something more like binary 
search (written as a method on String)? Please verify yourself, this 
kind of code is notoriously difficult to get right!


class String

   def shorten_by_bytes (max_bytes)
     long_bytes  = mystring.bytesize
     return self if long_bytes <= max_bytes
     # established invariant:
     # short_bytes <= max_bytes < long_bytes
     # (assuming max_bytes >= 0)
     long_chars  = mystring.size
     long_string = mystring

     short_chars  = 0
     short_string = ''
     short_bytes  = 0

     while true
       middle_chars  = (long_chars+short_chars).div 2
       middle_string = mystring[0,middle_chars]
       middle_bytes  = middle_string.bytesize
       return middle_string if middle_bytes == MAX_SIZE or
                               middle_chars == short_chars
                               # also middle_chars + 1 = long_chars
       if middle_bytes < MAX_SIZE
         short_chars  = middle_chars
         short_string = middle_string
         short_bytes  = middle_bytes
       else     # middle_bytes > MAX_SIZE
         long_chars  = middle_chars
         long_string = middle_string
         long_bytes  = middle_bytes
       end
     end
   end
end

Regards,   Martin.

On 2009/10/27 6:34, Mark Aiken wrote:
> Issue #2283 has been updated by Mark Aiken.
>
>
> Potentially important note: the string being shortened is accepted directly from the user; it may be garbage. Are there any situations (malformed multibyte char?) in which slice!(-1) will not actually reduce the length of a string?
> ----------------------------------------
> http://redmine.ruby-lang.org/issues/show/2283
>
> ----------------------------------------
> http://redmine.ruby-lang.org
>
>

-- 
#-# Martin J. D端rst, Professor, Aoyama Gakuin University
#-# http://www.sw.it.aoyama.ac.jp   mailto:duerst@it.aoyama.ac.jp

In This Thread