Return of the Template



  • PHP crashes right in the middle of rendering a template.

    Now the good thing about PHP is you can see where it crashed because you get output right up to that point. The bad thing is it crashes. The antagonizing thing is when I turn on error reporting it doesn't crash. And another bad thing is when it crashes it doesn't give diagnostics because segfault. My local copy runs fine throughout. Such is life with PHP, I don't even know how the PHP debugger works because the hairy stuff always happens on quirky webservers where I don't get debugging access and I can't reproduce it locally.

    Now in this case output stops in a helper function called loadWigetList. (Yes the names are changed but the original name had an annoying spelling error too.) Using tactically placed var_dump() I narrow the error down to this statement:

       // return wiget list
        return $template->assign('wigets', $wigets);
    

    Here assign() is used to set a template variable wigets which will later be used in the template. So that looks legit. Except it crashes the PHP interpreter somehow... ... When I'm stumped I like to start cleaning up the code. Sometimes this helps right away, and sometimes it just helps me to keep it calm. So I think "let's remove that useless comment. It's not even correct, because it assigns and doesn't return anything... wait...:wtf:... it does say return there". Turns out removing the return avoids the crash.

    But why would returning the result of an assign() crash PHP? Well assign() returns a reference to $template, I must assume this is for the method chaining hipsters out there. Whatever you return from a helper function will be printed into the template. Apparently PHP stumbles when trying to convert $template to a string. I'm not surprised because I've seen it before 😧



  • If it's segfaulting when trying to convert a template object to a string, it might be hitting the recursion limit. Possibly not a :wtf: in PHP (for a change).



  • @Dragnslcr Why is that a segfault and not a caught error, though? Still a WTF.


  • FoxDev

    @blakeyrat said in Return of the Template:

    Why is that a segfault and not a caught error, though?

    what langiage can you catch a segfault in? i was under the impression that a segfault ocurring more or less instantly means you are in nasal demon territory and so there is no sane thing you can do to recover because you are already operating in UB

    Accalia iz dum. no can readz.



  • @accalia Goddamned you're dumb.

    The error is running out of recursion levels, and yes that's something you can catch. Somehow BASIC managed it in like 1988, I'm pretty sure PHP could manage it 30 years later.


  • FoxDev

    @blakeyrat said in Return of the Template:

    The error is running out of recursion levels,

    okay. but that's not a segmentation error.

    you said .... oh.

    nevermind i read that wrong. you asked why the error was an uncaught segfault, not why the segfault wasn't caight

    blakeyrat wins again. all praise the glory and honor of blakeyrat.

    :rolleyes:



  • @accalia

    It might not be blakeyrat that's right, but instead me that was wrong. I'm not certain if PHP checks for recursion depth in normal function calls, or if I was mixing that up with the regex processor. PHP might just segfault when you run off the end of the stack.



  • @Dragnslcr said in Return of the Template:

    If it's segfaulting when trying to convert a template object to a string, it might be hitting the recursion limit. Possibly not a :wtf: in PHP (for a change).

    Well the segfault is a conjecture on my part, I can only observe the effects in this case. And I don't think it's the recursion limit. If it were, it should continue to fail when I enable error reporting, because error reporting should cause a deeper stack if anything. Curiously, the page renders fine with error reporting enabled.

    Note that $template does not have a __toString() method, it is not clear what causes the failure. Funnily enough, doing echo $template; does not fail, only echo $template->assign(...); does. Actually that got me thinking:

    gleemonk@abode$ php -r 'echo new stdClass();'
    PHP Catchable fatal error:  Object of class stdClass could not be converted to string in Command line code on line 1
    


  • @Dragnslcr said in Return of the Template:

    PHP might just segfault when you run off the end of the stack.

    It does:

    gleemonk@abode$ php -r 'function r() { r(); } r();'
    Segmentation fault
    


  • @blakeyrat said in Return of the Template:

    Somehow BASIC managed it in like 1988, I'm pretty sure PHP could manage it 30 years later.

    I'm pretty sure PHP is worse than BASIC.


Log in to reply