Shadowchase Continued – Is There A Bug In PHP Regular Expression Handling?

Feb 20, 2009   //   by Hackadelic   //   WordPress  //  No Comments

shadowboxThe recent “Shadowchaser Release” of Sliding Notes brought – among other features – compatibility with the “Shadowbox JS” plugin.

I’ve just came across another shadow-something1 that seemed to cause trouble.

I investigated the issue, and I’ve found out some interesting things.

The Crime Scene

[toc class=toc-right]

The defect manifested in a context when a Sliding Note contained many links to images, specially annotated with the rel=”shadowbox[my-gallery-name]”, in that the whole page (not only the Sliding Note itself) became empty all of a sudden.

Note that only a couple of such items did not seem to cause trouble. It had to be many. 255 has been rerported as the critical number to me, though my investigation shows that the actual critical number may vary, depending on what else is inside the note.

Who’s Innocent?

It was clear from the beginning that the effect couldn’t have anything to do with Sliding Notes. I’ll gladly repeat that: Sliding Notes does not have a bug. At least not this bug.

Sliding Notes relies on WordPress shortcode handling (which is admitedly far from perfect). It is WordPress who supplies the content between the shorcode tags, not Sliding Notes (or any other shortcode plugin). If that content is messed up, it’s not the plugin’s fault.

So Sliding Notes was not the evildoer.

It turned out that neither was Shadowbox.

The Proof Of Innocence

I could reduce the problem scenario to a dummy shortcode that just returns whatever is inside it’s tags, and a number of 1932 or more non-existent shortcode tags. That is:

Editor text Outcome
[dummy]xxx[/dummy] xxx
[dummy][xxx][/dummy] [xxx]
[dummy][xxx][xxx][xxx] (… 193 times or more) [/dummy] Ooops! Empty page?!?

Obviously, this takes both Sliding Notes and Shadowbox out of the equation.

See No Evil, Speak No Evil

My immediate assumption was that WordPress is tracking recursive shortcodes and “gets confused” somehow when there are too many. (There are many ways to mess things up with recursions, but I won’t go into these details now.)

So I dug into shortcode.php and what I found was pretty simple code heavily based on regular expression handling. Damn! So WordPress was innocent, too!

It was now that it hit me:

There had to be a bug in PHP’s regular expression library!

The Deciding Evidence

Frankly, I don’t have any. I didn’t want to go into investigating, let alone debugging PHP source code. (Which is C, I believe. Bah!) But the “preference” of the defect to manifest along numbers of nested tags close to a multiple of 8 seems like a familiar error pattern for C programs, especially with counter overruns and bit-shift operations. (Gory stuff, I won’t torture you with that.)

Shadowbox Workaround

So, lacking a universal Answer to the Ultimate Question of Life, the Universe, and Everything, (which as Doug Adams‘ fans know is 42) I’m coming back to Shadowbox, and what to do to overcome the defect.

Shadowbox, or more precise, it’s underlying Shadowbox.js javscript, utilizes the rel attribute on links. Links need to be annotated with rel="shadowbox" for Shadowbox.js to display them. An affiliation to named galleries is indicated by rel="shadowbox[gallery_name]"

The choice of square brackets is arbitrary, and internal to Shadowbox.js. There’s no option to override it.

The easiest way to circumvent the defect is to modify Shadowbox.js to not use square brackets, but curly braces instead. Alas, if you already have many annotated links with the brackets, you’ll have a lot of work changing them all.

Here’s the “quick and dirty” way:

1. Open Shadowbox.js, and find the code line

gallery: /^(light|shadow)box[(.*?)]/i, // rel attribute format for gallery link

2. Replace it with

gallery: /^(light|shadow)box{(.*?)}/i, // rel attribute format for gallery link

3. Use the markup


That’s it. As much for “quick and dirty”. If there is enough demand, I would be willing to attempt to develop a more sophisticated solution. It means: Everybody interested – drop me a note!

  1. Anything that uses the Shadowbox.js script []
  2. Note that 193, like 255, is right next to a number which is a multiple of 8. []

Comments are closed.

Blog Categories

I have come here to chew bubblegum and kick ass...
and I'm all out of bubblegum.
-- Nada in They Live