[New-bugs-announce] [issue5104] getsockaddrarg() casts port number from int to short without any warning
Roman Zeyde
report at bugs.python.org
Fri Jan 30 00:23:56 CET 2009
New submission from Roman Zeyde <roman.zeyde at gmail.com>:
The following code shouldn't fail without any warning at all:
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.bind(('localhost', 70000))
>>> print(s.getsockname())
('127.0.0.1', 4464)
After looking through socketmodule.c (rev. 68450), it seems that AF_INET
case casts an "int port" into a "short addr->sin_port", and does not
checks for overflows:
case AF_INET:
{
struct sockaddr_in* addr;
char *host;
int port, result;
if (!PyTuple_Check(args)) {
PyErr_Format(
PyExc_TypeError,
"getsockaddrarg: "
"AF_INET address must be tuple, not %.500s",
Py_TYPE(args)->tp_name);
return 0;
}
if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
"idna", &host, &port))
return 0;
addr=(struct sockaddr_in*)addr_ret;
result = setipaddr(host, (struct sockaddr *)addr,
sizeof(*addr), AF_INET);
PyMem_Free(host);
if (result < 0)
return 0;
addr->sin_family = AF_INET;
addr->sin_port = htons((short)port);
*len_ret = sizeof *addr;
return 1;
}
----------
components: None
messages: 80794
nosy: roman.zeyde
severity: normal
status: open
title: getsockaddrarg() casts port number from int to short without any warning
versions: Python 2.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5104>
_______________________________________
More information about the New-bugs-announce
mailing list