我有一些問題,我的連接處理線程,該線程似乎工作不錯,但我收到此異常,直到它結束:棧,而退出線程
*** stack smashing detected ***: ./listener2 terminated
Aborted (core dumped)
這是我的線程:
void* SocketHandler(void* par) {
struct thread_par* parameters=(struct thread_par*) par;
int *csock = parameters->client_desc;
MYSQL_RES *mysql_res;
int buffer_len = 128;
char client_message[buffer_len];
int bytecount;
memset(client_message, 0, buffer_len);
if ((bytecount = read(*csock, client_message, buffer_len) == -1)) {
fprintf(stderr, "Error receiving data\n");
close(*csock);
return 0;
}
printf("Received bytes %d\nReceived string \"%s\"\n", bytecount, client_message);
char *sensor_alive = strtok(client_message, "&&");
/*
* the next operations will be done only for alive sensors
*/
if (sensor_alive != NULL && strcmp(sensor_alive, "ALIVE") == 0) {
puts("sensor Alive OK");
//mysql_quwry returns
//Zero for success. Nonzero if an error occurred.
if (mysql_query(parameters->mysql_conn,
"--query--")) {
fprintf(stderr, "%s\n", mysql_error(parameters->mysql_conn));
mysql_free_result(mysql_res);
return 0;
}
mysql_res = mysql_use_result(parameters->mysql_conn);
}
puts("Query executed");
if (mysql_res->lengths > 0) {
//we will do a second query , don't need anymore this
mysql_free_result(mysql_res);
/*
* creation of a string containing the current time with the following format
* YYYY-MM-dd hh-mm-ss
*/
time_t t;
struct tm* tm;
char Date[11], Time[9];
time(&t);
tm = localtime(&t);
char time_string[20];
strftime(time_string, sizeof time_string, "%Y-%m-%d %H:%M:%S", tm);
puts(time_string);
/*
* creation of the query
* UPDATE sensors SET alive = 1, updated_at='YYYY-MM-dd hh-mm-ss' WHERE id_device='id';
*/
char* query_part_1 = "--query--";
char* query_part_2 = "--query--";
char query[sizeof(query_part_1) + sizeof(query_part_2)+ sizeof(time_string)];
strcpy(query, query_part_1);
strcpy(query + strlen(query_part_1), time_string);
strcpy(query + strlen(query_part_1) + strlen(time_string),
query_part_2);
//ALIVE UPDATE QUERY
if (mysql_query(parameters->mysql_conn, query)) {
fprintf(stderr, "%s\n", mysql_error(parameters->mysql_conn));
mysql_free_result(mysql_res);
return 0;
}
//mysql_free_result(mysql_res);
puts("Alive value updated");
//jump the part 1 of the message
strtok(NULL, "&&");
//jump the part 2 of the message
strtok(NULL, "&&");
//jump the part 3 of the message
strtok(NULL, "&&");
//jump the part 4 of the message
strtok(NULL, "&&");
char* query_begin = "--query--";
char* status_vector = strtok(NULL, "&&"); //part 5
strtok(NULL, "&&"); //part 6 don't needed
char* local_time = strtok(NULL, "&&"); //part 7
char* timer = strtok(NULL, "&&"); //part 8
char* created_at = strtok(NULL, "&&"); //part 9
int total_len = 150;
char query2[total_len];
snprintf(query2, sizeof(query2), "%s('%s','%s','%s','%s','%s');",
query_begin, status_vector, local_time, timer, created_at,
time_string);
puts(query2);
if (mysql_query(parameters->mysql_conn, query2)) {
fprintf(stderr, "%s\n", mysql_error(parameters->mysql_conn));
return 0;
}
//mysql_free_result(mysql_res);
puts("Value updated");
}
/*
* ending thread operations
*/
//close(*csock);
//free(csock);
puts("exiting thread");
//pthread_exit(0);
return 0;
}
這是我的輸出
Received bytes 0
Received string "ALIVE&&000000000035&&STATION NUM. 1&&40.1234234&&14.1234234&&0000000&&192.___&&00&&18:9:49&&2&&"
sensor Alive OK
Query executed
2014-10-16 18:08:10
Alive value updated
INSERT INTO -------;
Value updated
exiting thread
*** stack smashing detected ***: ./listener2 terminated
Aborted (core dumped)
我不是在C那麼好,所以我不知道如何解決我的親blem 你能幫助我嗎?
在'valgrind'下運行你的應用程序,它經常檢測到這種錯誤。 – 2014-10-16 16:35:23
看起來你沒有向我們展示你的實際代碼或你的實際輸出 - 你說'puts(query2);'line print'INSERT INTO -------;'但是根據它的代碼'--query - ( '.. '' .. '' .. '' .. '' ..');'。沒有準確的信息,我們無法幫助你。 – 2014-10-16 16:58:06
另外,'Received bytes 0'消息與非空字符串不是一個好兆頭。 – 2014-10-16 17:05:40