-1

i'm trying to run a C program. It seems to work fine, but at the end it shows:

STACK SMASHING DETECTED

This is the code:

#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]);

 }

 }

Does anybody know why this is happening? I've done a lot of research in Google but every possible answer i tried didn't work. Any help would be really helpful.

Thank you all!!

  • You have a stack overflow somewhere. You can disable stack cookies in your compiler, which will probably cause a segmentation fault that you can further analyse in your debugger. – Niklas B. Apr 27 '12 at 12:54
  • Did you look here: http://stackoverflow.com/questions/1345670/stack-smashing-detected ? – Nick Apr 27 '12 at 12:55
  • 2
    If you are going to ask people to help you, please make sure your code is formatted nicely, especially if you are going to post so much of it. (the indentation is very broken due to mixing tabs and spaces) – huon Apr 27 '12 at 13:00
  • That's one of the coolest-sounding (albeit frustrating, no doubt) errors I've never experienced. – rownage Apr 27 '12 at 13:05
  • Thank you for all the answers. I'd like to point out that i get that warning but the program runs well, i mean, it does what it should do – Alex Aldridge Asa Apr 27 '12 at 13:10
  • Maybe this is a clue to find the error? Not for me..maybe for an expert like you ;) – Alex Aldridge Asa Apr 27 '12 at 13:15
  • 1
    I suspect I know the error but I can't be bothered to read code this messy. – Lundin Apr 27 '12 at 13:39

2 Answers2

2

You're doing a lot of I/O into very small buffers. If any of those overflow, they're very likely to smash the stack.

You should run your program in Valgrind if possible. You could also try increasing the sizes of the buffers, and switching from fscanf() to something safer (such as reading one full line with fgets() and then parsing it in-memory).

unwind
  • 391,730
  • 64
  • 469
  • 606
0
 read(pipe2[0], hijoPID, 6);

should be

 read(pipe2[0], hijoPID, 4);

This is not the only problem though. You should be using constants for your widths so this doesn't happen.

const int hijoPID_l = 4;
read(pipe2[0], hijoPID, hijoPID_l);

Also when you do your fscanf you should specify your widths.

k=fscanf(fichero, "%7", id);
i=fscanf(fichero, "%10", orden);
j=fscanf(fichero, "%3", arg);

If you use the constants for your lengths then, for example,

char fmt_id[8];
sprintf(fmt_id, "%%%d", id_l);
k=fscanf(fichero, fmt_id, id);

can be used to dynamically create your format strings at the beginning of your loop then reused for each fscanf().

rmoro
  • 393
  • 1
  • 11