|
| 1 | +--TEST-- |
| 2 | +Bug #76839: socket_recvfrom may return an invalid 'from' address on MacOS |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { |
| 6 | + die('skip not valid for Windows.'); |
| 7 | +} |
| 8 | +if (!extension_loaded('sockets')) { |
| 9 | + die('skip sockets extension not available.'); |
| 10 | +} |
| 11 | +--FILE-- |
| 12 | +<?php |
| 13 | + |
| 14 | +// This bug only occurs when a specific portion of memory is unclean. |
| 15 | +// Unforunately, looping around 10 times and using random paths is the |
| 16 | +// best way I could manage to reproduce this problem without modifying php itself :-/ |
| 17 | + |
| 18 | +for ($i = 0; $i < 10; $i++) { |
| 19 | + $senderSocketPath = '/tmp/' . substr(md5(rand()), 0, rand(8, 16)) . '.sock'; |
| 20 | + $senderSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0); |
| 21 | + socket_bind($senderSocket, $senderSocketPath); |
| 22 | + |
| 23 | + $receiverSocketPath = '/tmp/' . substr(md5(rand()), 0, rand(8, 16)) . '.sock'; |
| 24 | + $receiverSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0); |
| 25 | + socket_bind($receiverSocket, $receiverSocketPath); |
| 26 | + |
| 27 | + // Send message from sender socket to receiver socket |
| 28 | + socket_sendto($senderSocket, 'Ping!', 5, 0, $receiverSocketPath); |
| 29 | + |
| 30 | + // Receive message on receiver socket |
| 31 | + $from = ''; |
| 32 | + $message = ''; |
| 33 | + socket_recvfrom($receiverSocket, $message, 65535, 0, $from); |
| 34 | + echo "Received '$message'\n"; |
| 35 | + |
| 36 | + // Respond to the sender using the 'from' address from socket_recvfrom |
| 37 | + socket_sendto($receiverSocket, 'Pong!', 5, 0, $from); |
| 38 | + echo "Responded to sender with 'Pong!'\n"; |
| 39 | + |
| 40 | + socket_close($receiverSocket); |
| 41 | + unlink($receiverSocketPath); |
| 42 | + socket_close($senderSocket); |
| 43 | + unlink($senderSocketPath); |
| 44 | +} |
| 45 | +--EXPECT-- |
| 46 | +Received 'Ping!' |
| 47 | +Responded to sender with 'Pong!' |
| 48 | +Received 'Ping!' |
| 49 | +Responded to sender with 'Pong!' |
| 50 | +Received 'Ping!' |
| 51 | +Responded to sender with 'Pong!' |
| 52 | +Received 'Ping!' |
| 53 | +Responded to sender with 'Pong!' |
| 54 | +Received 'Ping!' |
| 55 | +Responded to sender with 'Pong!' |
| 56 | +Received 'Ping!' |
| 57 | +Responded to sender with 'Pong!' |
| 58 | +Received 'Ping!' |
| 59 | +Responded to sender with 'Pong!' |
| 60 | +Received 'Ping!' |
| 61 | +Responded to sender with 'Pong!' |
| 62 | +Received 'Ping!' |
| 63 | +Responded to sender with 'Pong!' |
| 64 | +Received 'Ping!' |
| 65 | +Responded to sender with 'Pong!' |
0 commit comments