Rookie braucht hilfe



  • [code]Datenbank, compiliert wird mit gcc, system linux.
    Problem in der input funktion, segementation fehler.
    
    #include <stdio.h>
    #include <stdio_ext.h>
    #include <string.h>
    int iID,i;
    
    char strName[31];
    char strVorname[21];
    char strEmail[151];
    char astrKomplett[100][202];
    char cCorrect;
    char cInput;
    
    int main ()
    {
        for(i=0;i<100;astrKomplett[i++][0]='\0')
    		{}
    	printf("\nHere you are in our person register!\n");
    	fctmenue();
    }
    
    void fctInput()
    
        {
        	  int j,a=0,d;      // ID wird erzeugt
        	  do 
        	  { d=a;
       	  		for(iID=0;iID<100;iID++) //suche nach nächst freien ID[code]
       	  			if(iID==a)
       	  			{
       	  			a++;
       	  			}
            }while(!(d==a));
    
            printf("\nNow you can type in our own record!\n\n");
    
            printf("\nType in your first name:");
            scanf("%s", &strVorname); __fpurge(stdin); 						//why 0? 
    
            printf("\nType in your last name:");
            scanf("%s", &strName); __fpurge(stdin);        
    
            printf("\nType in your E-mail adress:");
     	     scanf("%s", &strEmail); __fpurge(stdin); 
    
     	     do 
     	     {
    	 	     printf("\n Are this records correct?");
    	 	     printf("\n firstname:%s",strVorname);   
    	 	     printf("\n [Y]es | [N]o : ");
    			  cCorrect=getchar(); __fpurge(stdin);
    	 		  if(cCorrect=='n')
    	 		  		{
    	 		  		printf ("\n Firstname:");
    	 	     		scanf("%s", &strVorname); __fpurge(stdin);
    	 	     		}
    	 	     else 
    	 	     		if (cCorrect=='N')
    	 	     		{
    	 		  		printf ("\n Fristname:");
    	 	     		scanf("%s", &strVorname); __fpurge(stdin);
    	 	     		}
    	 	     		else
    	 	     		break;
     	     }while(cCorrect=='n'); 	 	
    
     	     do									
    	 	     {
    	 	     printf("\n Are this records correct?");
    	 	     printf("\n name:%s",strName);
    	 	     printf("\n [Y]es | [N]o :");
    			  scanf("%c",&cCorrect);
    			  printf("%c",cCorrect);
    			  getchar();
    	 		  if (cCorrect=='n')
    	 		  		{
    	 		  		printf ("\n name:");  
    	 	     		scanf("%s",&strName); __fpurge(stdin);
    	 	     		}
    	 	     else 
    	 	     		if (cCorrect=='N')
    	 	     		{
    	 		  		printf ("\n name:");
    	 	     		scanf("%s",&strName); __fpurge(stdin);
    	 	     		}
    	 	     		else
    	 	     		break;
     	     }while((cCorrect=='n')||(cCorrect=='N'));
    
     	     //do
    	 	    // {
    	 	     	printf("\n Are these records correct?");
    	 	     	printf("\n Email:%s",strEmail);
    	 	     	printf("\n [Y]es | [N]o :");
    			  	scanf("%c",&cCorrect);__fpurge(stdin);
    			  	printf("%c",cCorrect);
    			  	if ((cCorrect=='n')|| (cCorrect=='N'))
    	 		  		{
    	 		  		printf ("\n Mail:");
    	 	     		scanf("%s",&strEmail);
    	 	     		__fpurge(stdin); 			
    	 	     		}
    	 	  										//segmentation fault in here 
    
    		 	     	printf(" gh ");		  
    				  // work up to here
    				  int iStelle=0;
    				  astrKomplett[iID][0]=iID/10;   
    				  astrKomplett[iID][1]=iID%10;   
    				  astrKomplett[iID][2]='-'; // nun mit strcpy und strlen string füllen
    				  strcpy(astrKomplett[iID][3],strVorname);
    				  iStelle=strlen(astrKomplett[iID]);
    				  astrKomplett[iID][iStelle+1]=',';
    				  strcpy(astrKomplett[iID][iStelle+2],strName);
    				  iStelle=strlen(astrKomplett[iID]);
    				  astrKomplett[iID][iStelle+1]=',';
    				  strcpy(astrKomplett[iID][iStelle+2],strEmail);
    				  fctmenue();
    
     	     //}while((cCorrect=='n')||(cCorrect=='N'));
    
        }
    
    void fctDelete()
    	{	
    	int j;
    		printf("Type in the ID (0-99) of register you wanna delete:");
    		scanf("%i",&iID); __fpurge(stdin);
    		for(i=0; i<100; i++)
          {
          	if(atoi(astrKomplett[i][0])*10 + atoi(astrKomplett[i][1]) == iID)
          		{		  
          		for (j=0;j<=202;j++)
    		  		astrKomplett[i][j]='\0';
    		      printf("deleted");
    		      }
    		 fctmenue();	
    		}}
    void fctDisplay()
    		{	
    		int i=0;
    		printf("Your lists");
    		while (astrKomplett[i][0]!='\0')
    		{
    		printf ("%s\n",astrKomplett[i]);
    		i++;
    
    		}fctmenue();
    		}
    
    void fctmenue()
    { 
    
    	printf("\nWhat do you wanna do?\n\n[1]Take your own record?\n[2]Delete a record?\n[3]Read the list of records?\n[4]Quit the program?\n\n");//cInput
    	printf("\nPlease choose a number:");
    	scanf("%c", &cInput); __fpurge(stdin);
    
    	switch(cInput)
    	{
    		case '1':
    		fctInput();
    		break;
    
    		case '2':
    		fctDelete();
    		break;
    
    		case '3':
    		fctDisplay();
    		break; 
    
    		case '4':
    		printf("\nYour choise is to quit the program!\n\n");
    		break;
    
    		default:
    		break;
    	} 
    
    } 
    	[/code]
    


  • Erstens: Wie lautet die Frage?

    Zweitens: Meinst du wirklich, irgendwer wird sich 200 Zeilen Text durchlesen auf der Suche nach möglichen Schwachstellen? DAS ist deine Aufgabe.



  • Was mir beim kurzen Überfliegen schon aufgefallen ist: scanf("%s", &strVorname); -> falsch. Es muss scanf("%s", strVorname); sein. Benutz gdb und printfs um zu Debuggen.





  • Ein Fehler könnte strcpy sein, da das Zielarray immer weiter verschoben wird. Mit den zahlen kann er nicht umgehen
    Kann das sein? und wenn ja, gibt es einen anderen befehl,mit dem ich das lösen kann?


Anmelden zum Antworten