php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31090 fwrite (supposed to be binary safe) sends string regardless of variable type
Submitted: 2004-12-14 17:18 UTC Modified: 2004-12-15 21:36 UTC
From: ifirla at aegissrl dot it Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 4.3.9 OS: Debian GNU/Linux 3.0
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ifirla at aegissrl dot it
New email:
PHP Version: OS:

 

 [2004-12-14 17:18 UTC] ifirla at aegissrl dot it
Description:
------------
an array holds elements of various types:

ascii|integer|integer|integer|integer

in a foreach loop, sending each element to a socket opened with fsockopen sends the elements as strings, not as the character type.

gettype within the foreach loop shows that I'm sending dealing with the correct types:

16:39:54 SENT     'K' of type string
16:39:54 SENT     '0' of type integer
16:39:54 SENT     '0' of type integer
16:39:54 SENT     '65' of type integer
16:39:54 SENT     '31' of type integer

fwrite is, according to the documentation, supposed to be binary safe.

I think there's a bug in fwrite that has it send all elements as strings.

Reproduce code:
---------------
function send_back($sock, $msg, $ip, $port) {
	$fp = fsockopen("udp://$ip", "$port");
	foreach ($msg as $msg_to_send) {
		fwrite($fp, $msg_to_send);
		echo date("H:i:s")." SENT     '$msg_to_send' of type ".gettype($msg_to_send)." \n"; //seen server side
	}
	fclose($fp);
}

Expected result:
----------------
running a packet sniffer, I can see that what's being sent are strings:

15:46:10  192.168.0.101:32778 --> 192.168.0.84:32896 | UDP |

 0000: 4b                                       K


15:46:10  192.168.0.101:32778 --> 192.168.0.84:32896 | UDP |


 0000: 30                                       0


15:46:10  192.168.0.101:32778 --> 192.168.0.84:32896 | UDP |

 0000: 30                                       0


15:46:10  192.168.0.101:32778 --> 192.168.0.84:32896 | UDP |

 0000: 3635                                     65


15:46:10  192.168.0.101:32778 --> 192.168.0.84:32896 | UDP |

 0000: 3331                                     31

Actual result:
--------------
Those last two (65 and 31) should be represented by single bytes, not two bytes.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-15 01:05 UTC] sniper@php.net
Here's the proto for fwrite():

int fwrite ( resource handle, string string [, int length])

See the 3rd parameter? Give the length to the function.
Also note the 2nd parameter type..

No bug here.

 [2004-12-15 21:36 UTC] wez@php.net
you need to pack() if you want to send a 32 bit integer
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 17:01:33 2025 UTC