title: LeetCode C++ Convert char[] to string, throws AddressSanitizer: stack-buffer-overflow error
body: <p>I'm learning C++, and on LeetCode, converting a <code>char[]</code> to a <code>string</code> gives a <code>AddressSanitizer: stack-buffer-overflow</code> error.</p>
  <pre><code>string test1() /* Line 70 */
  {
      char test[] = &quot;11&quot;;
      return string(test);
  }
  
  string test2() /* Line 76 */
  {
      char test[] = {'1', '1'};
      return string(test);
  }
  
  int main()
  {
      cout &lt;&lt; test1() &lt;&lt; endl;
      cout &lt;&lt; test2() &lt;&lt; endl;
  }
  </code></pre>
  <p>In this code above, <code>test1</code> returns &quot;11&quot; and <code>test2</code> gives the error below with ASAN on. Why does this happen? Aren't they just different ways to initialize a char array?</p>
  <pre><code>==87465==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffee2400c22 at pc 0x00010d837634 bp 0x7ffee2400ad0 sp 0x7ffee2400290
  READ of size 3 at 0x7ffee2400c22 thread T0
  pc_0x10d837633###func_wrap_strlen###file_&lt;null&gt;###line_3###obj_(libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x1a633)
  pc_0x10d803a14###func_std::__1::char_traits&lt;char&gt;::length(char const*)###file___string###line_253###obj_(CCC:x86_64+0x100005a14)
  pc_0x10d803950###func_std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt;::basic_string&lt;std::nullptr_t&gt;(char const*)###file_string###line_819###obj_(CCC:x86_64+0x100005950)
  pc_0x10d80326c###func_std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt;::basic_string&lt;std::nullptr_t&gt;(char const*)###file_string###line_817###obj_(CCC:x86_64+0x10000526c)
  pc_0x10d80338f###func_test2()###file_p67-add-binary.cpp###line_79###obj_(CCC:x86_64+0x10000538f)
  pc_0x10d803569###func_main###file_p67-add-binary.cpp###line_85###obj_(CCC:x86_64+0x100005569)
  pc_0x7fff6cf80cc8###func_start###file_&lt;null&gt;###line_2###obj_(libdyld.dylib:x86_64+0x1acc8)
  Address 0x7ffee2400c22 is located in stack of thread T0 at offset 34 in frame
  pc_0x10d80328f###func_test2()###file_p67-add-binary.cpp###line_77###obj_(CCC:x86_64+0x10000528f)
    This frame has 1 object(s):
      [32, 34) 'test' (line 78) &lt;== Memory access at offset 34 overflows this variable
  HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
        (longjmp and C++ exceptions *are* supported)
  SUMMARY: AddressSanitizer: stack-buffer-overflow (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x1a633) in wrap_strlen+0x183
  Shadow bytes around the buggy address:
    0x1fffdc480130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x1fffdc480140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x1fffdc480150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x1fffdc480160: f1 f1 f1 f1 f8 f2 f8 f3 00 00 00 00 00 00 00 00
    0x1fffdc480170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  =&gt;0x1fffdc480180: f1 f1 f1 f1[02]f3 f3 f3 00 00 00 00 00 00 00 00
    0x1fffdc480190: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
    0x1fffdc4801a0: f8 f8 f8 f2 f2 f2 f2 f2 00 00 00 f3 f3 f3 f3 f3
    0x1fffdc4801b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x1fffdc4801c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x1fffdc4801d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  Shadow byte legend (one shadow byte represents 8 application bytes):
    Addressable:           00
    Partially addressable: 01 02 03 04 05 06 07
    Heap left redzone:       fa
    Freed heap region:       fd
    Stack left redzone:      f1
    Stack mid redzone:       f2
    Stack right redzone:     f3
    Stack after return:      f5
    Stack use after scope:   f8
    Global redzone:          f9
    Global init order:       f6
    Poisoned by user:        f7
    Container overflow:      fc
    Array cookie:            ac
    Intra object redzone:    bb
    ASan internal:           fe
    Left alloca redzone:     ca
    Right alloca redzone:    cb
    Shadow gap:              cc
  </code></pre>