Skip to content

Commit

Permalink
calling die or exit directly lead to segfault (#55)
Browse files Browse the repository at this point in the history
* calling die or exit directly lead to segfault

* pre hook not needed
  • Loading branch information
pdelewski authored May 5, 2023
1 parent 7cd3e3f commit e7c5913
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
6 changes: 4 additions & 2 deletions otel_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,13 @@ static void observer_end(zend_execute_data *execute_data, zval *retval,
}

if (UNEXPECTED(EG(exception))) {
// do not release params[3] if exit was called
if (exception && !zend_is_unwind_exit(exception)) {
OBJ_RELEASE(Z_OBJ(params[3]));
}
if (exception) {
OBJ_RELEASE(exception);
OBJ_RELEASE(Z_OBJ(params[3]));
}

ZVAL_OBJ_COPY(&params[3], EG(exception));
}

Expand Down
39 changes: 39 additions & 0 deletions tests/calling_die_lead_to_segfault.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Check if calling die or exit will finish gracefully
--EXTENSIONS--
opentelemetry
--FILE--

<?php

use function OpenTelemetry\Instrumentation\hook;

class TestClass {
public static function countFunction(): void
{
for ($i = 1; $i <= 300; $i++) {
if ($i === 200) {
die('exit!');
}
}
}
}

hook(
'TestClass',
'countFunction',
null,
post: static function ($object, array $params, mixed $ret, ?\Throwable $exception ) {}
);

try{
TestClass::countFunction();
}
catch(Exception) {}
// Comment out line below and revert fix in order to trigger segfault
// reproduction frequency depends on platform
catch(TypeError) {}
?>

--EXPECT--
exit!

0 comments on commit e7c5913

Please sign in to comment.