【问题标题】:Can I accidentally harm system files or personal files with Visual Studio 2012使用 Visual Studio 2012 会意外损坏系统文件或个人文件吗
【发布时间】:2016-08-26 13:15:14
【问题描述】:

我很抱歉在这件事上缺乏知识,但这对我来说非常重要,所以至少我会试一试。 我在 Windows 7 Ultimate 上使用 Visual Studio 2012 express, 我编写了一些非常基本的愚蠢程序来练习 C 语言中的结构。不幸的是,我写的其中一种方法有一个错误的停止条件,它进入了一个无限循环,更糟糕的是它超出了给定的数组边界。我将在下面添加代码,但代码不是我主要关心的问题,我已经设法修复它,我只想知道我是否会损坏我的系统或我的个人文件(视频、照片、办公文件等)给定方法,因为当我尝试运行这段代码时,发生了一件可怕的事情,我的电脑开始每 0.5 秒无休止地“哔”一次,我无法阻止它,程序处于无限循环,我管理仅使用 Ctrl + Alt + Del 来停止这种“乐趣”。我之前编写过多次崩溃的代码,但我不确定我的电脑第一次发出哔哔声。我希望专家可以帮助我解决这个问题。非常感谢。

导致所有问题的无限循环方法是“printArrHero”:

#include<stdio.h>  
#include<string.h>


struct Date{

    int day, month, year;

} typedef date_s;

struct Superhero{

    char name[30];

    double power, speed;

    date_s birthday;

} typedef sp_s;


void printInfo(sp_s hero){

    printf("Super hero info:\nName:  [%s]\n" , hero.name);

    printf("Power: [%.2lf]\n" , hero.power);

    printf("Speed: [%.2lf]\n" , hero.speed);

    printf("Birthday: [%d/%d/%d]\n\n" , hero.birthday.day, hero.birthday.month, hero.birthday.year );

}


void addHero (sp_s arr[], int *k, sp_s newHero){

    arr[*k] = newHero;
    (*k)++;
    printf("\nSuccess! a new superhero is added.\n"); 
}

void printArrHero (sp_s arr[]){

    int j=0;

    while ( arr[j] != NULL ){

        printf("[#%d]>>>>>>>>>>>>>>>>>>\n" , j+1); 

        printInfo( arr[j] );

        j++;
    }


}

void main(){

    sp_s myHeros[100];
    sp_s newHero;

    int i = -1;
    int k = 0;

    while (i != 0){ // menu loop
        printf("Welcome to my game!\n"); 
        printf("To add a hero press 1\n");
        printf("To find you're strongest hero press 2\n");
        printf("To find you're fastest hero press 3\n");
        printf("To see all of you're heroes list 4\n");
        printf("To exit press 0\n");
        scanf("%d" , &i);

        switch (i){
        case 0:
            break;

        case 1:
            printf("Please enter the #%d hero characteristics:\nName: " , k+1);
            scanf("%s" , &newHero.name);
            printf("Power: ");
            scanf("%lf" , &newHero.power);
            printf("Speed: ");
            scanf("%lf" , &newHero.speed);
            printf("Birth date [xx/xx/xx](format): ");
            scanf("%d/%d/%d" , &newHero.birthday.day, &newHero.birthday.month, &newHero.birthday.year);

            addHero(myHeros, &k, newHero); // now when I collecten all the info needed to make a new hero lets send it to the func tht puts it in the #k place in the array
            break;

        case 2:

            break;
        case 3:

            break;

        case 4:
            printArrHero(myHeros);
            break;

        } // END of switch-case

    } // while menu loop


} // main

【问题讨论】:

  • 好吧,struct Something { /* something */ } typedef Something; 不正确,不会编译。所以这你的程序有问题
  • 亲爱的“ForceBru”它编译得很好我的朋友,否则它不会运行。

标签: c visual-studio visual-studio-2012 structure infinite-loop


【解决方案1】:

如果我可能会损坏我的个人文件(视频、照片、办公文件等)

Windows 程序在当前用户的上下文中运行,并且可以执行当前用户的任何操作。这意味着它可以编辑和删除您的个人文件。

一般来说,要让程序变得流氓,它需要在其中包含活动。删除文件的程序可以更容易地开始意外删除文件。 编辑文件的程序可以进行狡猾的编辑。

这不是先决条件,因为一旦它停止在您的设计之外执行,任何事情都可能发生。

如果我会损害我的系统?

为了使您的系统受到损害,您需要以管理员身份运行。这使程序能够执行管理员可以执行的任何操作,其中可能包括对您的机器造成严重损坏。

还有一个阶段,即system,它对机器拥有完全的控制权,而不是管理员。

有可能吗?

虽然有可能,但可能性不大。现代程序的可能性是它们在损坏当前打开的文件之前崩溃,但是您可以采取一些措施来降低损坏的可能性或更有限。

  • 如果您的用户有重要文件,请考虑切换到其他帐户进行开发。这将隔离错误程序可能造成的损害。

  • 确保不要在关闭用户访问保护的情况下运行。这会限制您的程序管理您的机器的能力。

  • 考虑一个替代平台,raspberry-pi 或英特尔计算棒提供了一个便宜的平台,如果出现问题可以重新构建。

在这种情况下会发生什么?

假设它卡在一个循环中,没有正确读取数据(可能 scanf() 返回 0)。

数组被所有 100 个元素填满,然后开始破坏堆栈。

有 2 种可能的内存布局(windows x86/x86-64)。

+---------+
| return  |
|   from  |
| main    |
+---------+
|hero[99] |
+---------+
|hero[98] |
+---------+
|hero[97] |
+---------+
| ...     |

+---------+
| return  |
|   from  |
| main    |
+---------+
|  k      |
+---------+
|hero[99] |
+---------+
|hero[98] |
+---------+
|hero[97] |
+---------+
| ...     |

在第一种情况下,数组溢出,返回地址被修改。正常编译(安全cookie)中内置了代码,可以检测到这一点,结果是在坏事发生之前崩溃。

在第二种情况下,变量 k 的内存被其中一个数组写入覆盖。然后这会将 k 发送到某个古怪的地方,并覆盖随机内存。这可能会损坏程序数据,但第一次尝试非自然 k(0-99 之外)很可能会导致崩溃。

可能的失败是您打印了一个控制代码(例如哔声) - 这会导致奇怪的行为,并且与您所看到的一致 - 我认为您没有发生任何不好的事情。

【讨论】:

  • 其实我并没有期待一个丰富而有用的答案,因为你是我的朋友!感谢您花时间为这个精彩的解释!我肯定会切换用户,这是我现在唯一能做的事情。如果我可能会问,对于这个特殊的“noob”代码,我使用的方法只是打印数组单元格,在这种情况下是否仍然有可能损害 pc 上的任何个人数据?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 2012-07-20
  • 2018-06-13
  • 1970-01-01
相关资源
最近更新 更多