<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Carla Souza on Medium]]></title>
        <description><![CDATA[Stories by Carla Souza on Medium]]></description>
        <link>https://medium.com/@carlaas?source=rss-618231b13b05------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_ZANBPmN_cNfGf-J.jpg</url>
            <title>Stories by Carla Souza on Medium</title>
            <link>https://medium.com/@carlaas?source=rss-618231b13b05------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 01:10:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@carlaas/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My first bash bug debugging, part 2]]></title>
            <link>https://medium.com/@carlaas/my-first-bashs-bug-debugging-part-2-1282a43df0b4?source=rss-618231b13b05------2</link>
            <guid isPermaLink="false">https://medium.com/p/1282a43df0b4</guid>
            <category><![CDATA[gdb]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[bash]]></category>
            <dc:creator><![CDATA[Carla Souza]]></dc:creator>
            <pubDate>Sat, 26 Nov 2016 00:33:42 GMT</pubDate>
            <atom:updated>2016-11-27T17:24:38.666Z</atom:updated>
            <content:encoded><![CDATA[<h4>The reason for bash’s crash wasn’t an int overflow, but lack of memory available.</h4><p><em>Part 1 is available </em><a href="https://medium.com/@carlaas/my-first-bashs-bug-debugging-eac4d423e2e9#.50ih9sk7p"><em>here</em></a><em>.</em></p><h4>Bash wasn’t trying to allocate a negative number.</h4><p>I didn’t debug deep enough. Also, I didn’t know C enough.</p><p>The functionxmalloc() takes size_t type as argument, as we can see in its source code below. size_t is an unsigned integer type used to represent the sizes of objects [1].</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/773d4725a59111f94af2920d87b9f592/href">https://medium.com/media/773d4725a59111f94af2920d87b9f592/href</a></iframe><p>What does it mean?</p><p>It means that even if I invoke xmalloc() with a negative integer number, size_t type will convert that integer into a not signed number.</p><p>Let’s see it happening with gdb:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4156659c420ed2048767bc32e8408b6/href">https://medium.com/media/a4156659c420ed2048767bc32e8408b6/href</a></iframe><p>The “conversion” happens because both have same binary representation.</p><p>Original int value: -294967280<br>Byte representation: 11101110011010110010100000010000</p><p>size_t conversion: 4000000016<br>Byte representation: 11101110011010110010100000010000</p><h3>Conclusion</h3><p>The root cause for bash’s crash was that my laptop didn’t have 4gb of memory available to be allocated (thanks to Google Chrome using all of it already).</p><p>[1] size_t: <a href="https://www.gnu.org/software/libc/manual/html_node/Important-Data-Types.html">https://www.gnu.org/software/libc/manual/html_node/Important-Data-Types.html</a></p><p><em>Thanks to Nenad Stojanovski for pointing the real root cause.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1282a43df0b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My first bash bug debugging]]></title>
            <link>https://medium.com/@carlaas/my-first-bashs-bug-debugging-eac4d423e2e9?source=rss-618231b13b05------2</link>
            <guid isPermaLink="false">https://medium.com/p/eac4d423e2e9</guid>
            <category><![CDATA[strace]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[gdb]]></category>
            <dc:creator><![CDATA[Carla Souza]]></dc:creator>
            <pubDate>Fri, 25 Nov 2016 20:50:21 GMT</pubDate>
            <atom:updated>2016-12-04T00:31:39.483Z</atom:updated>
            <content:encoded><![CDATA[<p>One day last week, I tried to open my terminal and I was surprised by this error:</p><pre>-bash: xmalloc: cannot allocate 4000000016 bytes</pre><p>Wat</p><p>Bash was trying to allocate 4Gb of memory? Why?</p><p>Looking at my <em>Frankenstein</em> .bashrc file, I decided that it was time to try to understand what each variable actually does. I like to keep everything I ever executed in my history, with timestamps and multi line support. I started cleaning it, and found out that this issue stopped happening once I removed the variables responsible for my bash’s history size.</p><p>These are the ones to blame:</p><pre>export HISTSIZE=500000000 <br>export HISTFILESIZE=500000000</pre><p>I had both $HISTSIZE and $HISTFILESIZE set up to 500,000,000 lines. It&#39;s a reasonable number, right? \s</p><p>But what do they actually do?</p><ul><li>HISTFILESIZE: the maximum number of commands to store in your $HISTFILE file, which by default is ~/.bash_history.</li><li>$HISTSIZE: the maximum number of commands that will be kept in memory and flushed to disk (into $HISTFILE) once you exit the shell. It is also used to load the last $HISTSIZE lines from your history file into memory when you open a new shell.</li></ul><p>Now back to the problem. Let’s invoke our friend strace. I left only the important lines of the trace:</p><pre>$ strace -f bash<br>...<br>[pid 11677] open(&quot;/home/carla/.bash_history&quot;, O_RDONLY) = 3 <br>[pid 11677] fstat(3, {st_mode=S_IFREG|0600, st_size=16755, ...}) = 0 <br>[pid 11677] read(3, &quot;ls\n#1478028902\n./i3-get-window-c&quot;..., 16755) = 16755 <br>[pid 11677] close(3) = 0 <br>[pid 11677] chown(&quot;/home/carla/.bash_history&quot;, 1000, 100) = 0 <br>[pid 11677] stat(&quot;/home/carla/.bash_history&quot;, {st_mode=S_IFREG|0600, st_size=16755, ...}) = 0 <br>[pid 11677] open(&quot;/home/carla/.bash_history&quot;, O_RDONLY) = 3 <br>[pid 11677] fstat(3, {st_mode=S_IFREG|0600, st_size=16755, ...}) = 0 <br>[pid 11677] read(3, &quot;ls\n#1478028902\n./i3-get-window-c&quot;..., 16755) = 16755 <br>[pid 11677] close(3) = 0 <br>[pid 11677] mmap(NULL, 4000002048, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory) <br>[pid 11677] brk(0xf0e0a000) = 0x2746000 <br>[pid 11677] mmap(NULL, 4000137216, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory) <br>[pid 11677] mmap(NULL, 134217728, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x7f9e145ae000 <br>[pid 11677] munmap(0x7f9e145ae000, 61153280) = 0 <br>[pid 11677] munmap(0x7f9e1c000000, 5955584) = 0 <br>[pid 11677] mprotect(0x7f9e18000000, 135168, PROT_READ|PROT_WRITE) = 0 <br>[pid 11677] mmap(NULL, 4000002048, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory) <br>[pid 11677] write(2, &quot;-bash: xmalloc: cannot allocate &quot;..., 49-bash: xmalloc: cannot allocate 4000000016 bytes ) = 49 <br>[pid 11677] exit_group(2) = ? <br>[pid 11677] +++ exited with 2 +++<br>...</pre><p>strace is telling me that as part of starting bash, it reads my .bash_history and then allocate memory right after. It was trying to allocate 4,000,000,016 bytes (4Gb) of memory. My first reaction was to blame Google Chrome for using all of my memory, causing memory starvation of my dear friend, bash.</p><p>This is all the information I have until this point: the history variables in my .bashrc, $HISTSISZE and $HISTFILESIZE, set to remember and store 500,000,000 commands, was causing bash to allocate 4Gb of memory upon start.</p><h3>My Hypothesis</h3><p>Let’s do some math.</p><p>How are these two numbers related? My first guess was to divide one by the other:</p><p>(4000000000 + 16) / (500000000 + 2) = 8 bytes per line, where this 2 is being added for some unknown reason.</p><p>strace can’t help me any further at this time, so I downloaded the source code. I made sure I had the same version as the one installed on my laptop (GNU bash, version 4.4.0(1)-release).</p><p>Now it’s time to use GDB. The code needs to be compiled with extra flags to allow the debugging.</p><pre>$ cd ~/bash/bash/<br>$ ./configure<br>$ CFLAGS=&#39;-g -Wall -Wextra&#39; make</pre><p>I cleaned my .bashrc, leaving only these two lines:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d66fb1366c3e6f49588578fc45f522c/href">https://medium.com/media/1d66fb1366c3e6f49588578fc45f522c/href</a></iframe><p>After glancing into the code (a.k.a using grep and find for the word ‘history’), I found a file I thought would lead me to the reason it was trying to allocate memory. Let’s look atlib/readline/history.c, specifically the function add_history (line_start), defined at line 276.</p><p>Now we’re good to go. Digging into the code with gdb:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7e58b55a91eb42de8725d4b95548d35/href">https://medium.com/media/b7e58b55a91eb42de8725d4b95548d35/href</a></iframe><p>BOOM! Int overflow.</p><p>Let’s try to understand what happened here.</p><p>This is the important information for us at the moment: history_max_entries equals 500000000, which is the value I have for $HISTSIZE on my .bashrc.</p><p>How does it end up there? It all starts when bash wants to load history: load_history() at bashhist.c:300. It fetches the value of $HISTSIZE from your environment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec2a47a1f4d06fd86d4f5ad8688d977f/href">https://medium.com/media/ec2a47a1f4d06fd86d4f5ad8688d977f/href</a></iframe><p>Let’s see what this function sv_histsize on line 12 does. It is defined at lib/readline/bind.c line 1825.</p><p>As a bonus we also can see that it sets the default value to 500 again. Why is it set twice, I’m not sure.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e30716e233be80ce538d36fe75984d2/href">https://medium.com/media/9e30716e233be80ce538d36fe75984d2/href</a></iframe><p>I see that nval will have my $HISTSIZE value. Now let’s see what the function stifile_history (nval).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8433cde9e89aeef31498dee4a0fdf953/href">https://medium.com/media/8433cde9e89aeef31498dee4a0fdf953/href</a></iframe><p>Remember the variable history_max_entries? The function above, on line 25, sets its value to 500000000.</p><p>As we saw in the GDB output, 500000000was then assigned to the variablehistory_size, and later multiplied by 8 as argument to the functionxmalloc(), causing the int overflow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbc5c61b085f879701198e951a41ad8d/href">https://medium.com/media/dbc5c61b085f879701198e951a41ad8d/href</a></iframe><p>This bug was introduced on September 15th (a0c0a00), on the 4.4 release and fixed in patch 1 on November 14th (8ddc8d6). The fix was to set a maximum limit for initial memory allocation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5de0a4813dfa21ec5521a82b77a9f523/href">https://medium.com/media/5de0a4813dfa21ec5521a82b77a9f523/href</a></iframe><h3>Update</h3><p>Nenad Stojanovski pointed that my conclusion is not correct. The real reason for bash’s crash wasn’t an int overflow, but lack of memory available. The detailed explanation is available <a href="https://medium.com/@carlaas/my-first-bashs-bug-debugging-part-2-1282a43df0b4#.fb5a1fz49">here</a>.</p><h3>What I learned</h3><p>There is no point into setting the $HISTSIZE more than 8192, since its limit was now set on the patch (unless it is being used elsewhere, which I didn’t look for. If so, please let me know).</p><p>So I changed my .bashrc to:</p><pre>export HISTSIZE=8192 <br>export HISTFILESIZE=500000000</pre><p>But then I remembered that I read somewhere we could improve bash’s history even more. Instead of keeping in memory the commands to later be appended to history file (and if you crash your terminal emulator, you’ll lose it), we can flush the commands as soon as we run them. To do that, we just need to add to .bashrc:</p><pre>shopt -s histappend <br>export PROMPT_COMMAND=&#39;history -a&#39;</pre><p>Now we could decrease even more the $HISTSIZE because we don’t need to keep in memory.</p><p>But there are two caveats:</p><ul><li>if you work with multiple terminals (like me) and is used to go back (arrow up) for the previous commands in a certain terminal, all your commands from all open terminals will be all mixed together, ordered by timestamp;</li><li>it will increase disk IO since it is flushing into the $HISTFILE at every command.</li></ul><p>So I changed back my mind, kept at the max 8192 and removed the append feature.</p><p>More info on bash’s variables: <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html">https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html</a></p><h3>TL;DR</h3><p>When opening a terminal having bash as $SHELL, bash allocates $HISTSIZE * 8 bytes in memory to remember the last $HISTSIZE commands. If your $HISTSIZE is too big and you don’t have enough memory available, you’re gonna have a bad time.</p><p><em>I would like to thank </em><a href="http://github.com/thiagoss"><em>Thiago Sousa</em></a><em> for guiding me through my first GDB debugging, along with </em><a href="http://github.com/pgray"><em>Patrick Gray</em></a><em>, </em><a href="http://github.com/andremilk"><em>Andre Leite</em></a><em>, </em><a href="http://github.com/dieb"><em>Andre Dieb</em></a><em> and </em><a href="http://github.com/raissaa"><em>Raissa Sarmento</em></a><em> for reviewing this post.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eac4d423e2e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>