title: Why does ALSA snd_pcm_hw_params_get_buffer_size react to the size of local stack frame allocations?
body: <p>Distro: Debian 9.11</p>
  
  <p>Compiler: gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516</p>
  
  <p>ALSA version: Advanced Linux Sound Architecture Driver Version k4.9.0-11-amd64.</p>
  
  <p>Please consider the following minimal reproducible example (now updated, with more error handling):</p>
  
  <pre><code>/**
   * Trying to set some parameters for ALSA.
   *
   * Code from: http://equalarea.com/paul/alsa-audio.html
   *
   */
  
  #include &lt;alsa/asoundlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;math.h&gt;
  
  #define PCM_DEVICE "default"
  
  int main(int argc, char **argv)
  {
      unsigned int err;
      snd_pcm_t *pcm_handle;
      snd_pcm_hw_params_t *params;
  
      if((err = snd_pcm_open(&amp;pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0)) &lt; 0) {
          printf("Error: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(err));
  
          return 1;
      }
  
      snd_pcm_hw_params_alloca(&amp;params);
  
      snd_pcm_hw_params_any(pcm_handle, params);
  
      if((err = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) &lt; 0) {
          printf("Error: Can't set interleaved mode. %s\n", snd_strerror(err));
  
          return 1;
      }
  
      if((err = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_FLOAT)) &lt; 0) {
          printf("Error: Can't set format. %s\n", snd_strerror(err));
  
          return 1;
      }
  
      int channels = 2;
      if((err = snd_pcm_hw_params_set_channels(pcm_handle, params, channels)) &lt; 0) {
          printf("Error: Can't set channels number. %s\n", snd_strerror(err));
  
          return 1;
      }
  
      int rate = 44100;
      if((err = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &amp;rate, 0)) &lt; 0) {
          printf("Error: Can't set rate. %s\n", snd_strerror(err));
  
          return 1;
      }
  
      printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
      printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
  
      int ch;
      if((err = snd_pcm_hw_params_get_channels(params, &amp;ch)) &lt; 0) {
          printf("Error: Unable to get the number of channels: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      if(ch &gt; 1) {
          printf("Channels: %i, stereo\n", ch);
      }
      else if(ch == 1) {
          printf("Channels: %i, mono\n", ch);
      }
      else {
          printf("Error: Unknown number of channels (%d).\n", ch);
  
          return 1;
      }
  
      if((err = snd_pcm_hw_params_get_rate(params, &amp;ch, 0)) &lt; 0) {
          printf("Error: Unable to get the rate: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      printf("Rate: %d bps\n", ch);
  
      int dir;
      snd_pcm_uframes_t period_size = 1024;
  
      printf("Attempting to set the period size to %d\n", period_size);
  
      if((err = snd_pcm_hw_params_set_period_size_near(pcm_handle, params, &amp;period_size, &amp;dir)) &lt; 0) {
          printf("Error: Unable to set the period size: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      printf("Attempting to get the period size.\n");
  
      if((err = snd_pcm_hw_params_get_period_size(params, &amp;period_size, 0)) &lt; 0) {
          printf("Error: Unable to get the period size: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      printf("Period size is now: %zd\n", period_size);
  
      int period_time = -1;
  
      printf("Attempting to get the period time.\n");
  
      if((err = snd_pcm_hw_params_get_period_time(params, &amp;period_time, NULL)) &lt; 0) {
          printf("Error: Unable to get the period time: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      printf("Period time: %d\n", period_time);
  
      int buffer_size = 4096;
  
      printf("Attempting to set the buffer size to: %d\n", buffer_size);
  
      if((err = snd_pcm_hw_params_set_buffer_size(pcm_handle, params, buffer_size)) &lt; 0) {
          printf("Error: Unable to set the buffer size: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      printf("Finalizing hw params.\n");
  
      if((err = snd_pcm_hw_params(pcm_handle, params)) &lt; 0) {
          printf("Error: Can't set harware parameters. %s\n", snd_strerror(err));
  
          return 1;
      }
  
      snd_pcm_uframes_t temp;
  
      printf("Attempting to get the buffer size.\n");
  
      if((err = snd_pcm_hw_params_get_buffer_size(params, &amp;temp)) &lt; 0) {
          printf("Error: Unable to get the buffer size: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      printf("Buffer size is now: %d\n", temp);
  
      if((err = snd_pcm_drain(pcm_handle)) &lt; 0) {
          printf("Error: Unable to drain the pcm handle: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      if((err = snd_pcm_close(pcm_handle)) &lt; 0) {
          printf("Error: Unable to close the pcm handle: %s\n", snd_strerror(err));
  
          return 1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>When char test[5] exists, I get this output:</p>
  
  <pre><code>PCM name: 'default'
  PCM state: OPEN
  Channels: 2, stereo
  Rate: 44100 bps
  Attempting to set the period size to 1024
  Attempting to get the period size.
  Period size is now: 1024
  Attempting to get the period time.
  Period time: -1
  Attempting to set the buffer size to: 4096
  Finalizing hw params.
  Attempting to get the buffer size.
  Buffer size is now: 523774
  </code></pre>
  
  <p>Note the "Buffer size is now: 523774". The expected output is given below:</p>
  
  <p>However, if I change test[5] to test[4] or less (or remove the statement altogether), recompile, and run, I get:</p>
  
  <pre><code>PCM name: 'default'
  PCM state: OPEN
  Channels: 2, stereo
  Rate: 44100 bps
  Attempting to set frames to 1024
  Frames is now: 1024
  Attempting to set the buffer size to: 4096
  Buffer size is now: 4096
  </code></pre>
  
  <p>I must be abusing the ALSA API, be doing something else weird I don't realize, or the ALSA API is broken.</p>
  
  <p>Why does it react to this trivial stack allocation of 5 bytes?</p>
  
  <p>Note that a higher value than 5 also produces this problem, such as a word aligned number of 32. I don't think has to do with stack alignment or something of that sort.</p>
  
  <p><strong>Compiler notes, how to reproduce:</strong></p>
  
  <p>Copy/paste the minimal example into broken_alsa.c</p>
  
  <p>Then issue:</p>
  
  <pre><code>$ gcc broken_alsa.c -lasound
  $ ./a.out
  </code></pre>
  
  <p>Remove the char temp[5] (or set it to 4), and it should work.</p>