OS: Linux Mint 22
Gscan2pfd from jeffreyratcliffe/ppa
Hi,
it's my first time to somehow contribute to FOSS, so please bear with my inexperience.
I regularly use the feature to append new scans to an existing PDF.
I always had the issue that the saving process seemingly freezes when the status bar says "Closing PDF".
I can exit the saving process via button and the program will claim it didn't safe my work, despite the new PDF and the .bak-file existing (As seen in the log after the freeze was cleared by empyting the process queue).
Yesterday I went on the endeavor to debug the issue with ChatGPT without knowing any Perl...
In the end I was able to fix the issue by focusing on the warnings in the log (see attachment).
Here are the results and the Patch generated by ChatGPT. I don't know whether it's just a bandaid or a proper fix but I hope it contributes to easily identify the issue. The changes have several prints from the debugging process inside left.
Hope it helps.
Best regards and thanks for the awesome work!
tz array not having enough elements, which caused issues when attempting to modify it and subsequently pass it to the Add_Delta_DHMS function.tz array and conditionally modifying it, we avoided passing an invalid or empty array to Add_Delta_DHMS.--- a/lib/Gscan2pdf/Document.pm
+++ b/lib/Gscan2pdf/Document.pm
@@ -2372,13 +2372,28 @@ sub prepare_output_metadata {
my ( $year, $month, $day, $hour, $min, $sec ) =
@{ $metadata->{datetime} };
my ( $sign, $dh, $dm ) = ( q{+}, 0, 0 );
+
+ # Change 1: Added checks to ensure tz is an array and has enough elements before using it.
if ( defined $metadata->{tz} ) {
- ( undef, undef, undef, $dh, $dm, undef, undef ) =
- @{ $metadata->{tz} };
- if ( $dh * $MINUTES_PER_HOUR + $dm < 0 ) { $sign = q{-} }
- $dh = abs $dh;
- $dm = abs $dm;
+ if ( ref($metadata->{tz}) eq 'ARRAY' && scalar(@{$metadata->{tz}}) >= 7 ) {
+ ( undef, undef, undef, $dh, $dm, undef, undef ) = @{ $metadata->{tz} };
+ print "tz metadata - dh: $dh, dm: $dm\n";
+ } else {
+ warn "tz is not an array reference or does not have enough elements. Defaulting dh and dm to 0.\n";
+ $dh = 0;
+ $dm = 0;
+ }
+ } else {
+ warn "tz metadata is missing. Defaulting dh and dm to 0.\n";
+ $dh = 0;
+ $dm = 0;
}
+
+ # Original code continues from here.
+ if ( $dh * $MINUTES_PER_HOUR + $dm < 0 ) { $sign = q{-} }
+ $dh = abs $dh;
+ $dm = abs $dm;
+
$h{CreationDate} = sprintf $dateformat, $year, $month, $day, $hour,
$min, $sec, $sign,
$dh, $dm;
@@ -3607,23 +3622,46 @@ sub _set_timestamp {
return;
}
+ print "Initial datetime array: ", join(", ", @{$options{metadata}{datetime}}), "\n";
+
my @datetime = @{ $options{metadata}{datetime} };
+
if ( defined $options{metadata}{tz} ) {
my @tz = @{ $options{metadata}{tz} };
- splice @tz, 0, 2;
- splice @tz, $LAST_ELEMENT, 1;
- for (@tz) {
- if ( not defined ) {
- $_ = 0;
- }
- else {
- $_ = -$_;
+
+ print "Initial tz array: ", join(", ", @tz), "\n";
+
+ # Change 2: Added check to ensure tz array has at least 7 elements before modifying it.
+ if (scalar(@tz) >= 7) {
+ splice @tz, 0, 2;
+ splice @tz, -1, 1; # -1 targets the last element.
+
+ for (@tz) {
+ if ( not defined ) {
+ $_ = 0;
+ }
+ else {
+ $_ = -$_;
+ }
}
+
+ print "Modified tz array: ", join(", ", @tz), "\n";
+ } else {
+ warn "tz array does not have enough elements to modify. Defaulting to empty array.\n";
+ @tz = ();
+ }
+
+ print "Datetime before Add_Delta_DHMS: ", join(", ", @datetime), "\n";
+ print "TZ before Add_Delta_DHMS: ", join(", ", @tz), "\n";
+
+ if (@tz) { # Only call Add_Delta_DHMS if @tz is not empty.
+ @datetime = Add_Delta_DHMS( @datetime, @tz );
}
- @datetime = Add_Delta_DHMS( @datetime, @tz );
}
+
try {
my $time = Date_to_Time(@datetime);
+ print "Final time value for utime: $time\n";
utime $time, $time, $options{path};
}
catch {
Thanks for the report and kudos for finding a fix without knowing any Perl!
I'm in the middle of a complete rewrite (using Python, which has datetime handling in core libraries). So I'll leave your report here for reference until I've been able to release the first version of the rewrite.