genio / io-stringy Goto Github PK
View Code? Open in Web Editor NEWI/O on in-core objects like strings and arrays
License: Other
I/O on in-core objects like strings and arrays
License: Other
Earlier this week I found a script which sends out emails hanging indefinitely. The offending code was this:
my $smtp = get_smtp_object();
$smtp->destinations($msg, @dst);
{
local $/ = undef; # Speeds up sending of large attachments significantly.
$smtp->trySend($msg, from => $return_path) or die "Could not send email: $!";
}
Under the hood, Mail::Transport::SMTP
uses IO::Lines
' getLines
, in fact it changed from using getLine
to getLines
a few years ago, see markov2/perl5-Mail-Transport@207814e.
The problem? This code works perfectly fine, except when the email doesn't have an attachment.
In ScalarArray, this line assumes that self->getLine
will return undef
at some point.
IO-Stringy/lib/IO/ScalarArray.pm
Line 374 in 91659c0
getLine
, however, returns this.
Line 61 in 91659c0
_getlines_for_newlines
, in turn, returns this:
return (@partial,
@$rArray[ $Str .. $#$rArray ]);
Even if the array is empty, the join
call will still not return undef
, so I think this assumption might be wrong. Simple script to reproduce the hanging:
#! /usr/bin/perl
use strict;
use warnings;
use Mail::Message;
my $iolines = IO::Lines->new(["hello"]);
local $/ = undef;
print $iolines->getlines;
Hello,
As of version 2.113, print
inside IO::Scalar
merely append data provided and does not take under account the position inside the scalar:
*$self->{Pos} = length(${*$self->{SR}} .= join('', @_) . (defined($\) ? $\ : ""));
To demonstrate this, consider this script:
#!/usr/bin/perl
BEGIN
{
use strict;
use warnings;
use open ':std' => ':utf8';
use IO::Scalar;
};
{
use utf8;
my $text = <<EOT;
Mignonne, allons voir si la rose
Qui ce matin avoit desclose
Sa robe de pourpre au Soleil,
A point perdu cette vesprée
Les plis de sa robe pourprée,
Et son teint au vostre pareil.
Author: Pierre de Ronsard
EOT
open( my $fh, '+>/tmp/test.txt' );
$fh->binmode( ':utf8' );
$fh->print( $text );
$fh->seek( length( $text ) - 1, 0 );
$fh->print( ', Les Odes' );
$fh->close;
# Now trying with IO::Scalar
my $data;
my $s = IO::Scalar->new( \$data );
$s->print( $text );
$s->seek( length( $text ) - 1, 0 );
$s->print( ', Les Odes' );
print( "Scalar is now:\n$s" );
$s->close;
}
__END__
The file /tmp/test.txt
will contain, as expected:
Mignonne, allons voir si la rose
Qui ce matin avoit desclose
Sa robe de pourpre au Soleil,
A point perdu cette vesprée
Les plis de sa robe pourprée,
Et son teint au vostre pareil.
Author: Pierre de Ronsa, Les Odes
Whereas the script that prints out the resulting value of IO::Scalar would yield:
Scalar is now:
Mignonne, allons voir si la rose
Qui ce matin avoit desclose
Sa robe de pourpre au Soleil,
A point perdu cette vesprée
Les plis de sa robe pourprée,
Et son teint au vostre pareil.
Author: Pierre de Ronsard
, Les Odes
The expectation is that the additional text printed to the scalar should have been printed before the last line feed as positioned earlier with the seek
.
I am running perl v5.32 under Ubuntu 20.04
Attached is a diff file with my proposed correction. I had to set the file suffix to .txt because Git would not let me use .diff
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.