2011-06-30 79 views
0

這裏是我正在努力工作在紅帽子6上的功能..C#define導致Seg Fault?

而且我對C很少有經驗,特別是使用#define,所以我不確定這部分甚至試圖do:SP-> s_port = htons(SP-> s_port);

#ifdef __linux 
#define GET_SERVICE_BY_NAME(SP, SERVICE, PROTOCOL)       \ 
    char   GSBN_servbuf[HOSTBUFFERLENGTH] = {0};      \ 
    struct servent GSBN_sp;             \ 
    struct servent *GSBN_serv_result;           \ 
    int    GSBN_s = 0;            \ 
    GSBN_s = getservbyname_r(SERVICE,           \ 
         PROTOCOL,            \ 
         &GSBN_sp,            \ 
         GSBN_servbuf,           \ 
         sizeof(GSBN_servbuf),         \ 
         &GSBN_serv_result);         \ 
    SP = GSBN_serv_result;             \ 
    SP->s_port = htons(SP->s_port);           \ 
    if (SP && SOCKET_DEBUG) {             \ 
     printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", \ 
      get_timestamp(), SP->s_name, SP->s_port, SP->s_proto);   \ 
     }                   \ 
    if (SP == NULL) {               \ 
     fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n", \ 
       get_timestamp(), SERVICE);          \ 
    } 
#else 
#define GET_SERVICE_BY_NAME(SP, SERVICE, PROTOCOL)       \ 
    char   GSBN_servbuf[HOSTBUFFERLENGTH] = {0};      \ 
    struct servent GSBN_serv_result;           \ 
    SP = getservbyname_r(SERVICE,            \ 
         PROTOCOL,            \ 
         &GSBN_serv_result,         \ 
         GSBN_servbuf,           \ 
         sizeof(GSBN_servbuf));        \ 
    if (SP && SOCKET_DEBUG) {             \ 
     printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", \ 
      get_timestamp(), SP->s_name, SP->s_port, SP->s_proto);   \ 
    }                   \ 
    if (SP == NULL) {               \ 
     fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n", \ 
       get_timestamp(), SERVICE);          \ 
    } 
#endif 

這是我收到的錯誤:

根據gdb的我在調用這個函數得到一個賽格故障:

GET_SERVICE_BY_NAME(SP,SERV,PROT);

下面是GDB輸出:

Program received signal SIGSEGV, Segmentation fault. 
[Switching to Thread 0x456c6c90 (LWP 14645)] 
0x420b1e69 in gi_get_port (serv=Unhandled dwarf expression opcode 0x9c 
) 
    at /home/user1/Common/src/socket.c:282 
282   GET_SERVICE_BY_NAME(sp, serv, prot); 
Current language: auto; currently c 

下面是該函數的調用:

int gi_get_port (char *serv, char *prot) 
/* obtain the port for the named service */ 
{ 
    int p, s; 

    /* Data for resolving service name to a socket description. */ 
    struct servent *sp = NULL; 

    GET_SERVICE_BY_NAME(sp, serv, prot); 

    if (sp != NULL) { 
    p = sp->s_port; 
    } else { 
    p = -1; 
    }; 

    return p; 
} 
+0

什麼是SP類型'? –

+1

htons =「主機到網絡短路」:將主機字節順序(英特爾上的LSB)的短整數轉換爲servent結構的網絡字節順序(MSB)。 – Rup

+0

我認爲sp是struct servent *,因爲它是聲明爲 struct ser vent * sp = NULL; – systemoutprintln

回答

2

這是你的代碼是什麼樣子一旦執行預處理:

int gi_get_port (char *serv, char *prot) 
/* obtain the port for the named service */ 
{ 
    int p, s; 

    /* Data for resolving service name to a socket description. */ 
    struct servent *sp = NULL; 

    char   GSBN_servbuf[HOSTBUFFERLENGTH] = {0};      
    struct servent GSBN_sp;             
    struct servent *GSBN_serv_result;           
    int    GSBN_s = 0;            
    GSBN_s = getservbyname_r(serv,           
         prot,            
         &GSBN_sp,            
         GSBN_servbuf,           
         sizeof(GSBN_servbuf),         
         &GSBN_serv_result);         
    sp = GSBN_serv_result;              
    sp->s_port = htons(SP->s_port);           
    if (sp && SOCKET_DEBUG) {             
     printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", 
      get_timestamp(), sp->s_name, sp->s_port, sp->s_proto);   
     }                   
    if (sp == NULL) {               
     fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n",  
       get_timestamp(), serv);          
    } 

    if (sp != NULL) { 
    p = sp->s_port; 
    } else { 
    p = -1; 
    }; 

    return p; 
} 

正如你所看到的,在你做之前你應該檢查'sp'是'NULL' htons()在端口上。

0

你的HOSTBUFFERLENGTH有多長?

編譯和運行代碼,如果HOSTBUFFERLENGTH設置爲一個我可以生成一個類似的崩潰,但它看起來像它的作品,如果它被設置爲2000 ....

,如果服務名稱的名字是無效(例如「akhaha」,而不是說「http」,那麼GSBN_serv_result是NULL並且SP是NULL,並且SP-> s_port也會導致崩潰.....

+0

這是什麼hostbufferlength設置爲 #define HOSTBUFFERLENGTH 1024 – systemoutprintln

+0

問題是與SP是一個空指針無效的服務名稱...您需要檢查NULL。 – Soren