scrivo / highlight.php

A port of highlight.js by Ivan Sagalaev to PHP
BSD 3-Clause "New" or "Revised" License
695 stars 45 forks source link

Infinite loop in JsonRef::resolvePathReferences #33

Closed assertchris closed 5 years ago

assertchris commented 5 years ago

Library version: ^9.13 PHP version: 7.3.1

I'm trying to highlight javascript code, and the highlighter gets stuck in an infinite loop while trying to resolvePathReferences. Here's a GIF of the diagnosis:

loop

The code it is compiling for is:

const copyToClipboard = function(string) {
    const element = document.createElement('textarea')
    document.body.appendChild(element)

    element.value = string
    element.select()

    // this copies currently selected input text
    document.execCommand('copy')

    document.body.removeChild(element)
}

$('[data-copy-code]').on('click', function(e) {
    copyToClipboard(this.dataset.copyCode)
})

...but I don't think the code is the problem here. I suspect a recent-ish change to the javascript syntax has caused this infinite loop behaviour.

Edit: after further investigation, I don't think think a syntax definition change is responsible, since the definition file hasn't changed recently in a way that would cause new behaviour. Still unsure of the cause.

allejo commented 5 years ago

I tested running the highlighter on PHP 7.2.10 and it runs just fine. However, it goes into an infinite loop starting with PHP 7.3.0 so something changed between 7.2 and 7.3 that is causing this infinite loop.

The behavior of how references are returned has changed in 7.3.0 and I believe this change in behavior is causing JsonRef::resolvePathReferences() to incorrectly nest into references it's already visited.

GKephart commented 5 years ago

I have come across the same issue.

screen shot 2019-01-13 at 9 56 02 pm
allejo commented 5 years ago

Thanks for the report and fix, @assertchris! Really appreciate it 👍

v9.13.1.1 has been released with your changes from #34 included. I've also added PHP 7.3 and nightly to Travis so that will hopefully help us find these problems sooner.

assertchris commented 5 years ago

Thanks for sorting this out so quickly, and sorry for not getting back to you sooner about it!