简体   繁体   English

为什么我会检测到堆栈粉碎?

[英]Why am I getting stack smashing detected?

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char currency[80];
    double exchangerate;
} exchangeT;

void main()
{
    char from[10];
    int i;

    printf("convert from: ");
    scanf("%s", &from[10]); //this seems to be where the problem is
    //printf("into: ");
    //scanf("%s", to);
    //printf("How many of type %s", to);

    FILE *file = fopen("/home/jeffwang/Desktop/exchange.dat", "r");
    exchangeT exchange[5];


    for(i = 0; i < 5; i++)
    {
        fscanf(file, "%s %lf", &exchange[i].currency, &exchange[i].exchangerate);
        printf("%s %lf\n", exchange[i].currency, exchange[i].exchangerate);

        //if(strcmp (from[8], exchange[0].currency) == 0)
        //    printf("lel\n");

    }
}

this is the actual error message 这是实际的错误消息

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

======= Backtrace: =========

/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x45)[0xb7700eb5]

/lib/i386-linux-gnu/libc.so.6(+0x104e6a)[0xb7700e6a]
./a.out[0x8048622]

/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb76154d3]
./a.out[0x8048471]

======= Memory map: ========

08048000-08049000 r-xp 00000000 08:01 667062     /home/jeffwang/Desktop/ECS 30/a.out

08049000-0804a000 r--p 00000000 08:01 667062     /home/jeffwang/Desktop/ECS 30/a.out

0804a000-0804b000 rw-p 00001000 08:01 667062     /home/jeffwang/Desktop/ECS 30/a.out

09d8e000-09daf000 rw-p 00000000 00:00 0          [heap]

b75cd000-b75e9000 r-xp 00000000 08:01 918526     /lib/i386-linux-gnu/libgcc_s.so.1

b75e9000-b75ea000 r--p 0001b000 08:01 918526     /lib/i386-linux-gnu/libgcc_s.so.1

b75ea000-b75eb000 rw-p 0001c000 08:01 918526     /lib/i386-linux-gnu/libgcc_s.so.1

b75fb000-b75fc000 rw-p 00000000 00:00 0 

b75fc000-b77a0000 r-xp 00000000 08:01 918505     /lib/i386-linux-gnu/libc-2.15.so

b77a0000-b77a2000 r--p 001a4000 08:01 918505     /lib/i386-linux-gnu/libc-2.15.so

b77a2000-b77a3000 rw-p 001a6000 08:01 918505     /lib/i386-linux-gnu/libc-2.15.so

b77a3000-b77a6000 rw-p 00000000 00:00 0 

b77b2000-b77b8000 rw-p 00000000 00:00 0 

b77b8000-b77b9000 r-xp 00000000 00:00 0          [vdso]

b77b9000-b77d9000 r-xp 00000000 08:01 918485     /lib/i386-linux-gnu/ld-2.15.so

b77d9000-b77da000 r--p 0001f000 08:01 918485     /lib/i386-linux-gnu/ld-2.15.so

b77da000-b77db000 rw-p 00020000 08:01 918485     /lib/i386-linux-gnu/ld-2.15.so

bfd29000-bfd4a000 rw-p 00000000 00:00 0          [stack]

Aborted (core dumped)

What I don't understand is: I'm using user input as from[10] and I never exceed 10. Also, if I remove the pointer & in scanf, the error message does not come up. 我不明白的是:我正在使用[10]中的用户输入而且我从不超过10.此外,如果我删除指针&scanf,则不会出现错误消息。 Also, when I change from[10] to something smaller say from[2], the error does not occur either! 此外,当我从[10]更改为较小的[2]时,错误也不会发生! Wut?! 武汉理工大学?

Yes, that's definitely wrong. 是的,这绝对是错的。

char from[10];
scanf("%s", &from[10]);

The expression &from[10] is the address of the end of the array. 表达式&from[10]是数组末尾的地址。 Not the last element, but the element "past" the last element, a non-existent element. 不是最后一个元素,而是元素“过去”最后一个元素,一个不存在的元素。 Use this instead: 请改用:

scanf("%s", from); // Still wrong

Note that this is also bad, because you could get more than 10 characters written to from . 请注意,这也是不好的,因为你可以得到写入到超过10个字符from

scanf("%10s", from); // Correct

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM