C++ Logo

std-discussion

Advanced search

Re: Segmentation Fault with stack top access.

From: Keenan Horrigan <friedkeenan_at_[hidden]>
Date: Mon, 11 Oct 2021 00:47:06 +0000
This is expected as std::stack::pop calls the underlying container's pop_back method, and performing pop_back on an empty vector is undefined behavior.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Sunday, October 10th, 2021 at 7:37 PM, Anubhav Guleria via Std-Discussion <std-discussion_at_[hidden]> wrote:

> Hi,
>
> The following piece of code is giving Segmentation Fault.
>
> Probably the size is getting overflowed and the condition is not
>
> handled in implementation.
>
> Is this a known issue?
>
> #include<iostream>
>
> #include<stack>
>
> #include<vector>
>
> using namespace std;
>
> int main(){
>
> stack<int, vector<int>> s;
>
> s.pop();
>
> cout<<s.size()<<endl;
>
> if(!s.empty()){
>
> cout<<s.top()<<endl;
>
> }
>
> return 0;
>
> }
>
> I compiled with: g++ -std=c++17 test.cpp
>
> Output I got:
>
> 18446744073709551615
>
> Segmentation fault (core dumped)
>
> Output with -fsanitize=address flag
>
> g++ -std=c++17 -fsanitize=address test.cpp
>
> anubhav_at_turing:~/Documents/Programming$ ./a.out
>
> 18446744073709551615
>
> AddressSanitizer:DEADLYSIGNAL
> ===============================================================================================================================================================================================================================================================================================================================================================================================
>
> ==6143==ERROR: AddressSanitizer: SEGV on unknown address
>
> 0x000000000000 (pc 0x559da7fa34e2 bp 0x7ffdefc01bf0 sp 0x7ffdefc01b40
>
> T0)
>
> ==6143==The signal is caused by a READ memory access.
>
> ==6143==Hint: address points to the zero page.
>
> #0 0x559da7fa34e1 in main (/home/anubhav/Documents/Programming/a.out+0x14e1)
>
> #1 0x7ff37989b0b2 in __libc_start_main
>
> (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
>
> #2 0x559da7fa330d in _start
>
> (/home/anubhav/Documents/Programming/a.out+0x130d)
>
> AddressSanitizer can not provide additional info.
>
> SUMMARY: AddressSanitizer: SEGV
>
> (/home/anubhav/Documents/Programming/a.out+0x14e1) in main
>
> ==6143==ABORTING
>
> g++ Version:
>
> g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
>
> Copyright (C) 2019 Free Software Foundation, Inc.
>
> This is free software; see the source for copying conditions. There is NO
>
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> Operating System:
>
> lsb_release -a
>
> No LSB modules are available.
>
> Distributor ID: Ubuntu
>
> Description: Ubuntu 20.04.3 LTS
>
> Release: 20.04
>
> Codename: focal
>
> Kernel: Linux 5.11.0-37-generic
>
> Thanks,
>
> Anubhav
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Std-Discussion mailing list
>
> Std-Discussion_at_[hidden]
>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2021-10-10 19:47:11