Just some friday thoughts I'd like to share.

There's a line oriented network protocol where each command ends with a newline character. One of the commands there uses base64 encoded string as it's last argument.

On another side there's a client. It's written in Perl and it wasn't written by me. And it started misbehaving when an additional parameter was added after the one that was base64 encoded. The troubles were caused by a newline...

The client produced base64 data with encode_base64() from MIME::Base64 like this:

my $base64 = encode_base64("<some data>");

This seems perfectly fine. Later these data were sent to the server:

$sock->send("<data>$base64\n") or die "Can't write to $socket";

This also seems perfectly fine. Now a new parameter was added:

$sock->send("<data>$base64<param>\n") or die "Can't write to $socket";

The new parameter was ignored by the server when it processed the command and seeing what was wrong was not easy. Finally debugging at the server side showed that the client sent the new argument as a command by itself. This was very strange. Then I thought that maybe a newline somehow came between base64 data and the new argument... Reading the docs revealed that encode_base64() by default returns the data with a newline at the end!

encode_base64($str, $eol); Encode data by calling the encode_base64() function. The first argument is the string to encode. The second argument is the line-ending sequence to use. It is optional and defaults to "\n".

The fix was easy.

Beware of that newline and have a happy week end!


In reply to Beware of the newline at the end of the encode_base64() encoded strings by kirillm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.