C #define导致Seg Fault?

时间:2011-06-30 23:02:51

标签: c gcc compilation

这是我尝试使用Red Hat 6的功能..

我对C的经验很少,特别是使用#define,所以我不确定这个部分甚至试图做什么: 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我在这个函数调用中遇到了一个seg错误:

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;
}

2 个答案:

答案 0 :(得分: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;
}

如您所见,在端口上执行htons()之前,您应该检查'sp'是否为'NULL'。

答案 1 :(得分:0)

你的HOSTBUFFERLENGTH有多长时间?

编译并运行代码,如果将HOSTBUFFERLENGTH设置为1,我可以生成类似的崩溃,但如果将其设置为2000,它看起来是有效的。

如果服务名称名称无效(例如“akhaha”而不是“http”,那么GSBN_serv_result为NULL且SP为NULL,SP-> s_port也会导致崩溃....