Coder Social home page Coder Social logo

io-stringy's People

Contributors

haarg avatar

Watchers

 avatar  avatar

Forkers

haarg pghmcfc

io-stringy's Issues

IO::ScalarArray assumes join() with empty array returns undef

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.

push @lines, $line while (defined($line = $self->getline));

getLine, however, returns this.

return join( '', $self->_getlines_for_newlines );

_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;

IO::Scalar::print does not take under account the position inside the data and do a mere appending

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

Scalar.pm.txt

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.