LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2006, 08:10 PM   #1
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 48
"C" program creates Seg fault


does anyone see a problem in my code here, it compiles without error, but when I run it........it Seg faults, thx
Code:
#include <stdio.h>

int main(int argc, char *argv[1]){
	char password[5] = "linux";
	char *pass=password;
    
    *strcpy(pass,argv[1]);
    	
     while(*pass){
		*pass=((*pass-'Z')*'A')%26+'Z';
		pass++;
	}
	printf("%s\n",password);
	return 0;
}
 
Old 06-23-2006, 08:14 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by 320mb
Code:
	char password[5] = "linux";
you need at least 6 (for the 5 letters and the null terminator)

also, password must have enough space to store the string copied from argv[1]

Last edited by spooon; 06-23-2006 at 08:18 PM.
 
Old 06-23-2006, 08:16 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by 320mb
Code:
char password[5] = "linux";
Here's your problem. The C string literal "linux" actually has six characters in it: the five you can read plus the null character \0 (value of zero) which indicates the end of all C strings.

This causes your string literal to go outside the bounds of the array you're assigning it into (six bytes for five slots).

Edit: Sorry; looks like I'm a slower typist than Spooon.

Last edited by taylor_venable; 06-23-2006 at 08:17 PM.
 
Old 06-24-2006, 10:49 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
if you use g++ to compile it will catch an error such as the one that was mentioned.

i wonder if that is causing the segfault though. depending on what you are passing in for argv[1], that could also be a problem.
 
Old 06-24-2006, 02:31 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Hmm, yes; looking closer, I see what might be a few more problems. Normally, the main method's signature is expressed as
Code:
int main(int argc, char** argv)

or

int main(int argc, char* argv[])
I'm also unsure about the meaning of the star (*) in front of strcpy(). While I'm certain that writing out of array bounds will (or rather, should) cause a segmentation fault, xhi is also correct. Passing argv[1] as "somethingverylong", or any string more than four characters, would cause the same problem. That's why you should normally use something at least as secure as strncpy() when dealing with possibly untrustworthy input.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strcat is causing seg fault (c program) mfrick Programming 9 08-20-2014 07:34 AM
Getting error: "ALSA device "default" is already in use by another program." brynjarh Debian 7 02-04-2005 11:45 AM
Slack10 Install prob "Seg Fault Error" dem0nk1d Linux - Software 2 01-06-2005 03:20 PM
why iam getting problems like "...too many files opened" or "segmentation fault" naren_0101bits Linux - Newbie 2 07-19-2004 12:20 PM
"segmentation fault" when run a simple c program acer_peri Programming 11 05-28-2004 04:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration