protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

PHP Extension - Segmentation fault when serialize() is called on a protobuf message

Open FabioBatSilva opened this issue 2 months ago • 3 comments

The PHP C extension segfaults when serialize() is called on a protobuf message.

[!IMPORTANT]
This is a unusual use case but the extension should not crash if a proto message is serialize by mistake.


What version of protobuf and what language are you using? Version: main Language: php OS: Linux

What did you do?

<?php
# serialize_segfault.php

require_once (dirname(__DIR__) . '/vendor/autoload.php');

use Foo\TestMessage;

$msg = new TestMessage(['optional_string' => "Hello, World!"]);

serialize($msg);
# Compile the extension 
./php/tests/compile_extension.sh

# Run script
php -d display_errors=on -dextension=../ext/google/protobuf/modules/protobuf.so  serialize_segfault.php

What did you expect to see

# no errors.

What did you see instead?

Segmentation fault

Patch here

FabioBatSilva avatar Nov 08 '25 02:11 FabioBatSilva

If you're willing to send us your patch as a PR, we would likely accept it (subject to code review).

bellspice avatar Nov 11 '25 18:11 bellspice

@FabioBatSilva do you know why serialization isn't supported? Your patch adds an exception but it doesn't look like a bugfix

hotrush avatar Dec 15 '25 14:12 hotrush

@FabioBatSilva do you know why serialization isn't supported? Your patch adds an exception but it doesn't look like a bugfix

From what I can tell the extension bypasses a lot of the builtin mechanisms that are necessary for that to work. PHP serialize on a proto message is a bit of an odd use case anyways, so I opted to just prevent the segfault.

FabioBatSilva avatar Dec 15 '25 16:12 FabioBatSilva