Home > front end >  How do I get an error from `pop_back()` if `size()` is 0?
How do I get an error from `pop_back()` if `size()` is 0?

Time:11-26

I was explaining to a coworker why we have small test with sanitizers on them. He asked about popping a vector too many times and if it was an exception, assert, UB and which sanitizer catches it

It appears NONE catches them. Address and memory will if you call back() after popping too many times but if you pop and do size() you can get negative values

Is there a way I can get an assert or exception or runtime termination when I pop too many times? I really thought a debug build without sanitizers would have caught that (with an assert or exception)

I use clang sanitizer but build options with gcc will also be helpful

CodePudding user response:

Both libstdc and libc have a "debug mode" with assertions, that can be enabled using:

  • -D_GLIBCXX_DEBUG for libstdc
  • -D_LIBCPP_DEBUG for libc

Also -fsanitize=undefined appears to catch it, but the error message is much more cryptic.

  • Related