Skip to content

Messages with a trailing space can cause a deadlock when using a unix domain socket. #2

@larntz

Description

@larntz

Not sure if this would be considered a bug or intended behavior.

The Message class is using pack() and unpack(), but with the A* formatting unpack() will strip trailing spaces php doc. Changing that to a* fixes the trailing space issue, but I'm not sure if it would cause unexpected side effects.

The "A" code now strips all trailing ASCII whitespace (spaces, tabs, newlines, carriage returns, and NULL bytes).

This causes a deadlock here if the sender calls receiveMessage() after sendMessage().

The reason for this is that strlen($msg['payload']) is 1 byte less than $msg['length'], because $msg['length'] is including the trailing space. Multiple trailing spaces will cause +1 byte difference for each trailing space.

at 14:30:09 ❯ php bin/packer.php
PACK_FORMAT = JA*
UNPACK_FORMAT = Jlength/A*payload

we're now going to pack a message with a trailing space: 'space space space > '
original msg == 'space space space > '
strlen(msg) == 20
strlen(packed) == 28 (+8 bytes for header)
unpacked == 'space space space >'
strlen(unpacked) == 19
unpacked['length'] == 20

-----

we're now going to pack a message _without_ a trailing space: 'word word word word >'
original msg == 'word word word word >'
strlen(msg) == 21
strlen(packed) == 29 (+8 bytes for header)
unpacked == 'word word word word >'
strlen(unpacked) == 21
unpacked['length'] == 21

Multiple trailing spaces.

we're now going to pack a message with a trailing space: 'space space space >     '
original msg == 'space space space >     '
strlen(msg) == 24
strlen(packed) == 32 (+8 bytes for header)
unpacked == 'space space space >'
strlen(unpacked) == 19
unpacked['length'] == 24

I created a minimal example to reproduce the issue: https://github.com/larntz/php-symplib-testing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions