Looping forever?
Hey, while trying to iterate with "next" as a pointer in my structure, it loops forever for no reason, even if i try to make a fake pointer to the structure (not with calling a function to set it up)
my code:
void
command_login(conn,msg)
void *conn;
const char *msg;
{
const char *u,*p;
char *home;
ConnectionQueue* q;
DBResult *result;
DBResult *tmp;
bool got_pass,got_user,admin;
if (!conn)
return;
/* initalize */
q = (ConnectionQueue *)conn;
got_pass = false; got_user = false; admin = false;
result = NULL;
tmp = NULL;
if (msg == NULL || *msg == '\0'){
send_socket(q->conn,"LOGIN: Invalid user and password\n");
return;
}
(void)strtok((char *)msg," ");
u = strtok((char *)NULL, " ");
p = strtok((char *)NULL, " ");
if (!u || !p){
send_socket(q->conn,"LOGIN: Invalid user or pa开发者_StackOverflow社区ssword\n");
return;
}
printf("command_login(): u = %s, p = %s\n",u,p);
tmp = store_query("SELECT * FROM `users`");
if (!tmp){
/* should never happen */
send_socket(q->conn,"LOGIN: Failed to fetch results\n");
return;
}
printf("command_login(): Checking user and password...\n");
result = tmp;
while (result != NULL){
if (!got_user){
if (strcmp(result->field_name,"user") == 0 && strcmp(result->field_value,u) == 0)
got_user = true;
}else if (!got_pass){
if (strcmp(result->field_name,"pass") == 0){
if (strcmp(result->field_value,transform_password(p)) == 0)
got_pass = true;
}
}else if (!admin){
if (strcmp(result->field_name,"admin") == 0){
admin = boolean_string(result->field_value);
}
}else{
break;
}
result = result->next;
}
free_result(result);
printf("command_login(): Checking got user and got password\n");
if (!got_user || !got_pass){
send_socket(q->conn,"LOGIN: Invalid user or password\n");
return;
}
printf("command_login(): Login successfully\n");
send_socket(q->conn,"LOGIN: Success\n");
q->conn->state &= ~S_NEED_LOGIN;
q->admin = admin;
#ifndef _WIN32
home = getenv("HOME");
#else
home = getenv("HOMEPATH");
#endif
if (!home)
return;
if (!chdir(home)){
send_socket(q->conn,"LOGIN: Failed to change to user home directory %s: %s\n",home,strerror(errno));
close_connection(q->conn);
return;
}
send_socket(q->conn,"CWD: %s\n",home);
}
it loops forever here:
while (result != NULL){
if (!got_user){
if (strcmp(result->field_name,"user") == 0 && strcmp(result->field_value,u) == 0)
got_user = true;
}else if (!got_pass){
if (strcmp(result->field_name,"pass") == 0){
if (strcmp(result->field_value,transform_password(p)) == 0)
got_pass = true;
}
}else if (!admin){
if (strcmp(result->field_name,"admin") == 0){
admin = boolean_string(result->field_value);
}
}else{
break;
}
result = result->next;
}
any ideas? thanks
When you create a new DBResult
structure, do you set the next
pointer to NULL
? If you create the structure with malloc
, your test for result
would never end.
It's really difficult to know more without knowing the definition of DBResult
and the definition of store_query
. What does store_query
return?
You say you are making a list. In store_query
you don't actually set next
of ret
to tmp
. I assume that is what you want to do. Look carefully at your list construction because it is not correct.
It might help to draw a picture of your DBResult
as it gets constructed.
Okay, so what you're doing is
ret->next = tmp;
ret = tmp;
ret->next = tmp; // = ret
So you're making a circular linked list with one element. What you need to do is allocate a new element each time you add one. Then remember to free them all at the end.
edit
The pattern should look like this
DBResult* head = NULL;
DBResult* row = NULL;
while(have_rows()) {
row = malloc(sizeof(DBResult));
// load row into row
row->next = head;
head = row;
}
return head;
// later when you're all done
while(head) {
DBResult* element = head;
head = head->next;
free(element);
}
精彩评论