Skip to content

Commit

Permalink
Initialize socketName in a thread safe way
Browse files Browse the repository at this point in the history
Use pthread_once() to initialize the variable only once.
It was possible that 2 threads were modifying the variable at the same
time.

The ThreadSanitizer trace was:
==================
WARNING: ThreadSanitizer: data race (pid=14864)
  Write of size 1 at 0x7f8803cccd2b by main thread:
    #0 getSocketName src/winscard_msg.c:101 (libpcsclite.so.1+0xa28c)
    #1 getSocketName src/winscard_msg.c:85 (libpcsclite.so.1+0xa28c)
    #2 SCardCheckDaemonAvailability src/winscard_clnt.c:3550 (libpcsclite.so.1+0x65cf)
    #3 SCardEstablishContext src/winscard_clnt.c:479 (libpcsclite.so.1+0x66a7)
    #4 main /tmp/pcsc/pcsc_demo.cpp:86 (pcsc_demo+0x1517)

  Previous write of size 1 at 0x7f8803cccd2b by thread T1:
    #0 getSocketName src/winscard_msg.c:101 (libpcsclite.so.1+0xa28c)
    #1 getSocketName src/winscard_msg.c:85 (libpcsclite.so.1+0xa28c)
    #2 SCardCheckDaemonAvailability src/winscard_clnt.c:3550 (libpcsclite.so.1+0x65cf)
    #3 SCardEstablishContext src/winscard_clnt.c:479 (libpcsclite.so.1+0x66a7)
    #4 do_statuschange /tmp/pcsc/pcsc_demo.cpp:12 (pcsc_demo+0x1264)
    #5 statuschange_thread /tmp/pcsc/pcsc_demo.cpp:53 (pcsc_demo+0x1264)

  Location is global '<null>' at 0x000000000000 (libpcsclite.so.1+0xfd2b)

  Thread T1 (tid=14867, running) created by main thread at:
    #0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1001 (libtsan.so.2+0x5e686)
    #1 start_thread(char const*) /tmp/pcsc/pcsc_demo.cpp:61 (pcsc_demo+0x1432)
    #2 main /tmp/pcsc/pcsc_demo.cpp:84 (pcsc_demo+0x14cc)

SUMMARY: ThreadSanitizer: data race src/winscard_msg.c:101 in getSocketName
==================
ThreadSanitizer: reported 1 warnings

Thanks to Stefan Ehmann for the bug report.
  • Loading branch information
LudovicRousseau committed Nov 19, 2023
1 parent 1fb174c commit 9466fc0
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/winscard_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,27 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#define member_size(type, member) sizeof(((type *)0)->member)

char *getSocketName(void)
static char SocketName[member_size(struct sockaddr_un, sun_path)];
static pthread_once_t SocketName_init_control = PTHREAD_ONCE_INIT;
static void SocketName_init(void)
{
static char socketName[member_size(struct sockaddr_un, sun_path)];

if ('\0' == socketName[0])
{
/* socket name not yet initialized */
char *socketNameEnv;
/* socket name not yet initialized */
char *socketNameEnv;

socketNameEnv = getenv("PCSCLITE_CSOCK_NAME");
if (socketNameEnv)
strncpy(socketName, socketNameEnv, sizeof(socketName));
else
strncpy(socketName, PCSCLITE_CSOCK_NAME, sizeof(socketName));
socketNameEnv = getenv("PCSCLITE_CSOCK_NAME");
if (socketNameEnv)
strncpy(SocketName, socketNameEnv, sizeof SocketName);
else
strncpy(SocketName, PCSCLITE_CSOCK_NAME, sizeof SocketName);

/* Ensure a NUL byte */
socketName[sizeof socketName -1] = '\0';
}
/* Ensure a NUL byte */
SocketName[sizeof SocketName -1] = '\0';
}

return socketName;
char *getSocketName(void)
{
pthread_once(&SocketName_init_control, SocketName_init);
return SocketName;
}

/**
Expand Down

0 comments on commit 9466fc0

Please sign in to comment.