catgets 的段错误..
GDB报告:
Program received signal SIGSEGV, Segmentation fault.
0x0000003935430cc6 in catgets () from /lib64/libc.so.6
代码:
char *msg_log( int msg_len,int msg_switch,int severity,int classi,const char *File,int Line,nl_catd cd,int msg_set,int msg_no,const char *dftfmt...)
{
va_list args;
va_start(args, dftfmt);
char *fp, *lp;
int checked = 0, len;
fp = strchr(dftfmt, '%');
if (fp != NULL)
{
lp=strrchr(dftfmt, '%');
if(fp==lp && *(fp+1)=='s')
{
fp=va_arg(args, char *);
checked = 1;
len=strlen(dftfmt);
if(strlen(fp)>BUFLEN-len)
{
fp[BUFLEN - len] = '\0';
}
}
}
va_end(args);
char catbuf[BUFLEN];
char *fmt=catgets(cd,msg_set,msg_no,(char *)dftfmt);
}
最佳答案
在 if 语句中: if (fp != NULL)
您正在检查 fp 是否为 NULL,但稍后在代码中您将取消引用 fp + 1: if(fp ==lp && *(fp+1)==')
。如果fp+1
为空,这将引发段错误。
关于c++ - 来自 "catgets"的信号 11(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45418103/