I have been writing a network app (in fact, a Gnutella client) that reads a character stream, and processes it. Fine and dandy. I've got my routine breaking the stream into packets and acting on them. But every so often the program would lose sync with the stream.

Finally after many hours debugging I discover that it's my fault. For some reason, my while loop was exiting before it reaches the end of the string. It turns out that there is a magic character in the stream that causes Perl to end the string too soon. But here's the confusing bit - *only some commands are vulnerable*. I can print out the entire string. But I can't substr it. Then when I try to print it again, it is foreshortened.

The evil character appears near (before, I think) the character '0' - that's the number 0. Naturally I guess unknown character is an end-of-string character for perl, but it's not a chr(0) - those come through fine.

while (!(recv $connection,$z,1000,0)){}; #The following line prints the correct number of packets #fetched print "\nLength fetched: ",length($z)," string ",$z if length($z); my $g=0; #$z=quotemeta $z; #This while exits *before* it has finished the string while (my $a=substr $z,$g,1) { $g++; #Process characters in this here

Does anyone know what's going on? I'd appreciate any help or pointers to info that you know of. I read part of the perguts manpage but I couldn't figure anything useful from it.

____________________
Jeremy
I didn't believe in evil until I dated it.


In reply to Bad Char in string by jepri

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.