[New-bugs-announce] [issue22206] PyThread_create_key(): fix comparison between signed and unsigned numbers

STINNER Victor report at bugs.python.org
Fri Aug 15 23:45:30 CEST 2014

New submission from STINNER Victor:

The issue #22110 enabled more compiler warnings. I would like to fix this one:
gcc -pthread -c -Wno-unused-result -Wsign-compare -g -O0 -Wall -Wstrict-prototypes    -Werror=declaration-after-statement   -I. -IInclude -I./Include    -DPy_BUILD_CORE -o Python/thread.o Python/thread.c
In file included from Python/thread.c:86:0:
Python/thread_pthread.h: In function ‘PyThread_create_key’:
Python/thread_pthread.h:611:22: attention : signed and unsigned type in conditional expression [-Wsign-compare]
     return fail ? -1 : key;

Attached patch uses Py_SAFE_DOWNCAST() to explicitly downcast to int.

On Linux (on my Fedora 20/amd64), pthread_key_t is defined as an unsigned int, whereas the result type of PyThread_create_key is a signed int.

Nobody complained before, so I get that nobody noticed the possible overflow for a key > INT_MAX. I checked the code, we only check if PyThread_create_key() returns -1, if you reach UINT_MAX keys. UINT_MAX keys sounds insane, you probably hit another limit before.

On Linux, it looks like the key is a counter and deleted values are reused:

haypo at selma$ ./python
Python 3.5.0a0 (default:a0b38f4eb79e, Aug 15 2014, 23:37:42) 
[GCC 4.8.3 20140624 (Red Hat 4.8.3-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> ctypes.pythonapi.PyThread_create_key()
>>> ctypes.pythonapi.PyThread_create_key()
>>> ctypes.pythonapi.PyThread_create_key()
>>> ctypes.pythonapi.PyThread_delete_key(3)
>>> ctypes.pythonapi.PyThread_create_key()

files: PyThread_create_key.patch
keywords: patch
messages: 225367
nosy: haypo, neologix
priority: normal
severity: normal
status: open
title: PyThread_create_key(): fix comparison between signed and unsigned numbers
versions: Python 3.5
Added file: http://bugs.python.org/file36380/PyThread_create_key.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list