我的连接处理程序线程有问题,线程似乎工作正常,但我收到此异常,直到它终止:
*** 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方面不太好,所以我不知道如何解决我的问题 你能救我吗?
答案 0 :(得分:0)
你有一个缓冲区溢出,这就是你得到"堆栈粉碎检测到的原因"信息。检查你的代码。 但我查看了你的代码,并且无法找到任何超支,老实说:)
答案 1 :(得分:0)
编译时使用-fno-stack-protector
作为选项。这将导致出现不同的错误,主要是分段错误。正如其他答案所提到的,这显然是缓冲区溢出。您可以使用strace或gdb运行程序,以检测代码中问题的确切位置。例如strace ./a.out
或gdb a.out
答案 2 :(得分:0)
您的query
变量太小:
char query[sizeof(query_part_1) + sizeof(query_part_2) + sizeof(time_string)];
您已声明它足够大,只能容纳query_part_1
,query_part_2
和time_string
中的字符数。您忘记考虑字符串终止的NUL字节,因此第三个strcpy
调用将写入query
的末尾并破坏堆栈。