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 password\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
DBResultstructure, do you set thenextpointer toNULL? If you create the structure withmalloc, your test forresultwould never end.It’s really difficult to know more without knowing the definition of
DBResultand the definition ofstore_query. What doesstore_queryreturn?You say you are making a list. In
store_queryyou don’t actually setnextofrettotmp. 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
DBResultas it gets constructed.