Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

fsockopen()/stream_socket_client() accept TCP ports above 65535, which wrap to another port

Open Beginner friendly
#24,616 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, php

Research direction

Start by reading parse_ip_address_ex() in main/streams/xp_socket.c, especially the atoi() calls noted in the issue, and check how socket ports are validated elsewhere, including parse_url(). Add a regression test for stream_socket_client() and fsockopen() with a port above 65535. Done means both reject the invalid port instead of connecting to the wrapped port.

Written by the indexing model from the issue text.

Description

Description

A TCP port number above 65535 is not rejected by fsockopen() or stream_socket_client(): it wraps modulo 65536 and the connection goes to a different port. parse_ip_address_ex() in main/streams/xp_socket.c reads the port with atoi() and stores it without a range check.

The following code:

<?php
$srv = stream_socket_server("tcp://127.0.0.1:0");
$name = stream_socket_get_name($srv, false);
$port = (int)substr($name, strrpos($name, ':') + 1);

$c = stream_socket_client("tcp://127.0.0.1:" . ($port + 65536));
echo "asked for ", $port + 65536, ", connected to ", stream_socket_get_name($c, true), "\n";

$c2 = fsockopen("127.0.0.1", $port + 65536);
echo "fsockopen connected to ", stream_socket_get_name($c2, true), "\n";

Resulted in this output:

asked for 99735, connected to 127.0.0.1:34199
fsockopen connected to 127.0.0.1:34199

But I expected this output instead: both calls fail with an invalid port error, since 99735 is not a valid TCP port (parse_url() already rejects such ports).

Reproduced on 8.2.34-dev (512c04e39f) and 8.4.28-dev; master has the same atoi() calls (xp_socket.c lines 654 and 664).

This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually.

PHP Version
PHP 8.4.28-dev, PHP 8.2.34-dev
Operating System

Linux (Ubuntu 20.04, x86_64)

Dominant language
C
Stars
40.7k
Forks
8.3k
Avg merge
2d 15h
Merged PRs (30d)
155

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from php/php-src

All issues in php/php-src

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.