Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

c - Can I accidentally harm system files or personal files with Visual Studio 2012

I do apologize for the lack of knowledge in this matter but it is very important for me, so at least I'll give it a try. I am using Visual Studio 2012 express on Windows 7 ultimate, I have written some very basic a silly program to practice structures in the C language. Unfortunately, one of the methods that I've written had a wrong condition to stop and it goes to an infinite loop and the worse part it exceeds the given array boundaries. I will add the code below BUT THE CODE IS NOT my main concern tho,I managed to fix it already, I only wish to know if I could harm my system or my personal files (videos, photos, office files etc.) with the given method because when I've tried to run this code, a scary thing happened, my pc started to "Beep" endlessly every 0.5 second and I could't do nothing to stop that, the program was on infinite loop and it I managed to stop this "fun" only with Ctrl + Alt + Del. I had written codes that crashed many times before, but I'ts sure the first time my pc strated to sound beeps. I hope an expert can help me out on this one. Many thanks.

The method with the infinite loop that cause all the problem is "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:
Name:  [%s]
" , hero.name);

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

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

    printf("Birthday: [%d/%d/%d]

" , hero.birthday.day, hero.birthday.month, hero.birthday.year );

}


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

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

void printArrHero (sp_s arr[]){

    int j=0;

    while ( arr[j] != NULL ){

        printf("[#%d]>>>>>>>>>>>>>>>>>>
" , 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!
"); 
        printf("To add a hero press 1
");
        printf("To find you're strongest hero press 2
");
        printf("To find you're fastest hero press 3
");
        printf("To see all of you're heroes list 4
");
        printf("To exit press 0
");
        scanf("%d" , &i);

        switch (i){
        case 0:
            break;

        case 1:
            printf("Please enter the #%d hero characteristics:
Name: " , 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

if I could harm my personal files (videos, photos, office files etc.)

Windows programs run in the context of the current user, and can perform any action of the current user. This means it could edit and delete your personal files.

Generally for a program to go rogue, it would need to have the activity within it. A program which deletes files, can more easily start accidentally deleting files. A program which edits files can make dodgy edits.

This is not a pre-requisite, as once it stops executing outside of your design, anything could happen.

if I could harm my system?

For your system to be harmed, you would need to be running as administrator. That gives the program the ability to do any action which an adminstator could do, which could include serious damage to your machine.

There is one further stage, which is system, which has complete control on the machine, more than an administator.

Is it likely?

Whilst it is possible, it is not very likely. The likelihood for modern programs is they crash before they corrupt files other than they are currently opening, however there are things which you can do to make the damage less-likely or more limited.

  • If you have significant files for your user, then consider switching to a different account for development. This would ring-fence the damage an errant program would cause.

  • Ensure you don't run with User-access-protection turned off. This limits your programs ability to administrate your machine.

  • Consider an alternative platform, the raspberry-pi, or Intel compute stick offer a cheap platform which can be re-built if things go wrong.

What happens in this case?

Assuming it gets stuck in a loop, without properly reading data (probably scanf() returning 0).

The array gets filled up with all 100 elements, and then starts corrupting the stack.

There are 2 plausible memory layouts (windows x86/x86-64).

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

and

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

In the first case, the array gets over-run, and the return address is modified. There is code built into normal compiles (security cookie), which would detect this, and the result is a crash before bad stuff happens.

In the second case, the memory for variable k gets overwritten with one of the array writes. This then sends k to some kooky place, and random memory is overwritten. This could corrupt the program data, but most likely the first attempt at non-natural k (outside of 0-99) would result in a crash.

The likely failure was that you printed out a control code such as a beep - that causes strange behavior, and is consistent with what you saw - I don't think anything bad happened for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...