-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
719 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="generator" content="pandoc" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> | ||
<title>Cursed Code Collection</title> | ||
</head> | ||
<body> | ||
<h1 id="cursed-code-collection">Cursed Code Collection</h1> | ||
<hr> | ||
<p>The shebang for C is just a little bit longer than one line.</p> | ||
<div class="sourceCode" id="cb1"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#if 0</span></span> | ||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co">TMP=</span><span class="st">"$(mktemp -d)"</span></span> | ||
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co">cc -o </span><span class="st">"$TMP/a.out"</span><span class="co"> -x c </span><span class="st">"$0"</span><span class="co"> && </span><span class="st">"$TMP/a.out"</span><span class="co"> $@</span></span> | ||
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co">RVAL=$?</span></span> | ||
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co">rm -rf </span><span class="st">"$TMP"</span></span> | ||
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co">exit $RVAL</span></span> | ||
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="pp">#endif</span></span> | ||
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdio.h></span></span> | ||
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">()</span> <span class="op">{</span></span> | ||
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> puts<span class="op">(</span><span class="st">"This is such a cool bash script!"</span><span class="op">);</span></span> | ||
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> | ||
<hr> | ||
<p>Open a file, swap the nibbles of every byte, and write it back. I'm | ||
not sure if this error handling macro is big brain or small brain. For | ||
sure it wouldn't pass code review.</p> | ||
<div class="sourceCode" id="cb2"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdlib.h></span></span> | ||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><unistd.h></span></span> | ||
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><fcntl.h></span></span> | ||
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><sys/stat.h></span></span> | ||
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdio.h></span></span> | ||
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><errno.h></span></span> | ||
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="dt">char</span><span class="op">*</span> b<span class="op">;</span></span> | ||
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="dt">size_t</span> i<span class="op">;</span></span> | ||
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> fd<span class="op">;</span></span> | ||
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> stat st<span class="op">;</span></span> | ||
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a><span class="pp">#define EH(fn,...) fn(__VA_ARGS__); if (errno) return perror(#fn), 1;</span></span> | ||
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a><span class="pp">#define sz st.st_size</span></span> | ||
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">(</span><span class="dt">int</span> argc<span class="op">,</span> <span class="dt">char</span><span class="op">**</span> argv<span class="op">)</span> <span class="op">{</span></span> | ||
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="op">(</span>argc <span class="op">!=</span> <span class="dv">3</span><span class="op">)</span> <span class="cf">return</span> <span class="dv">1</span><span class="op">;</span></span> | ||
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a> EH<span class="op">(</span>stat<span class="op">,</span> argv<span class="op">[</span><span class="dv">1</span><span class="op">],</span> <span class="op">&</span>st<span class="op">);</span></span> | ||
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a> b <span class="op">=</span> EH<span class="op">(</span>malloc<span class="op">,</span> sz<span class="op">);</span></span> | ||
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a> fd <span class="op">=</span> EH<span class="op">(</span>open<span class="op">,</span> argv<span class="op">[</span><span class="dv">1</span><span class="op">],</span> O_RDONLY<span class="op">);</span></span> | ||
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a> EH<span class="op">(</span>read<span class="op">,</span> fd<span class="op">,</span> b<span class="op">,</span> sz<span class="op">);</span></span> | ||
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> <span class="op">(</span>i <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> i <span class="op"><</span> sz<span class="op">;</span> <span class="op">++</span>i<span class="op">)</span></span> | ||
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a> b<span class="op">[</span>i<span class="op">]</span> <span class="op">=</span> <span class="op">((</span>b<span class="op">[</span>i<span class="op">]</span> <span class="op">&</span> <span class="bn">0x0F</span><span class="op">)</span> <span class="op"><<</span> <span class="dv">4</span><span class="op">)</span> <span class="op">|</span> <span class="op">((</span>b<span class="op">[</span>i<span class="op">]</span> <span class="op">&</span> <span class="bn">0xF0</span><span class="op">)</span> <span class="op">>></span> <span class="dv">4</span><span class="op">);</span></span> | ||
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a> fd <span class="op">=</span> EH<span class="op">(</span>creat<span class="op">,</span> argv<span class="op">[</span><span class="dv">2</span><span class="op">],</span> S_IRWXU<span class="op">);</span></span> | ||
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a> EH<span class="op">(</span>write<span class="op">,</span> fd<span class="op">,</span> b<span class="op">,</span> sz<span class="op">);</span></span> | ||
<span id="cb2-26"><a href="#cb2-26" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> | ||
<hr> | ||
<p>Annihilate your python runtime.</p> | ||
<div class="sourceCode" id="cb3"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>da, di <span class="op">=</span> <span class="bu">delattr</span>, <span class="bu">dir</span></span> | ||
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>shred_runtime <span class="op">=</span> <span class="kw">lambda</span>: [da(__builtins__, a) <span class="cf">for</span> a <span class="kw">in</span> di(__builtins__)]</span> | ||
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>shred_runtime()</span> | ||
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co"># print() is no longer defined</span></span> | ||
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Hello World!"</span>)</span></code></pre></div> | ||
<hr> | ||
<p>Use generics to figure out what version of <code>strerror_r</code> | ||
you have. This is less cursed code, and more just the most sane way of | ||
doing things. But the fact that <code>_Generic</code> is actually the | ||
best solution to any problem is baffling, and cursed in its own | ||
right.</p> | ||
<div class="sourceCode" id="cb4"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><string.h></span></span> | ||
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdio.h></span></span> | ||
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="pp">#define STRERR_R _Generic(strerror_r, \</span></span> | ||
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="pp"> int(*)(int, char*, size_t): "XSI", \</span></span> | ||
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="pp"> char*(*)(int, char*, size_t): "GNU", \</span></span> | ||
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="pp"> default: "UNK")</span></span> | ||
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">(</span><span class="dt">void</span><span class="op">)</span> <span class="op">{</span></span> | ||
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a> puts<span class="op">(</span>STRERR_R<span class="op">);</span></span> | ||
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> | ||
<hr> | ||
<p><code>eval()</code> exists in javascript and python. It takes code as | ||
a string and runs it. What if I told you it existed in C as well?</p> | ||
<div class="sourceCode" id="cb5"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdio.h></span></span> | ||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdlib.h></span></span> | ||
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><dlfcn.h></span></span> | ||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><unistd.h></span></span> | ||
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a><span class="dt">void</span> eval<span class="op">(</span><span class="dt">char</span><span class="op">*</span> program<span class="op">,</span> <span class="dt">char</span><span class="op">*</span> symbol<span class="op">)</span> <span class="op">{</span></span> | ||
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">FILE</span><span class="op">*</span> tmpf <span class="op">=</span> fopen<span class="op">(</span><span class="st">"/tmp/libtmp.c"</span><span class="op">,</span> <span class="st">"w"</span><span class="op">);</span></span> | ||
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> fprintf<span class="op">(</span>tmpf<span class="op">,</span> program<span class="op">);</span></span> | ||
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> fclose<span class="op">(</span>tmpf<span class="op">);</span></span> | ||
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> system<span class="op">(</span><span class="st">"cc /tmp/libtmp.c -shared -o /tmp/libtmp.so"</span><span class="op">);</span></span> | ||
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="op">((</span><span class="dt">void</span><span class="op">(*)(</span><span class="dt">void</span><span class="op">))</span>dlsym<span class="op">(</span>dlopen<span class="op">(</span><span class="st">"/tmp/libtmp.so"</span><span class="op">,</span> RTLD_LAZY<span class="op">),</span> symbol<span class="op">))();</span></span> | ||
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> | ||
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">()</span> <span class="op">{</span></span> | ||
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> <span class="dt">char</span><span class="op">*</span> hello_world <span class="op">=</span> <span class="op">(</span><span class="dt">char</span><span class="op">*)</span><span class="st">"#include <stdio.h></span><span class="sc">\n</span><span class="st">"</span></span> | ||
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="st">"void hello(void) {</span><span class="sc">\n</span><span class="st">"</span></span> | ||
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> <span class="st">" puts(</span><span class="sc">\"</span><span class="st">Hello World!</span><span class="sc">\"</span><span class="st">);</span><span class="sc">\n</span><span class="st">"</span></span> | ||
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a> <span class="st">"}"</span><span class="op">;</span></span> | ||
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a> eval<span class="op">(</span>hello_world<span class="op">,</span> <span class="st">"hello"</span><span class="op">);</span></span> | ||
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> | ||
<hr> | ||
<p>My favorite hello world program. The compiler replaces the while loop | ||
with a call to memset, and it calls our memset rather than the one from | ||
the stdlib, because that one hasn't been included.</p> | ||
<div class="sourceCode" id="cb6"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdio.h></span></span> | ||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="dt">void</span><span class="op">*</span> memset<span class="op">(</span><span class="dt">void</span><span class="op">*</span> s<span class="op">,</span> <span class="dt">int</span> c<span class="op">,</span> <span class="dt">size_t</span> n<span class="op">)</span> <span class="op">{</span></span> | ||
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> puts<span class="op">(</span><span class="st">"Hello world"</span><span class="op">);</span></span> | ||
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> | ||
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">(</span><span class="dt">int</span> argc<span class="op">,</span> <span class="dt">char</span><span class="op">**</span> argv<span class="op">)</span> <span class="op">{</span></span> | ||
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span> <span class="op">(</span>argc<span class="op">--)</span> argv<span class="op">[</span>argc<span class="op">]</span> <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span> | ||
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> | ||
<hr> | ||
<p>Melt your compiler. GCC does the right thing and tries to read | ||
/dev/urandom forever. For some reason, clang does not, doesn't even | ||
error, and compiles a functional hello world program.</p> | ||
<div class="sourceCode" id="cb7"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"></dev/urandom></span></span> | ||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im"><stdio.h></span></span> | ||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span> | ||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">()</span> <span class="op">{</span></span> | ||
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> puts<span class="op">(</span><span class="st">"Hello world!"</span><span class="op">);</span></span> | ||
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> | ||
</body> | ||
</html> |
Oops, something went wrong.