2014-10-16 142 views
-2

我有一些問題,我的連接處理線程,該線程似乎工作不錯,但我收到此異常,直到它結束:棧,而退出線程

*** 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 你能幫助我嗎?

+0

在'valgrind'下運行你的應用程序,它經常檢測到這種錯誤。 – 2014-10-16 16:35:23

+1

看起來你沒有向我們展示你的實際代碼或你的實際輸出 - 你說'puts(query2);'line print'INSERT INTO -------;'但是根據它的代碼'--query - ( '.. '' .. '' .. '' .. '' ..');'。沒有準確的信息,我們無法幫助你。 – 2014-10-16 16:58:06

+0

另外,'Received bytes 0'消息與非空字符串不是一個好兆頭。 – 2014-10-16 17:05:40

回答

0

query變量太小:

char query[sizeof(query_part_1) + sizeof(query_part_2) + sizeof(time_string)]; 

你已經宣佈它爲大到足以容納唯一的字符數在query_part_1query_part_2time_string。您忘記考慮字符串終止NUL字節,因此第三個strcpy調用將會在query的末尾寫入並破壞堆棧。

+0

看起來好像非常時間有一個C網絡問題,空終止出現作爲一個問題:( – 2014-10-16 18:01:11

0

你有一個緩衝區溢出,這就是爲什麼你得到「堆棧粉碎檢測」消息。檢查你的代碼。 但我查看了你的代碼,並且無法找到任何溢出,誠實地:)

0

在編譯時使用-fno-stack-protector作爲選項。這將導致獲得不同的錯誤,主要是分段錯誤。正如提到的其他答案,這顯然是緩衝區溢出。您可以使用strace或gdb運行程序來檢測代碼中的問題。 e.g strace ./a.outgdb a.out