C++ Logo

std-discussion

Advanced search

Segmentation Fault with stack top access.

From: Anubhav Guleria <anubhav.nitsri.it_at_[hidden]>
Date: Mon, 11 Oct 2021 05:07:34 +0530
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

Received on 2021-10-10 18:37:49