recv() can fail and return undef.
Alexey V. Barantsev [Thu, 2 Nov 2000 20:12:20 +0000 (23:12 +0300)]
Subject: [ID 20001102.003] Net::Ping patch: "Bad arg lenght" error appears if host is unreachable
Message-Id: <200011021712.UAA07919@dallas.kazbek.ispras.ru>

p4raw-id: //depot/perl@7529

lib/Net/Ping.pm

index 9cc942d..e3006f9 100644 (file)
@@ -369,16 +369,17 @@ sub ping_udp
         elsif ($nfound)         # A packet is waiting
         {
             $from_msg = "";
-            $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags);
-            ($from_port, $from_ip) = sockaddr_in($from_saddr);
-            if (($from_ip eq $ip) &&        # Does the packet check out?
-                ($from_port == $self->{"port_num"}) &&
-                ($from_msg eq $msg))
-            {
-                $ret = 1;       # It's a winner
-                $done = 1;
-            }
-        }
+            $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags)
+               or last; # For example an unreachable host will make recv() fail.
+           ($from_port, $from_ip) = sockaddr_in($from_saddr);
+           if (($from_ip eq $ip) &&        # Does the packet check out?
+               ($from_port == $self->{"port_num"}) &&
+               ($from_msg eq $msg))
+           {
+               $ret = 1;       # It's a winner
+               $done = 1;
+           }
+       }
         else                    # Oops, timed out
         {
             $done = 1;