作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的连接处理程序线程有一些问题,该线程似乎工作正常,但我收到此异常,直到它终止:
*** 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语言不太好,所以我不知道如何解决我的问题你能帮我吗?
最佳答案
您的查询
变量太小:
char query[sizeof(query_part_1) + sizeof(query_part_2) + sizeof(time_string)];
您已声明它足够大,只能容纳 query_part_1
、query_part_2
和 time_string
中的字符数。您忘记考虑字符串终止 NUL 字节,因此第三个 strcpy
调用将写入超过 query
的末尾并损坏堆栈。
关于c - 退出线程时检测到堆栈崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26409320/
我是一名优秀的程序员,十分优秀!