2

因此,我正在我的编程 I 课程中编写一个程序,我们被分配创建一个程序,该程序将检查银行的​​路由号码是否有效。每次我在 NetBeans 上运行这个程序时,我都没有问题。但是,当我通过我们的 Ubuntu 学生服务器运行它时,我得到了这个疯狂的错误。我一直在尝试调试这个程序一段时间,但不太清楚这个错误是从哪里来的。我已经尝试更改数组大小,但没有运气,我仍然收到此错误。这是错误的副本:

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)

这是我的源代码的副本:

#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;
}

如果您有任何人熟悉此错误消息,请帮助!太感谢了!

4

2 回答 2

1

ch_code 缓冲区被输入溢出。

于 2016-02-04T02:47:40.673 回答
0

您尝试使用 gcc 选项 -fsanitize-address 进行构建,它会在您超出堆栈写入时崩溃。

于 2019-10-21T15:23:53.153 回答