Skip to content

Instantly share code, notes, and snippets.

@tonyarkles
Last active October 6, 2021 16:25
Show Gist options
  • Save tonyarkles/e702e4d5b530a9b7cd3fc9837c6609f2 to your computer and use it in GitHub Desktop.
Save tonyarkles/e702e4d5b530a9b7cd3fc9837c6609f2 to your computer and use it in GitHub Desktop.
Overflowing C++ Vectors

First example:

#include <vector>
#include <stdio.h>

int main(int argc, char* argv[]) {
  std::vector<int> foo(10, 0);
  std::vector<int> bar(10, 1);

  for(int i = 0; i < 10; i++) {
    printf("foo[%d] = %d\n", i, foo[i]);
  }
  for(int i = 0; i < 10; i++) {
    printf("bar[%d] = %d\n", i, bar[i]);
  }
}

Output is:

foo[0] = 0
foo[1] = 0
foo[2] = 0
foo[3] = 0
foo[4] = 0
foo[5] = 0
foo[6] = 0
foo[7] = 0
foo[8] = 0
foo[9] = 0
bar[0] = 1
bar[1] = 1
bar[2] = 1
bar[3] = 1
bar[4] = 1
bar[5] = 1
bar[6] = 1
bar[7] = 1
bar[8] = 1
bar[9] = 1

Now, overflowing vector foo:

#include <vector>
#include <stdio.h>

int main(int argc, char* argv[]) {
  std::vector<int> foo(10, 0);
  std::vector<int> bar(10, 1);

  for(int i = 0; i < 10; i++) {
    printf("foo[%d] = %d\n", i, foo[i]);
  }
  for(int i = 0; i < 10; i++) {
    printf("bar[%d] = %d\n", i, bar[i]);
  }

  for(int i = 0; i < 20; i++) {
    foo[i] = 42;
  }

  for(int i = 0; i < 10; i++) {
    printf("foo[%d] = %d\n", i, foo[i]);
  }
  for(int i = 0; i < 10; i++) {
    printf("bar[%d] = %d\n", i, bar[i]);
  }
}

Output is:

aja042@marzipan:~/scratch$ ./vec
foo[0] = 0
foo[1] = 0
foo[2] = 0
foo[3] = 0
foo[4] = 0
foo[5] = 0
foo[6] = 0
foo[7] = 0
foo[8] = 0
foo[9] = 0
bar[0] = 1
bar[1] = 1
bar[2] = 1
bar[3] = 1
bar[4] = 1
bar[5] = 1
bar[6] = 1
bar[7] = 1
bar[8] = 1
bar[9] = 1
foo[0] = 42
foo[1] = 42
foo[2] = 42
foo[3] = 42
foo[4] = 42
foo[5] = 42
foo[6] = 42
foo[7] = 42
foo[8] = 42
foo[9] = 42
bar[0] = 42
bar[1] = 42
bar[2] = 42
bar[3] = 42
bar[4] = 42
bar[5] = 42
bar[6] = 42
bar[7] = 42
bar[8] = 1
bar[9] = 1
*** Error in `./vec': munmap_chunk(): invalid pointer: 0x00000000014d5c50 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fa2a01d17e5]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x1a8)[0x7fa2a01de698]
./vec[0x400f60]
./vec[0x400ec3]
./vec[0x400dcc]
./vec[0x400c7e]
./vec[0x400b9e]
./vec[0x400a11]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fa2a017a830]
./vec[0x400789]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:01 6015648                            /home/aja042/scratch/vec
00601000-00602000 r--p 00001000 08:01 6015648                            /home/aja042/scratch/vec
00602000-00603000 rw-p 00002000 08:01 6015648                            /home/aja042/scratch/vec
014c4000-014f6000 rw-p 00000000 00:00 0                                  [heap]
7fa29fe51000-7fa29ff59000 r-xp 00000000 08:01 5409469                    /lib/x86_64-linux-gnu/libm-2.23.so
7fa29ff59000-7fa2a0158000 ---p 00108000 08:01 5409469                    /lib/x86_64-linux-gnu/libm-2.23.so
7fa2a0158000-7fa2a0159000 r--p 00107000 08:01 5409469                    /lib/x86_64-linux-gnu/libm-2.23.so
7fa2a0159000-7fa2a015a000 rw-p 00108000 08:01 5409469                    /lib/x86_64-linux-gnu/libm-2.23.so
7fa2a015a000-7fa2a031a000 r-xp 00000000 08:01 5409474                    /lib/x86_64-linux-gnu/libc-2.23.so
7fa2a031a000-7fa2a051a000 ---p 001c0000 08:01 5409474                    /lib/x86_64-linux-gnu/libc-2.23.so
7fa2a051a000-7fa2a051e000 r--p 001c0000 08:01 5409474                    /lib/x86_64-linux-gnu/libc-2.23.so
7fa2a051e000-7fa2a0520000 rw-p 001c4000 08:01 5409474                    /lib/x86_64-linux-gnu/libc-2.23.so
7fa2a0520000-7fa2a0524000 rw-p 00000000 00:00 0 
7fa2a0524000-7fa2a053a000 r-xp 00000000 08:01 5378569                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa2a053a000-7fa2a0739000 ---p 00016000 08:01 5378569                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa2a0739000-7fa2a073a000 rw-p 00015000 08:01 5378569                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa2a073a000-7fa2a08ac000 r-xp 00000000 08:01 8913084                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fa2a08ac000-7fa2a0aac000 ---p 00172000 08:01 8913084                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fa2a0aac000-7fa2a0ab6000 r--p 00172000 08:01 8913084                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fa2a0ab6000-7fa2a0ab8000 rw-p 0017c000 08:01 8913084                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fa2a0ab8000-7fa2a0abc000 rw-p 00000000 00:00 0 
7fa2a0abc000-7fa2a0ae2000 r-xp 00000000 08:01 5409472                    /lib/x86_64-linux-gnu/ld-2.23.so
7fa2a0cb4000-7fa2a0cba000 rw-p 00000000 00:00 0 
7fa2a0ce0000-7fa2a0ce1000 rw-p 00000000 00:00 0 
7fa2a0ce1000-7fa2a0ce2000 r--p 00025000 08:01 5409472                    /lib/x86_64-linux-gnu/ld-2.23.so
7fa2a0ce2000-7fa2a0ce3000 rw-p 00026000 08:01 5409472                    /lib/x86_64-linux-gnu/ld-2.23.so
7fa2a0ce3000-7fa2a0ce4000 rw-p 00000000 00:00 0 
7ffdd10ce000-7ffdd10f0000 rw-p 00000000 00:00 0                          [stack]
7ffdd11d2000-7ffdd11d5000 r--p 00000000 00:00 0                          [vvar]
7ffdd11d5000-7ffdd11d7000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment