Alex Pak
Alex Pak

Reputation: 21

*** stack smashing detected ***: a.out terminated

So I'm working on a program in my Programming I class and we were assigned to create a program that will check to see if a routing number from a bank is valid. Every time I run this program on NetBeans, I have no issue. BUT, when I run it through our Ubuntu student server, I get this crazy error. I've been trying to debug this program for a while now and can't quite see where this error is originating from. I've tried changing the array sizes and with no luck, I'm still getting this error. Here is a copy of the error:

Enter a bank routing number: 
103000648
1 0 3 0 0 0 6 4 8 
7 3 9 7 3 9 7 3 
7 0 27 0 0 0 42 12 
7 7 34 34 34 34 76 88 
88
8
the check code is valid
*** stack smashing detected ***: a.out terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7f28e6be8e57]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x0)[0x7f28e6be8e20]
a.out[0x400f99]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f28e6b0076d]
a.out[0x400b39]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:11 40895235                           /home/wm/wm130/a.out
00601000-00602000 r--p 00001000 08:11 40895235                           /home/wm/wm130/a.out
00602000-00603000 rw-p 00002000 08:11 40895235                           /home/wm/wm130/a.out
01ffd000-0202f000 rw-p 00000000 00:00 0                                  [heap]
7f28e67e3000-7f28e68de000 r-xp 00000000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e68de000-7f28e6add000 ---p 000fb000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e6add000-7f28e6ade000 r--p 000fa000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e6ade000-7f28e6adf000 rw-p 000fb000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e6adf000-7f28e6c93000 r-xp 00000000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6c93000-7f28e6e92000 ---p 001b4000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6e92000-7f28e6e96000 r--p 001b3000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6e96000-7f28e6e98000 rw-p 001b7000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6e98000-7f28e6e9d000 rw-p 00000000 00:00 0 
7f28e6e9d000-7f28e6eb3000 r-xp 00000000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e6eb3000-7f28e70b2000 ---p 00016000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e70b2000-7f28e70b3000 r--p 00015000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e70b3000-7f28e70b4000 rw-p 00016000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e70b4000-7f28e71b6000 r-xp 00000000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e71b6000-7f28e73b5000 ---p 00102000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e73b5000-7f28e73bd000 r--p 00101000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e73bd000-7f28e73bf000 rw-p 00109000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e73bf000-7f28e73c2000 rw-p 00000000 00:00 0 
7f28e73c2000-7f28e73e4000 r-xp 00000000 08:01 5505877                    /lib/x86_64-linux-gnu/ld-2.15.so
7f28e75c5000-7f28e75ca000 rw-p 00000000 00:00 0 
7f28e75df000-7f28e75e4000 rw-p 00000000 00:00 0 
7f28e75e4000-7f28e75e5000 r--p 00022000 08:01 5505877                    /lib/x86_64-linux-gnu/ld-2.15.so
7f28e75e5000-7f28e75e7000 rw-p 00023000 08:01 5505877                    /lib/x86_64-linux-gnu/ld-2.15.so
7ffffd60a000-7ffffd62b000 rw-p 00000000 00:00 0                          [stack]
7ffffd676000-7ffffd677000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

And here is a copy of my source code:

#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <string.h>

using namespace std;

int main() {

    string code;

    int routing[9];
    int multipliers[8] = {7, 3, 9, 7, 3, 9, 7, 3};
    int subtotals = 0;
    int products[8];
    int sum = 0;

    cout << "Enter a bank routing number: " << endl;
    cin >> code;

    char ch_code[8];
    strcpy(ch_code, code.c_str()); //store character code into array

    for (int i = 0; i < 9; i++) { //convert string to integer and print routing
        routing[i] = ch_code[i] - '0';
        cout << routing[i] << " ";
    } cout << endl;

    for (int i = 0; i < 8; i++) { //print multipliers
        cout << multipliers[i] << " ";
    } cout << endl;

    for (int i = 0; i < 8; i++) { // print and calculate products
        products[i] = routing[i] * multipliers[i];
        cout << products[i] << " ";
    } cout << endl;

    for (int i = 0; i < 8; i++) { // calculate subtotals and print
        subtotals += products[i];
        cout << subtotals << " ";
    } cout << endl;

    cout << subtotals << endl; // print sum
    cout << subtotals % 10 << endl; // check digit

    if(subtotals % 10 == routing[8]) {
        cout << "the check code is valid" << endl;
    }

    else cout << "the check code is invalid" << endl;
}

If any of you are familiar with this error message, please please help! Thank you so much!

Upvotes: 2

Views: 3227

Answers (2)

Alok
Alok

Reputation: 127

you try to build with gcc option -fsanitize-address, it will crash at the point where you are writing beyond stack.

Upvotes: 1

Krum
Krum

Reputation: 498

ch_code buffer is being overrun by the input.

Upvotes: 1

Related Questions