rbpdf icon indicating copy to clipboard operation
rbpdf copied to clipboard

Inifite loop

Open salmanmp opened this issue 3 years ago • 4 comments

When try to export some html text that contains tag with invalid src attribute, it is gone into infinit loop. May be this line causes that infinite loop. https://github.com/naitoh/rbpdf/blob/838341d18417de3d0b5d4c832a23859c01e55328/lib/rbpdf.rb#L12173

salmanmp avatar Jul 31 '21 14:07 salmanmp

Hello,

It's not invalid src attribute the problem but the width of the image who need new linebreak to format the document.

The key-- is used to evaluate the new linebreak and reevaluate img tag. But, if the image's width is too large a newline is add to infinity and beyond.

skalimer0 avatar Feb 24 '22 08:02 skalimer0

A solution is to check imgw, and if it's too bigger, use instead : 100% rendering

skalimer0 avatar Feb 24 '22 09:02 skalimer0

It's not invalid src attribute the problem but the width of the image who need new linebreak to format the document.

Probabely both of them. I tested that situation. Thanks.

salmanmp avatar Feb 24 '22 10:02 salmanmp

i fork the project and patch it for the oversize infinite loop on line 12173

I've make a pull request too.

skalimer0 avatar Feb 24 '22 10:02 skalimer0

Is it going to update gem on rubygems.org?

salmanmp avatar Jan 31 '23 18:01 salmanmp

please use https://rubygems.org/gems/rbpdf/versions/1.21.0

naitoh avatar Feb 13 '23 14:02 naitoh

I tested with an invalid src attribute and unfortunately infinite loop was occurred! something like <img src="/system/files/not-exist-file.png">

salmanmp avatar Feb 23 '23 15:02 salmanmp

I tested with an invalid src attribute and unfortunately infinite loop was occurred! something like <img src="/system/files/not-exist-file.png">

@salmanmp

I can't reproduce it in my environment.

$ irb
> require 'rbpdf'
> pdf = RBPDF.new
> pdf.add_page
> pdf.write_html('<img src="/system/files/not-exist-file.png">')
E, [2023-02-25T20:49:32.257183 #37955] ERROR -- : pdf: Image: error: RBPDF error: Image file is not found. : /system/files/not-exist-file.png
=> nil
> pdf.write_html('<html><body><img src="/system/files/not-exist-file.png"></body></html>')
E, [2023-02-25T20:52:38.317192 #37988] ERROR -- : pdf: Image: error: RBPDF error: Image file is not found. : /system/files/not-exist-file.png
=> nil

My Environment: Ruby 3.1.2, no rmagick, no mini_magick.

$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
$ gem list |grep magick
$
$ gem list rbpdf

*** LOCAL GEMS ***

rbpdf (1.21.0)
rbpdf-font (1.19.1)

naitoh avatar Feb 25 '23 11:02 naitoh

I tested again and could not reproduce that bug. Thank you.

salmanmp avatar Jun 17 '23 09:06 salmanmp

Hi again I found the scenario that makes an infinit loop!

#irb
html = '<p><img src="/somewhere/image.png" style="width:2000px;height:563px;"></p>'
r = RBPDF.new
r.add_page
r.set_rtl(true)
r.writeHTMLCell(0, 0, "", "", html)

If you decerase width to 1000px it works fine.

As I mentioned at the first message it is related to this code block in rbpdf.rb. But it's not related to src and existance of image, but related to oversize of image (that @skalimer0 told before :+1: ).

if (@rtl and (@x - imgw < @l_margin + @c_margin)) or (!@rtl and (@x + imgw > @w - @r_margin - @c_margin))
    # add automatic line break
    autolinebreak = true
    Ln('', cell)
    # go back to evaluate this line break
    key -= 1
end

My Environment:

$ruby -v
ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5) [x86_64-linux]

$gem list rbpdf
*** LOCAL GEMS ***
rbpdf (1.21.1, 1.21.0, 1.20.1)
rbpdf-font (1.19.1)

salmanmp avatar Jun 22 '23 15:06 salmanmp