segmentation fault from sprintf function

This is a discussion on segmentation fault from sprintf function within the C Programming forums, part of the General Programming Boards category; sorry for my bad english. I need some help to find an error in my c program, hope someone can ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    segmentation fault from sprintf function

    sorry for my bad english.
    I need some help to find an error in my c program, hope someone can tell me what's wrong with my program. I try to make a program to shutdown my linux, but when I using sprintf to combine the command, it's going wrong.
    Code:
    #include <stdlib.h>#include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    
    
    
    
    
    
    void processval(int sel, char *pass){
    	char *comm;
    	switch(sel){
    		case 1:
    			sprintf(comm,"echo %s | sudo -S -b poweroff",pass);
    			system(*comm);
    		break;
    		case 2:
    			sprintf(comm,"echo %s | sudo -S -b reboot",pass);
    			system(*comm);
    		break;
    		case 3:
    			sprintf(comm,"echo %s | sudo -S -b killall icewm",pass);
    			system(*comm);
    		break;
    		case 4:
    			system("xscreensaver-command -lock");
    		break;
    	}
    }
    
    
    int main(){
    	int i,sel;
    	i = 0;
    	char yrn,c;
    	system("echo Hello");
    	char *pass = getpass("Password:");
    	start:
    	sel = 0;
    	printf("Process to:\n");
    	printf("1.Shutdown\n");
    	printf("2.Reboot\n");
    	printf("3.logout\n");
    	printf("4.lockscreen\n");
    	printf("5.exit\n\n");
    	printf("Choose by number:");
    	scanf("%d",&sel);
    	printf("\n Are you sure?[y/n]");
    	choose:
    	scanf("%c",&yrn);
    	switch(yrn){
    		case 'y':
    			processval(sel,pass);
    		break;
    		case 'n':
    			goto start;
    		break;
    		default:goto choose;
    	}
    	ends:
    	return 0;
    }
    Please help me, to fix the problems. Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    A pointer is not an array. So your processval() function is tromping a random area of memory on every sprintf() call.

    Change the definition of comm to
    Code:
        char comm[100];
    (100 or any value that exceeds the length of what sprintf() will write).

    Also, when calling system(), your usage would be "system(comm)" not "system(*comm)". The compiler should have complained about that.


    Based on your clear misunderstand of pointers and arrays, I would guess that your getpass() is also doing something invalid.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    It's works. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault on recursive function
    By xbfish in forum C Programming
    Replies: 12
    Last Post: 10-12-2011, 11:44 AM
  2. Recursive function and segmentation fault
    By davidjg in forum C Programming
    Replies: 4
    Last Post: 02-11-2011, 10:23 AM
  3. Segmentation fault: vector to function
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2008, 05:36 AM
  4. Replies: 2
    Last Post: 10-29-2005, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21