C++ Logo

std-discussion

Advanced search

Re: Segmentation Fault with stack top access.

From: Anubhav Guleria <anubhav.nitsri.it_at_[hidden]>
Date: Mon, 11 Oct 2021 12:23:43 +0530
Thanks for clarifying.

Any specific reason why container's pop_back method can't check for
current size and if it is 0 then make the pop operation a no-op?

Thanks,
Anubhav

On Mon, Oct 11, 2021 at 6:17 AM Keenan Horrigan via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> 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
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2021-10-11 01:53:57