gcc - C #define causes Seg Fault? -
here function trying working on red hat 6..
and have little experience c, , using #define, i'm unsure portion trying 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
this error getting:
according gdb getting seg fault @ function call:
get_service_by_name(sp, serv, prot);
here gdb output:
program received signal sigsegv, segmentation fault. [switching thread 0x456c6c90 (lwp 14645)] 0x420b1e69 in gi_get_port (serv=unhandled dwarf expression opcode 0x9c ) @ /home/user1/common/src/socket.c:282 282 get_service_by_name(sp, serv, prot); current language: auto; c
here how function called:
int gi_get_port (char *serv, char *prot) /* obtain port named service */ { int p, s; /* data resolving service name 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; }
this code once preprocessor executed :
int gi_get_port (char *serv, char *prot) /* obtain port named service */ { int p, s; /* data resolving service name 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; }
as see, should checking 'sp' being 'null' before htons() on port.
Comments
Post a Comment