Req #69175 [Opn]: posix_strerror is not thread-safe

From: Date: Wed, 01 Sep 2021 12:53:43 +0000
Subject: Req #69175 [Opn]: posix_strerror is not thread-safe
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236213@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69175&edit=1

 ID:                 69175
 Updated by:         cmb@php.net
 Reported by:        james at jamesreno dot com
 Summary:            posix_strerror is not thread-safe
 Status:             Open
 Type:               Feature/Change Request
 Package:            POSIX related
 Operating System:   Linux
 PHP Version:        5.6.6
 Block user comment: N
 Private report:     N

 New Comment:

Seems like a good idea, but would require to check whether
strerror_r(3) is available at build time.

PRs welcome!


Previous Comments:
------------------------------------------------------------------------
[2015-03-03 18:33:50] james at jamesreno dot com

Description:
------------
As with alot of the PHP extensions some are not thread-safe by nature however posix_strerror could
easily be thread-safe by making a small change to the way it operates and as such is fully capable
of becoming thread-safe. It is my impression that this use is not thread-safe (I could be wrong).

The problem is that posix_strerror makes a call to strerror() which returns a pointer to the string
which may *not* be modified as it is reused and overwritten by the next call to strerror. Instead
posix_strerror should call strerror_r which accepts a buffer and buffer length and writes the data
there instead.

Granted that PHP seems to be making a copy of the output for its own use there is a possibility of a
race condition occurring with other threads. This is just a small potential improvement that
shouldnt take much time to implement and would be a a step forward for thread-safety. 

There may be other cases of thread-safety issues in the posix extension but those can be tackled one
at a time. This is just a very limited and specific report for one use case. If someone wants to
scan the entire extension for said issues more power to them but I am just requesting this
particular case be resolved.

Test script:
---------------
<?php
 echo posix_strerror(110);
?>

Expected result:
----------------
No corruption should ever be possible to occur.

Actual result:
--------------
Corruption could possibly occur in a build utilizing multiple threads.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=69175&edit=1


Thread (2 messages)

« previous php.bugs (#236213) next »