PhilterPaper / Perl-PDF-Builder

Extended version of the popular PDF::API2 Perl-based PDF library for creating, reading, and modifying PDF documents
https://www.catskilltech.com/FreeSW/product/PDF%2DBuilder/title/PDF%3A%3ABuilder/freeSW_full
Other
6 stars 7 forks source link

[RT 121073] Negative offset to vec in lvalue context (Perl 5.26 bug) #60

Closed PhilterPaper closed 7 years ago

PhilterPaper commented 7 years ago

Tue Apr 11 12:08:04 2017 jv [...] cpan.org - Ticket created Subject: Negative offset to vec in lvalue context

A bug in Perl 5.26 causes PDF::API2 to crash when loading PNG images. See https://rt.perl.org/Public/Bug/Display.html?id=131083 for details.

The attached patch adds a workaround for this problem, in case it is not solved in time for the 5.26 release. Subject: PDF_Builder_Resource_XObject_Image_PNG.patch

*** PDF/API2/Resource/XObject/Image/PNG.pm~ 2017-04-11 18:03:15.841626104 +0200
--- PDF/API2/Resource/XObject/Image/PNG.pm  2017-04-11 18:00:44.084060531 +0200
***************
*** 264,269 ****
--- 264,275 ----
      }
  }

+ # Prevent "Negative offset to vec in lvalue context" for Perl 5.26.
+ sub __vec {
+     return 0 if $_[1] < 0;
+     vec( $_[0], $_[1], $_[2] );
+ }
+
  sub unprocess {
      my ($bpc,$bpp,$comp,$width,$height,$scanline,$sstream)=@_;
      my $stream=uncompress($$sstream);
***************
*** 280,286 ****
              $clear=$line;
          } elsif($filter==1) {
              foreach my $x (0..length($line)-1) {
!                 vec($clear,$x,8)=(vec($line,$x,8)+vec($clear,$x-$bpp,8))%256;
              }
          } elsif($filter==2) {
              foreach my $x (0..length($line)-1) {
--- 286,292 ----
              $clear=$line;
          } elsif($filter==1) {
              foreach my $x (0..length($line)-1) {
!                 vec($clear,$x,8)=(vec($line,$x,8)+__vec($clear,$x-$bpp,8))%256;
              }
          } elsif($filter==2) {
              foreach my $x (0..length($line)-1) {
***************
*** 288,299 ****
              }
          } elsif($filter==3) {
              foreach my $x (0..length($line)-1) {
!                 vec($clear,$x,8)=(vec($line,$x,8)+floor((vec($clear,$x-$bpp,8)+vec($prev,$x,8))/2))%256;
              }
          } elsif($filter==4) {
              # die "paeth/png filter not supported.";
              foreach my $x (0..length($line)-1) {
!                 vec($clear,$x,8)=(vec($line,$x,8)+PaethPredictor(vec($clear,$x-$bpp,8),vec($prev,$x,8),vec($prev,$x-$bpp,8)))%256;
              }
          }
          $prev=$clear;
--- 294,305 ----
              }
          } elsif($filter==3) {
              foreach my $x (0..length($line)-1) {
!                 vec($clear,$x,8)=(vec($line,$x,8)+floor((__vec($clear,$x-$bpp,8)+vec($prev,$x,8))/2))%256;
              }
          } elsif($filter==4) {
              # die "paeth/png filter not supported.";
              foreach my $x (0..length($line)-1) {
!                 vec($clear,$x,8)=(vec($line,$x,8)+PaethPredictor(__vec($clear,$x-$bpp,8),vec($prev,$x,8),__vec($prev,$x-$bpp,8)))%256;
              }
          }
          $prev=$clear;

# April 13, 2017, 01:43:32 PM » Hmm. So this patch is a workaround for a Perl bug, rather than something wrong in PDF::API2's code?

  1. Is this bug something that's going to blow up every time you use Perl 5.26 and handle PNG images, or is it a rare edge case? Has it been confirmed to show up in PDF::API2?
  2. If we implement this patch, it won't cause problems once Perl is fixed?
  3. Are there any penalties in using this patch, such as much slower operation, or a difference in how PNG images are rendered? Can we safely assume that even once Perl is fixed, that we can keep the patch in and it won't hurt anything?

If everyone is satisfied that these conditions are met, I can go ahead and put the patch in release 3.003 (end of this month). If Perl gets patched before 5.26 is released, I can back out the patch. When is 5.26 coming out, and we could confirm whether or not the patch is needed? If it's within a week or two, I would rather wait to see if the patch is still needed. # April 14, 2017, 04:48:42 AM by sciurius

Yes. We're the victim, not the cause.

  1. Amost every time (depends on the filter used).
  2. No, it is up/backwards compatible.
  3. Speed loss is negligible (I did time it).

But the best would be if they fix it before 5.26 comes out. I suggest applying this workaround fix only if 5.26 is released with the bug. # April 14, 2017, 02:36:33 PM by Phil

OK, I'll wait until Perl 5.26 comes out, and put in the fix if needed. Since I don't monitor Perl releases like you apparently do, please bump this thread to let me know one way or the other.

PhilterPaper commented 7 years ago

JV tells me that Perl 5.26 was fixed before release, so this bug is no longer applicable. Closing.