Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null initialized shared memory #7

Closed
TheLevti opened this issue Jan 26, 2020 · 0 comments
Closed

Handle null initialized shared memory #7

TheLevti opened this issue Jan 26, 2020 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@TheLevti
Copy link
Owner

The travis ci pipeline sometimes fails randomly on the ProcessManagerTest::testLargeBatchProcessing test. As it turns out, the unserialize function raises a notice, because of an non unserializable string, which phpunit translates into an exception. ProcessManager::fork catches this exception when calling $shm->send($message, false); and replaces the result with null.

The output of this bug is then the following:

There was 1 failure:

1) Spork\ProcessManagerTest::testLargeBatchProcessing with data set #0 (10)
null does not match expected type "array".

Background

When a new shared memory is created it may be filled with null.

https://www.php.net/manual/en/function.shmop-read.php#48828

shmop_read() reads and returns the whole memory segment's data. This is not useful if you're just working with strings. If you need to read a string from shared memory, call str_from_mem() on the result of shmop_read(). Similarly when writing strings to memory (instead of binary data), null terminate your strings with str_to_nts() before passing the value on to shmop_write().

As expected shmop_read returns the whole shared memory block filled with null terminators ("\x00\x00...").

Possible Fix

Before passing the shared memory contents to unserialize, make sure to truncate everything after the first null byte. Respectively add a null byte to the end of the serialized string before passing the data to shmop_write().

@TheLevti TheLevti self-assigned this Jan 26, 2020
@TheLevti TheLevti added the bug Something isn't working label Feb 1, 2020
@TheLevti TheLevti closed this as completed Feb 1, 2020
TheLevti added a commit that referenced this issue Feb 1, 2020
### Changed

- #5: Preserve and restore previous signal handler. Refactored event dispatcher.

### Removed

- #5: Removed the method `addListener` from the `ProcessManager` class. Add
  signal/normal listeners through the event dispatcher on the process manager.

### Fixed

- #7: Fixed missing null terminator handling on shared memory blocks.
- #8: Fixed parent's shutdown function being executed in child processes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant