title: GCC generate Canary or not?
body: <p>my gcc version is 4.8.2 and operating system is ubuntu 14.04 (64 bit).
  I found that sometimes gcc auto generate the canary to do buffer overflow protection sometimes not, why?</p>
  
  <p>case to generate canary: when SIZE is multiple of four</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #define SIZE 4
  
  int main()
  {
      char s[SIZE];
      scanf("%s", s);
      return 0;
  }
  </code></pre>
  
  <p>asm after gcc -c -g -Wa,-a,-ad</p>
  
  <pre><code>...
     4:a.c           **** int main()
     5:a.c           **** {
    13                    .loc 1 5 0
    14                    .cfi_startproc
    15 0000 55            pushq   %rbp
    16                    .cfi_def_cfa_offset 16
    17                    .cfi_offset 6, -16
    18 0001 4889E5        movq    %rsp, %rbp
    19                    .cfi_def_cfa_register 6
    20 0004 4883EC10      subq    $16, %rsp
    21                    .loc 1 5 0
    22 0008 64488B04      movq    %fs:40, %rax
    22      25280000 
    22      00
    23 0011 488945F8      movq    %rax, -8(%rbp)
    24 0015 31C0          xorl    %eax, %eax
     6:a.c           ****     char s[SIZE];
     7:a.c           ****     scanf("%s", s);
  ...
  </code></pre>
  
  <p>case not to  generate canary : not the multiple of four</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #define SIZE 2
  
  int main()
  {
      char s[SIZE];
      scanf("%s", s);
      return 0;
  }
  </code></pre>
  
  <p>asm after gcc -c -g -Wa,-a,-ad</p>
  
  <pre><code>...
     4:a.c           **** int main()
     5:a.c           **** {
    13                    .loc 1 5 0
    14                    .cfi_startproc
    15 0000 55            pushq   %rbp
    16                    .cfi_def_cfa_offset 16
    17                    .cfi_offset 6, -16
    18 0001 4889E5        movq    %rsp, %rbp
    19                    .cfi_def_cfa_register 6
    20 0004 4883EC10      subq    $16, %rsp
     6:a.c           ****     char s[SIZE];
     7:a.c           ****     scanf("%s", s);
  ...
  </code></pre>