# beta2, deriving from class Extension and calling parent::__toString() segfaults

- Thread: https://externals.io/message/5802
- Original archive: https://news-web.php.net/php.internals/5802
- Started by mfischer@gjat.josefine@Markus Fischer on 2003-11-29T14:39:17+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #5802 — mfischer@gjat.josefine@Markus Fischer — 2003-11-29T14:39:17+00:00

URL: https://externals.io/message/5802#5802

When trying to derive from class Exception and access the 'string'
  property or call 'parent::__toString' PHP segfaults:

<?php
    class MyException extends Exception {
        function __toString() {
            var_dump($this->string);
        }
    }

    throw new MyException("hmm");
?>

Output:

$ php testException.php
 `NULL` 
Segmentation fault

  Or this example:

<?php
    class MyException extends Exception {
        function __toString() {
			parent::__toString();
        }
    }

    throw new MyException("hmm");
?>

Output:

$ php testException.php
Segmentation fault


	The need for this came up when I saw that the reported exception is
	not foramted in the HTML output; it's quite hard next to impossible
	to properly read the stack trace. So my goal was to make my own
	exception and call `nl2br()` on the string returned from the class.

	Besides the segfaults above, is there a chance we have a nicer HTML
	formatting for reported Exceptions (while still strip those HTML
	tags from it for logging)?

	- Markus

---

## #5803 — Marcus Boerger — 2003-11-29T17:09:36+00:00

URL: https://externals.io/message/5802#5803 · In reply to: #5802

Hello Markus,

this works now.

regards
marcus (the other one)

Saturday, November 29, 2003, 3:39:17 PM, you wrote:

>   When trying to derive from class Exception and access the 'string'
>   property or call 'parent::__toString' PHP segfaults:

> <?php
>     class MyException extends Exception {
>         function __toString() {
>             var_dump($this->string);
>         }
>     }

>     throw new MyException("hmm");
?>>

> Output:

> $ php testException.php
> `NULL` 
> Segmentation fault

>   Or this example:

> <?php
>     class MyException extends Exception {
>         function __toString() {
> 			parent::__toString();
>         }
>     }

>     throw new MyException("hmm");
?>>

> Output:

> $ php testException.php
> Segmentation fault


> 	The need for this came up when I saw that the reported exception is
> 	not foramted in the HTML output; it's quite hard next to impossible
> 	to properly read the stack trace. So my goal was to make my own
> 	exception and call `nl2br()` on the string returned from the class.

> 	Besides the segfaults above, is there a chance we have a nicer HTML
> 	formatting for reported Exceptions (while still strip those HTML
> 	tags from it for logging)?

> 	- Markus




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #5807 — mfischer@gjat.josefine@Markus Fischer — 2003-11-30T07:17:49+00:00

URL: https://externals.io/message/5802#5807 · In reply to: #5803

Hi Marcus,

On Sat, Nov 29, 2003 at 06:09:36PM +0100, Marcus Boerger wrote : 
> Hello Markus,
> 
> this works now.
> 
> regards
> marcus (the other one)

	Thanks, calling parent::__toString() or accessing $this->string
	works now. I've found just another scenario which causes a segfault.

    If you do not return anything from __toString() or return int or an
    object, php segfaults:

<?php
    class MyException extends Exception {
        function __toString() {
            # segfault
            #return 1;

            # segfault
            #return new StdClass

            # no segfault
            #return array("foo");

            # segfault (no return statement)
        }
    }

    throw new MyException("hmm");
?>

Output:

$ php testException.php
Segmentation fault

    - Markus

---

## #5838 — mfischer@gjat.josefine@Markus Fischer — 2003-11-30T21:59:50+00:00

URL: https://externals.io/message/5802#5838 · In reply to: #5803

Hi,

On Sat, Nov 29, 2003 at 06:09:36PM +0100, Marcus Boerger wrote : 
> this works now.

    Thanks. Sorry for the order of reply of the mails, my spam filer
    catches this one and I only discovered it yet.

> > […]

    Can you advise in this issue too?

    The thing is, the stack trace is alright and looks good, but not in
    HTML since the line breaks are ordinary ones; there's not HTML
    formatting for them, so the line break is not visible in the HTML
    output.

    If I derive my own exception and override __toString() I can call
    `nl2br()`; but then I've the HTML output in the error_log too.

    Seems like an chicken and egg problem. Another solution is to modify
    error_prepend/append to encapuslate the error output in a pre block.


    Besides this possible, would it be feasable to handle this issue
    right away in PHP for HTML and error log output? Maybe it's not
    possible anyway. Hm.

    - Markus

---

## #5849 — Marcus Boerger — 2003-12-01T06:57:29+00:00

URL: https://externals.io/message/5802#5849 · In reply to: #5838

Hello Markus,

Sunday, November 30, 2003, 10:59:50 PM, you wrote:

> 	Hi,

> On Sat, Nov 29, 2003 at 06:09:36PM +0100, Marcus Boerger wrote : 
> > […]

>     Thanks. Sorry for the order of reply of the mails, my spam filer
>     catches this one and I only discovered it yet.

> > […]

>     Can you advise in this issue too?

>     The thing is, the stack trace is alright and looks good, but not in
>     HTML since the line breaks are ordinary ones; there's not HTML
>     formatting for them, so the line break is not visible in the HTML
>     output.

>     If I derive my own exception and override __toString() I can call
>     `nl2br()`; but then I've the HTML output in the error_log too.

>     Seems like an chicken and egg problem. Another solution is to modify
>     error_prepend/append to encapuslate the error output in a pre block.

There you go.


>     Besides this possible, would it be feasable to handle this issue
>     right away in PHP for HTML and error log output? Maybe it's not
>     possible anyway. Hm.

I don't think this makes sense but maybe we *could* try to establish a
callback into main to display uncaught exceptions. But this must wait until
after 5 release i think.


-- 
Best regards,
 Marcus                            mailto:helly@php.net
