2012-04-27 7 views
-1

私はCプログラムを実行しようとしています。正常に動作するようですが、最後にそれが示しています。なぜこれが起こっているスタックスマッシングが検出されました。 Cプログラミング

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 

extern int errno; 

int main(int argc, char *argv[]){ /*Para pasar argumentos a la funcion main*/ 

char id[7]="\0";  
char orden[10]="\0"; 
char arg[3]="\0"; 
char idhijo[7]="\0";  
char ordenhijo[10]="\0"; 
char arghijo[3]="\0"; 

int i=0, j=0, k=0; 

int a=1, pipe1[2], pipe2[2], control, argument=0, status, posx=50, posy=50, s, t, arg_val=0, orden_val=0; 

FILE *fichero; 

char mensaje1[4], mensaje2[4], mensaje3[6]; 
char posxpadre[4]="50", posypadre[4]="50", hijoPID[4]; 

system("clear"); 

control = pipe(pipe1); 
if(control !=0){ 

perror("pipe1"); 
exit(errno); 
} 

control = pipe(pipe2); 
if(control !=0){ 

perror("pipe2"); 
exit(errno); 
} 

control = fork();        

fichero=fopen(argv[1], "r"); 
if (fichero){ 

while((i != EOF) && (j != EOF) && (k != EOF)){ 


if((i != EOF) && (j != EOF) && (k != EOF)){ 

    if (control ==-1){ 

    perror("fork"); 
    exit(errno); 

    } 


    if (control !=0){ 


     k=fscanf(fichero, "%s", id); 
     i=fscanf(fichero, "%s", orden); 
     j=fscanf(fichero, "%s", arg); 


     if(Robot_valido(id)){ 

      write(pipe1[1], id, 7); 
       write(pipe1[1], arg, 3); 
       write(pipe1[1], posxpadre, 4);    
       write(pipe1[1], posypadre, 4); 
       write(pipe1[1], orden, 10); 

      read(pipe2[0], posxpadre, 4); 
      read(pipe2[0], hijoPID, 6); 
      read(pipe2[0], posypadre, 4);    

      printf("Hijo con PID %s desplazo el robot a la posicion: (%s, %s)\n\n", hijoPID, posxpadre, posypadre); 

      } 
} 

else{ 

    read(pipe1[0], idhijo, 7); 
    read(pipe1[0], arghijo, 3); 
    read(pipe1[0], posxpadre, 4); 
    read(pipe1[0], posypadre, 4); 
    read(pipe1[0], ordenhijo, 10); 

    if (Robot_valido(idhijo)!=0){ 

     Orden_valida(ordenhijo, arghijo, &orden_val, &arg_val); 

     if (orden_val !=0 && arg_val !=0){ 

      argument=atoi(arghijo); 
      posy=atoi(posypadre); 
      posx=atoi(posxpadre); 

     if (strcmp(ordenhijo, "arriba")==0){ 

      if (posy>1){ 

       while (argument > 0){ 

        if (posy>0){ 

        posy--; 
        argument--; 
         } 

        else{ 

        argument=0; 
        } 
       } 
        } 
      } 

     if (strcmp(ordenhijo, "abajo")==0){ 

        if (posy<100){ 

       while (argument > 0){ 

        if (posy<100){ 
        posy++; 
        argument--; 

        } 

        else{ 

        argument=0; 
        } 
       } 
        } 
      } 

     if (strcmp(ordenhijo, "derecha")==0){ 

        if (posx<100){ 

        while (argument > 0){ 

        if (posx<100){ 
         posx++; 
         argument--; 

         } 

        else{ 
         argument=0; 
        } 
       } 
       } 
      } 

     if (strcmp(ordenhijo, "izquierda")==0){ 

       if (posx>0){ 

        while (argument > 0){ 

        if (posx>0){ 
         posx--; 
         argument--; 
         } 

        else{ 

         argument=0; 
        } 
         } 
       } 
      } 



     printf("Robot desplazado hasta la posicion: (%d, %d)\n", posx, posy); 



     sprintf(mensaje1, "%d", posx); 
     sprintf(mensaje2, "%d", posy); 
     sprintf(mensaje3, "%d", getpid());  

     write(pipe2[1], mensaje1, 4); 
      write(pipe2[1], mensaje3, 6); 
     write(pipe2[1], mensaje2, 4); 

     } 

    } 

    } 
    } 
} 

close(pipe1[0]); 
close(pipe1[1]); 
close(pipe2[0]); 
close(pipe2[1]); 

} 

} 

誰もが知っています:

STACK SMASHINGこれはコードです

が検出さ?私はGoogleでたくさんの研究をしましたが、試したすべての可能な回答はうまくいきませんでした。どんな助けでも本当に役に立ちます。

ありがとうございました!

+0

スタックオーバーフローが発生しています。コンパイラでスタッククッキーを無効にすると、デバッガでさらに解析できるセグメンテーションフォルトが発生する可能性があります。 –

+0

あなたはここを見ましたか?http://stackoverflow.com/questions/1345670/stack-smashing-detected? – Nick

+2

あなたに助けを求める人に尋ねるなら、特にあなたがあまりポストするつもりなら、あなたのコードがきれいにフォーマットされていることを確認してください。 (タブとスペースが混ざっているために窪みが非常に壊れています) – huon

答えて

2

非常に小さなバッファに多くのI/Oを実行しています。それらのどれかがオーバーフローした場合、スタックを壊す可能性が非常に高いです。

可能であれば、Valgrindでプログラムを実行する必要があります。また、バッファのサイズを増やして、fscanf()からより安全なもの(fgets()で1行を読み取り、メモリ内で解析するなど)に切り替えることもできます。

関連する問題