<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Finite Hate Machine &#187; Ugly Implementation</title>
	<atom:link href="http://finite.grimoire.ca/category/ugly-implementation/feed/" rel="self" type="application/rss+xml" />
	<link>http://finite.grimoire.ca</link>
	<description>PHP is not my favourite language.</description>
	<lastBuildDate>Wed, 02 Feb 2011 15:18:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>You make this all go away</title>
		<link>http://finite.grimoire.ca/2010/12/array-keys-surprise/</link>
		<comments>http://finite.grimoire.ca/2010/12/array-keys-surprise/#comments</comments>
		<pubDate>Fri, 17 Dec 2010 15:59:04 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Language Design]]></category>
		<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=118</guid>
		<description><![CDATA[Been a while. Have a simple one &#8211; what does this print?

$arr&#91;null&#93; = &#34;Null string&#34;;
$arr&#91;false&#93; = &#34;False string&#34;;
$arr&#91;0&#93; = &#34;Zero string&#34;;
&#160;
print_r&#40;$arr&#41;;


It&#8217;s&#8230;
Array ( [] => Null string [0] => Zero string )
Surprise!
It gets better:

$arr&#91;null&#93; = &#34;Null string&#34;;
$arr&#91;false&#93; = &#34;False string&#34;;
$arr&#91;0&#93; = &#34;Zero string&#34;;
$arr&#91;''&#93; = &#34;Empty string string&#34;;

gives
Array ( [] => Empty string string [0] => Zero [...]]]></description>
			<content:encoded><![CDATA[<p>Been a while. Have a simple one &#8211; what does this print?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Null string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;False string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Zero string&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-118"></span><br />
It&#8217;s&#8230;</p>
<pre>Array ( [] => Null string [0] => Zero string )</pre>
<p>Surprise!</p>
<p>It gets better:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Null string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;False string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Zero string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Empty string string&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>gives</p>
<pre>Array ( [] => Empty string string [0] => Zero string )</pre>
<p>Hat tip to Will for this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/12/array-keys-surprise/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Just when everything was making sense</title>
		<link>http://finite.grimoire.ca/2010/01/lambdarrrgh/</link>
		<comments>http://finite.grimoire.ca/2010/01/lambdarrrgh/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 00:06:39 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=97</guid>
		<description><![CDATA[Okay, let&#8217;s say you managed to ensure that your program doesn&#8217;t exhaust the heap with anonymous functions, and that you understand the edge cases involved in function-type variables. There are still lots of ways for create_function to bite you in the glutes.

The program argument to create_function is a string. This has two immediate knock-on effects [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, let&#8217;s say you managed to ensure that your program doesn&#8217;t <a href="http://finite.grimoire.ca/2010/01/lambduh/">exhaust the heap with anonymous functions</a>, and that you <a href="http://finite.grimoire.ca/2010/01/lambdurr/">understand the edge cases involved in function-type variables</a>. There are still lots of ways for <code>create_function</code> to bite you in the glutes.</p>
<p><span id="more-97"></span></p>
<p>The program argument to <code>create_function</code> is a string. This has two immediate knock-on effects that can really ruin your afternoon.</p>
<p>One: <code>create_function</code>&#8217;s arguments are strings, which means they&#8217;re invisible to any syntax highlighting, code navigation, or refactoring features you might be using to help make sense of the code. Any attempt to implement these properly for <code>create_function</code> will be heuristic at best.</p>
<p>Two: <code>create_function</code>&#8217;s arguments are strings, which means they interact with PHP&#8217;s string interpolation rules. The following program has a very subtle bug:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$axes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$x</span>&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">&quot;return <span style="color: #006699; font-weight: bold;">$x</span> * <span style="color: #006699; font-weight: bold;">$x</span>;&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$axes</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints several errors and then nothing (FALSE); should print 1 (TRUE) */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span> <span style="color: #339933;">==</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Can you see it? Here&#8217;s the fixed version:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$axes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'$x'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'return $x * $x;'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$axes</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints 1 (TRUE) */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span> <span style="color: #339933;">==</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The PHP developers <em>do</em> think this is a problem: this is one of the motivations for the PHP closures RFC.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/lambdarrrgh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Did you think I wouldn&#8217;t recognize this compromise</title>
		<link>http://finite.grimoire.ca/2010/01/lambduh/</link>
		<comments>http://finite.grimoire.ca/2010/01/lambduh/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 21:00:07 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=94</guid>
		<description><![CDATA[On Thursday, commenter Grumqa asked how PHP deals with the slow accumulation of functions if you call create_function.
It doesn&#8217;t.


&#60;?php
for &#40;$i = 0; $i &#60; 100000; $i++&#41;
    create_function&#40;&#34;&#34;, &#34;return 0;&#34;&#41;;
?&#62;

This script fairly reliably kills the interpreter with the message &#8220;Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40 bytes) [...]]]></description>
			<content:encoded><![CDATA[<p>On Thursday, commenter <a href="/2010/01/lambdurr/comment-page-1/#comment-9">Grumqa</a> asked how PHP deals with the slow accumulation of functions if you call <code>create_function</code>.</p>
<p>It doesn&#8217;t.</p>
<p><span id="more-94"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;return 0;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This script fairly reliably kills the interpreter with the message &#8220;Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40 bytes) &#8230;&#8221;. You can postpone it by increasing PHP&#8217;s <a href="http://php.net/manual/en/ini.core.php#ini.memory-limit">memory limit</a>, but if you call <code>create_function</code> an arbitrary number of times, eventually you will murder the interpreter. The only safe way to use <code>create_function</code> is by ensuring you only call it a bounded number of times, and at that point, why not use a normal function and be done with it?</p>
<p>Naturally, <a href="http://bugs.php.net/bug.php?id=6333">the PHP team doesn&#8217;t think this is a problem</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/lambduh/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My purity you stole</title>
		<link>http://finite.grimoire.ca/2010/01/lambdurr/</link>
		<comments>http://finite.grimoire.ca/2010/01/lambdurr/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 00:26:51 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Core Library]]></category>
		<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=33</guid>
		<description><![CDATA[Yesterday, I wrote about how $f() variable-function syntax works in PHP. While it is pretty bad, it&#8217;s also the groundwork for understanding the ways in which create_function is terrible.
No, I mean besides taking a string full of code as one of its arguments.

Consider this contrived example:

1
2
3
4
5
6
7
8
9
10
11
&#60;?php
$input = array&#40;1, 2, 3&#41;;
&#160;
$monolithic_dimensions = array_map&#40;
    [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote about how <code>$f()</code> variable-function syntax works in PHP. While it is pretty bad, it&#8217;s also the groundwork for understanding the ways in which <a href="http://php.net/manual/en/function.create-function.php"><code>create_function</code></a> is terrible.</p>
<p>No, I mean besides taking a string full of code as one of its arguments.</p>
<p><span id="more-33"></span></p>
<p>Consider this contrived example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$monolithic_dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$elem'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return $elem * $elem;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$input</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints Array([0] =&gt; 1, [1] =&gt; 4, [2] =&gt; 9) */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$monolithic_dimensions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>On the face of it, this looks like a decent approximation of functional programming idiom. However, every time PHP executes <code>create_function</code>, it adds a new function to the global function table.</p>
<p>What&#8217;s really happening is a little easier to see if you look at <code>create_function</code>&#8217;s return value:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$elem'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return $elem * $elem;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <span style="color: #990000;">gettype</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$square</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Prints string */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints
0  []
108	[l]
97	[a]
109	[m]
98	[b]
100	[d]
97	[a]
95	[_]
49	[1]
*/</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$square</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$square</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>[&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Yup. PHP is creating a function at runtime, with an &#8220;impossible&#8221; name (the zero byte isn&#8217;t allowed in <code>function</code> statements) to avoid collisions with user-defined functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/lambdurr/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>I decided I was never coming down</title>
		<link>http://finite.grimoire.ca/2010/01/recursion-segfaults/</link>
		<comments>http://finite.grimoire.ca/2010/01/recursion-segfaults/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 19:04:44 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=70</guid>
		<description><![CDATA[I&#8217;m still sick, so you get another easy target today. It&#8217;s trivially easy to kill the PHP interpreter, dead, without invoking any extensions or unusual language features:

1
2
3
4
5
6
7
8
9
&#60;?php
function recurse&#40;$n&#41; &#123;
    if &#40;$n &#60;= 0&#41;
        return;
    return recurse&#40;$n - 1&#41;;
&#125;
&#160;
recurse&#40;100000&#41;; /* You may need [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still sick, so you get another easy target today. It&#8217;s trivially easy to kill the PHP interpreter, dead, without invoking any extensions or unusual language features:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> recurse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> recurse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
recurse<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* You may need to adjust this upwards. */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><span id="more-70"></span></p>
<p>This script dies unceremoniously (on Linux, with a segmentation fault) when the PHP interpreter runs off the end of its own stack. This is the kind of behaviour that gives security researchers nightmares, since <a href="http://en.wikipedia.org/wiki/Stack_buffer_overflow">segmentation faults and bus errors usually go hand in hand with code injection exploits</a>. It&#8217;s also a pain to diagnose: you need to break out <code>gdb</code>, because the PHP interpreter is too dead to explain what went wrong.</p>
<p>Other modern languages either monitor the depth of the stack and abort gracefully when it gets too large (Python, Ruby, Java, C#) or prevent large stacks from scribbling over sensitive parts of the program (Perl, Lisp, Haskell). The <a href="http://www.freshbooks.com/">FreshBooks</a> dev blog <a href="http://developers.freshbooks.com/blog/view/comparative_infinite_recursion/">illustrates</a> a few examples.</p>
<p>Predictably, the PHP developers <a href="http://bugs.php.net/bug.php?id=37612">don&#8217;t think this is a problem</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/recursion-segfaults/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I really don&#8217;t know what you mean</title>
		<link>http://finite.grimoire.ca/2010/01/indexing-expressions/</link>
		<comments>http://finite.grimoire.ca/2010/01/indexing-expressions/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 18:15:38 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Language Design]]></category>
		<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=26</guid>
		<description><![CDATA[It&#8217;s Monday, and I&#8217;m sick, so you get some low-hanging fruit today. The following snippet produces a syntax error in PHP:

1
2
3
4
5
6
7
&#60;?php
function monolithic_dimensions&#40;&#41; &#123;
    return array&#40;1, 4, 9&#41;;
&#125;
&#160;
print monolithic_dimensions&#40;&#41;&#91;0&#93;; // syntax error, unexpected '['
?&#62;


Of course, it&#8217;s trivial to work around:

1
2
3
4
5
6
&#60;?php
/* ... */
&#160;
$temp = monolithic_dimensions&#40;&#41;;
print $temp&#91;0&#93;; // Prints 1
?&#62;

In almost every other language with [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s Monday, and I&#8217;m sick, so you get some low-hanging fruit today. The following snippet produces a syntax error in PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// syntax error, unexpected '['</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><span id="more-26"></span></p>
<p>Of course, it&#8217;s trivial to work around:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
&nbsp;
<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Prints 1</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In almost every other language with array-indexing syntax, any expression can be indexed if it evaluates to an array. Consider Python:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> monolithic_dimensions<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>: 
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">9</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> monolithic_dimensions<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># Prints 1</span></pre></td></tr></table></div>

<p>Or Ruby:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> monolithic_dimensions
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> monolithic_dimensions<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;"># Prints 1</span></pre></td></tr></table></div>

<p>Or even C:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> <span style="color: #993333;">const</span> <span style="color: #339933;">*</span>monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">static</span> <span style="color: #993333;">int</span> <span style="color: #993333;">const</span> dimensions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">9</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> dimensions<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* Prints 1 */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Of course, since there&#8217;s no formal language spec for PHP, there&#8217;s no way to know whether this is by design or by accident.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/indexing-expressions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Let&#8217;s go dancing on the backs of the bruised</title>
		<link>http://finite.grimoire.ca/2010/01/raii-surprise/</link>
		<comments>http://finite.grimoire.ca/2010/01/raii-surprise/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 22:26:24 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=12</guid>
		<description><![CDATA[Remember the C++ idiom I mentioned yesterday? In PHP, it has some unique gotchas.

First, let&#8217;s look at a simple C++ RAII example, using objects that do nothing except log their own destruction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include &#60;iostream&#62;
#include &#60;string&#62;
&#160;
class LogsDestruction &#123;
public:
  LogsDestruction&#40;std::string name&#41; &#123;
    this-&#62;name = name;
  &#125;
&#160;
  ~LogsDestruction&#40;&#41; &#123;
    std::cout [...]]]></description>
			<content:encoded><![CDATA[<p>Remember the <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization#C.2B.2B_example">C++ idiom</a> I mentioned <a href="/2010/01/no-finally/">yesterday</a>? In PHP, it has some unique gotchas.</p>
<p><span id="more-12"></span></p>
<p>First, let&#8217;s look at a simple C++ RAII example, using objects that do nothing except log their own destruction:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">class</span> LogsDestruction <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  LogsDestruction<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name <span style="color: #000080;">=</span> name<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ~LogsDestruction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  LogsDestruction a<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;A&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  LogsDestruction b<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;B&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If you build and run this, you can see that objects <code>a</code> and <code>b</code> are destroyed in last-to-first order:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #007800;">CXXFLAGS</span>=<span style="color: #ff0000;">'-W -Wall -pedantic -std=c++98'</span> <span style="color: #c20cb9; font-weight: bold;">make</span> raii
<span style="color: #c20cb9; font-weight: bold;">g++</span> <span style="color: #660033;">-W</span> <span style="color: #660033;">-Wall</span> <span style="color: #660033;">-pedantic</span> <span style="color: #660033;">-std</span>=<span style="color: #c20cb9; font-weight: bold;">c++</span><span style="color: #000000;">98</span>    raii.cpp   <span style="color: #660033;">-o</span> raii
$ .<span style="color: #000000; font-weight: bold;">/</span>raii 
B
A</pre></div></div>

<p>This order is guaranteed by the C++ language spec.</p>
<p>Now let&#8217;s see what happens with the equivalent PHP program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> LogsDestruction <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	     <span style="color: #b1b100;">print</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
             <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;B&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Running this from either a web request or the PHP command-line interpreter gives:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php raii.php 
A
B</pre></div></div>

<p>Surprise!</p>
<p>This is guaranteed to bite you if you have multiple resources that need guaranteed cleanup in the same block. The only workaround available in the language is to do at most one cleanup <em>per function</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000000; font-weight: bold;">function</span> raii_example_inner<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;B&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  raii_example_inner<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/raii-surprise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

