<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chad Perrin</title>
    <description>The latest articles on DEV Community by Chad Perrin (@apotheon).</description>
    <link>https://dev.to/apotheon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F65903%2Fdbc71260-8af5-4aa0-9dd7-aad994b02091.png</url>
      <title>DEV Community: Chad Perrin</title>
      <link>https://dev.to/apotheon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/apotheon"/>
    <language>en</language>
    <item>
      <title>OpenBSD Is C Documentation</title>
      <dc:creator>Chad Perrin</dc:creator>
      <pubDate>Tue, 10 Apr 2018 20:47:43 +0000</pubDate>
      <link>https://dev.to/apotheon/openbsd-is-c-documentation-3gd1</link>
      <guid>https://dev.to/apotheon/openbsd-is-c-documentation-3gd1</guid>
      <description>&lt;p&gt;StackOverflow is the de facto standard place to go when you need to figure out how to do something as a programmer.  A web search for information about how to check the number of lines in a file using the C programming language will probably give you StackOverflow questions as the most relevant search results.  Unfortunately, while writing good, clean, reliable C does not need to be difficult, it is certainly difficult to find information about how to do it well, and the people answering these questions on StackOverflow are not filtered by their skills or knowledge of good practices.  From time to time, the top-rated answer or the accepted answer is even the worst answer that addresses the question.&lt;/p&gt;

&lt;p&gt;The best example I have seen of a straightforward line count algorithm is in a place that might seem obvious after the fact: the source code of the &lt;code&gt;wc&lt;/code&gt; command line utility.  Figure out how the &lt;code&gt;-l&lt;/code&gt; option is handled in a good implementation of &lt;code&gt;wc&lt;/code&gt;, and all that's left is to adapt it to your needs.  Ideally, the solution you find should be short, simple, careful (e.g. checking for error conditions), robust, well-worn, and well-formatted.&lt;/p&gt;

&lt;p&gt;The C coding standards of the OpenBSD community are among the highest and most pragmatic you'll ever find, and when someone makes a decision about implementation whose reasoning is not pretty obvious in the code itself you're almost certain to see that reasoning clearly documented in code comments, which can also teach you something about being a good programmer in general.  The &lt;a href="https://github.com/openbsd/src"&gt;OpenBSD source tree&lt;/a&gt; is the best place to start for such questions, in my experience, and it's where I went to check my own thinking about how to count lines recently.&lt;/p&gt;

&lt;p&gt;OpenBSD is a great place to start when all you need is some guidance on what function to use for something, as well.  You may have heard &lt;code&gt;atoi()&lt;/code&gt; is not a function you should use.  If you haven't heard before, you have now.  There are some great resources on the internet that describe why it's a terrible idea to use &lt;code&gt;atoi()&lt;/code&gt;, and a plethora of other deprecated functions in good C programming.  What should you use instead?&lt;/p&gt;

&lt;p&gt;That's a good question, and it turns out OpenBSD has the answer, in almost every case, in a manpage.  Use the &lt;code&gt;man atoi&lt;/code&gt; command on an OpenBSD system, and you'll find a caveats section of the file that talks about &lt;code&gt;strtol()&lt;/code&gt;, &lt;code&gt;strtoul()&lt;/code&gt;, and &lt;code&gt;strtonum()&lt;/code&gt; as alternatives; check their manpages for more details.  (Spoiler: &lt;code&gt;strtonum()&lt;/code&gt; is almost always what you want.)  The same applies to things like why you should generally use &lt;code&gt;snprintf()&lt;/code&gt; instead of &lt;code&gt;sprintf()&lt;/code&gt; (explained in the caveats section for both functions and a bunch of related functions), &lt;code&gt;strlcat()&lt;/code&gt; instead of &lt;code&gt;strcat()&lt;/code&gt; (in the description section of &lt;code&gt;man strcat&lt;/code&gt;), and &lt;code&gt;strncmp()&lt;/code&gt; instead of &lt;code&gt;strcmp()&lt;/code&gt; (they share a manpage, which includes information about why to choose one over the other).  If you don't have easy access to an OpenBSD system, you can always check &lt;a href="http://man.openbsd.org/cgi-bin/man.cgi"&gt;OpenBSD manpages&lt;/a&gt; online, too.&lt;/p&gt;

&lt;p&gt;The "check OpenBSD" technique is not perfect for finding the best ways to do things in all cases, but it's the quickest, easiest way to get a better idiom or function choice than you would likely have found otherwise.  In the majority of cases when something is in the OpenBSD codebase or OpenBSD's manpages it's about the best you're likely to get.  An example of a slight shortcoming is that, while the manpage for &lt;code&gt;malloc()&lt;/code&gt; and &lt;code&gt;calloc()&lt;/code&gt; does give good guidance for reasons to prefer &lt;code&gt;calloc()&lt;/code&gt; most of the time in the caveats section, it is not explicit about one of the most important reasons: zeroing out the memory you just allocated, automatically, to protect leftover data from software previously running on the system (for instance).  That manpage also talks about initialization overhead for &lt;code&gt;calloc()&lt;/code&gt;, but it turns out &lt;code&gt;calloc()&lt;/code&gt; is often &lt;em&gt;faster&lt;/em&gt; than &lt;code&gt;malloc()&lt;/code&gt; for reasons of arcane implementation details.&lt;/p&gt;

&lt;p&gt;That, however, is not something you will likely find in a web search for the best way to allocate memory in C at all, possibly excepting specific searches for all the gory details about how &lt;code&gt;malloc()&lt;/code&gt; and &lt;code&gt;calloc()&lt;/code&gt; work under the hood.  The fact that StackOverflow answers will give &lt;code&gt;malloc()&lt;/code&gt; solutions where &lt;code&gt;calloc()&lt;/code&gt; would be better, as often as not, helps make my point.  There's a lot of information on StackOverflow, but let the reader beware.  Sanity-check the answers against OpenBSD source code and documentation.&lt;/p&gt;

&lt;p&gt;In short, when you need to write C, OpenBSD is excellent C documentation.&lt;/p&gt;

</description>
      <category>c</category>
      <category>documentation</category>
      <category>openbsd</category>
      <category>programming</category>
    </item>
    <item>
      <title>Crypto Is Hard</title>
      <dc:creator>Chad Perrin</dc:creator>
      <pubDate>Wed, 04 Apr 2018 20:02:22 +0000</pubDate>
      <link>https://dev.to/apotheon/crypto-is-hard-3fbe</link>
      <guid>https://dev.to/apotheon/crypto-is-hard-3fbe</guid>
      <description>&lt;p&gt;Software is easy to get wrong, and security software needs to be right.  This makes good security software hard.&lt;/p&gt;

&lt;p&gt;It's especially hard for beginners, because (in my experience) good documentation, examples, and tutorials for common, "hello world" level security software development that actually does things right is nearly nonexistent.  Try finding an example of how to write a tool that takes a password to encrypt and decrypt text using ChaCha20, for instance, or even AES, that is comprehensible to someone who has been writing web applications, test frameworks, and Unix command line tools.&lt;/p&gt;

&lt;p&gt;This is a big problem, but nobody seems to have noticed, or to care.&lt;/p&gt;

</description>
      <category>cryptography</category>
      <category>documentation</category>
      <category>security</category>
    </item>
  </channel>
</rss>
