473,408 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

segfault when strcmp

Hi,

I've got this problem with this project i am working on, i am
a bit new to c so i don't know why this happens.

string is read from a telnet socket.

Here's the piece of code that segfaults:

struct login_usertable{
char username;
char password;
int status; /* 0=disconnected, 1=need password, 2=connected */
};

struct login_usertable user;

int login_fillusers(){
user.username="robert";
user.password="test";
user.status=0;
}

int login_requesthandler(int socket,char *string){
if(user.status==0) login_showlogin(socket, 0);
if(user.status==1){
if(strcmp((char *)user.username,string)==0){ //segfaults
printf("logged in\n");
login_showlogin(socket, 1);
}else{
printf("err\n");
login_showlogin(socket, 0);
}
}
}

What's wrong with it?

Thanks in Advance,

Robert

--
ln[dot]tenlap[at]snem_trebor
read reversed and fill dot&at for email
Nov 13 '05 #1
6 3194
Robert Mens <ro*********@hotmail.com> scribbled the following:
Hi, I've got this problem with this project i am working on, i am
a bit new to c so i don't know why this happens. string is read from a telnet socket. Here's the piece of code that segfaults: struct login_usertable{
char username;
Take a close look at the declaration of this field. Note the type.
char password;
int status; /* 0=disconnected, 1=need password, 2=connected */
}; struct login_usertable user; int login_fillusers(){
user.username="robert";
You are shoving a char * into a char. Undefined behaviour.
user.password="test";
user.status=0;
} int login_requesthandler(int socket,char *string){
if(user.status==0) login_showlogin(socket, 0);
if(user.status==1){
if(strcmp((char *)user.username,string)==0){ //segfaults
The undefined behaviour in the first line of login_fillusers()
already destroyed all hope of user.username being a valid char *
value. No amount of casting will magically change it back. Thus
this strcmp() call is pointless.
printf("logged in\n");
login_showlogin(socket, 1);
}else{
printf("err\n");
login_showlogin(socket, 0);
}
}
} What's wrong with it?


Try changing the char fields in struct login_usertable to char * and
see if it works.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
Nov 13 '05 #2
In article <bn**********@reader11.wxs.nl>, Robert Mens wrote:
Hi,

I've got this problem with this project i am working on, i am
a bit new to c so i don't know why this happens.

string is read from a telnet socket.

Here's the piece of code that segfaults:

struct login_usertable{
char username;
char password;
int status; /* 0=disconnected, 1=need password, 2=connected */
};

struct login_usertable user;

int login_fillusers(){
user.username="robert";
user.password="test";

Both user.username and user.password are characters, not
strings. Therefore you can't make those two assignments.

Try this instead:

#define MAXLENGTH 64

struct login_usertable {
char username[MAXLENGTH];
char password[MAXLENGTH];
int status;
}

int login_fillusers()
{
strncpy(user.username, "robert", MAXLENGTH);
strncpy(user.password, "piggsvin", MAXLENGTH);
/* etc. */
Read up on strings in some book.
--
Andreas Kähäri
Nov 13 '05 #3
On Wed, 22 Oct 2003 22:04:27 +0200
Robert Mens <ro*********@hotmail.com> wrote:
Hi,

I've got this problem with this project i am working on, i am
a bit new to c so i don't know why this happens.

string is read from a telnet socket.

Here's the piece of code that segfaults:
#include <string.h> /* for string functions */
#include <stdio.h> /* for printf and friends */
struct login_usertable{
char username;
char password;
char *username;
char *password;

/*
This was your big problem. You want pointer to strings if you are going
to make them point to something, which you do below. A single char as
you declared them can only store a single character.

Alternatively you might want character arrays, depending on what the
rest of your program is. However, you would then have to change your
login_fillusers function.
*/
int status; /* 0=disconnected, 1=need password, 2=connected */
};

struct login_usertable user;

int login_fillusers(){ void login_fillusers(){

/* You don't return a value, so tell the compiler the truth */
user.username="robert";
user.password="test";
user.status=0;
}

int login_requesthandler(int socket,char *string){ void login_requesthandler(int socket,char *string){

/* Again, you don't return an int so don't tell the compiler you will */
if(user.status==0) login_showlogin(socket, 0);
if(user.status==1){
if(strcmp((char *)user.username,string)==0){
if(strcmp(user.username,string)==0){
/*
I bet you added the cast above to shut up the compiler. That was wrong,
the compiler was warning because YOU had got it wrong.
*/
//segfaults /*
The // style comment was introduced in C99 and may not be supported by
your compiler when invoked in conforming mode (something you should do)
so it is better to use the old style comments like I have been doing.

Old style comments also survive reformatting on posting to Usenet
whereas // style comments often get broken.
*/
printf("logged in\n");
login_showlogin(socket, 1);
}else{
printf("err\n");
login_showlogin(socket, 0);
}
}
}

What's wrong with it?


See above.

For the future you should find out how to invoke your compiler in ISO
compliant mode with a a high level of warnings. I got masses of warnings
when I did this. Also don't ignore warnings or silence them with casts
since at your level I don't think you know enough to understand which
warnings you can ignore.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #4
Robert Mens wrote:
Hi,

I've got this problem with this project i am working on, i am
a bit new to c so i don't know why this happens.

string is read from a telnet socket.

Here's the piece of code that segfaults:

struct login_usertable{
char username;
char password;
int status; /* 0=disconnected, 1=need password, 2=connected */
};

struct login_usertable user;

int login_fillusers(){
user.username="robert";
user.password="test";
user.status=0;
}

int login_requesthandler(int socket,char *string){
if(user.status==0) login_showlogin(socket, 0);
if(user.status==1){
if(strcmp((char *)user.username,string)==0){ //segfaults
printf("logged in\n");
login_showlogin(socket, 1);
}else{
printf("err\n");
login_showlogin(socket, 0);
}
}
}

What's wrong with it?

Thanks in Advance,

Robert


Thanks a lot for your answers
solved it now by places the char * thingys
--
ln[dot]tenalp[at]snem_trebor
read reversed and fill dot&at for email
Nov 13 '05 #5
On Wed, 22 Oct 2003 20:39:45 +0000 (UTC), Andreas Kahari
<ak*******@freeshell.org> wrote:
strncpy(user.username, "robert", MAXLENGTH);
strncpy(user.password, "piggsvin", MAXLENGTH);
/* etc. */


I wouldn't recommend strncpy here - it doesn't terminate the string if it's
too long to fit. Use strncat instead, after initialising the first element
of the destination to zero.

-- Mat.

Nov 13 '05 #6
Mathew Hendry <md****@blueyonder.co.uk> wrote in message news:<ro********************************@4ax.com>. ..
On Wed, 22 Oct 2003 20:39:45 +0000 (UTC), Andreas Kahari
<ak*******@freeshell.org> wrote:
strncpy(user.username, "robert", MAXLENGTH);
strncpy(user.password, "piggsvin", MAXLENGTH);
/* etc. */


I wouldn't recommend strncpy here - it doesn't terminate the string if it's
too long to fit.


It rather depends on the intended usage. Maximum memory utilisation of
fixed width fields is exactly what strncpy was designed for.

Of course, subsequent use of the field as an assumed string (e.g. with
strcmp) is not likely to have a good outcome.

However, fprintf specifically supports such fields too...

printf("%.*s\n", MAXLENGTH, user.username);

....as the argument to %s need only be an array, it needn't be a
string.

--
Peter
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider these two classes. Class Accepter in placement_classes.inc.php works as a form validation object, and it works like a charm: PHP: // placement_classes.inc.php - THIS ONE WORKS! ...
6
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
7
by: James Leddy | last post by:
For some reason, I get a segmentation fault when I exit this program I made. Most of the time when I have seen this it has occured in the middle of the program and the program terminates. However,...
11
by: Ramprasad A Padmanabhan | last post by:
I have got a pretty simple script , that uses bsearch to look for a particular element The problem is , it simply segfaults inside the compare function. I have a similar script that works fine...
162
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. ...
10
by: somebody | last post by:
There are two files below named search.c and search.h. In the for loop in search.c, the for loop never exits, even if mystruct.field1 has no match. Instead of exiting the for loop it keeps going...
47
by: fishpond | last post by:
One way I've seen strcmp(char *s1, char *s2) implemented is: return immediately if s1==s2 (equality of pointers); otherwise do the usual thing of searching through the memory at s1 and s2. Of...
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.