警告:格式化不是字符串文字,并且在某些GCC中没有格式参数

时间:2015-11-19 18:20:48

标签: gcc compiler-warnings

gcc -v: gcc版本4.9.2(Ubuntu 4.9.2-10ubuntu13)

#define d_write_log_evolved(old_fmt, args...) \
    do \
    { char new_fmt[2048] = {0}; \
      time_t timep; \
      struct tm *p; \
      timep = time(&timep); \
      p = localtime(&timep); \
      sprintf(new_fmt, "%d-%d-%d %d:%d:%d [process %d][thread %lu]: ", \
              (int)(1900+p->tm_year), \
              (int)(1+p->tm_mon), \
              (int)(p->tm_mday), \
              (int)(p->tm_hour), \
              (int)(p->tm_min), \
              (int)(p->tm_sec), \
              getpid(), \
              pthread_self()); \
      strcat(new_fmt, old_fmt); \
      FILE *logfp = fopen(CCDAEMON_LOG, "a"); \
      fprintf(logfp, new_fmt, ##args); \
      fclose(logfp); \
    } \
    while (0)

d_write_log_evolved("[err] compile %s failed, thread will exit.\n", file);//here no warning.

d_write_log_evolved("socket() error, thread exit.\n"); //here have the warning.

当我将一些args传递给d_write_log_evolved()时,没有报告警告,但是没有args,总是警告:警告:格式化不是字符串文字而没有格式参数[-Wformat-security]

虽然程序可以正常运行,但这真的很麻烦。

顺便说一句,在Scientific Linux下没有这样的警告。

你能告诉我如何消除这个警告吗?谢谢!

PS。以下是预处理结果。 没有agrs:

do { char new_fmt[2048] = {0}; time_t timep; struct tm *p; timep = time(&timep); p = localtime(&timep); sprintf(new_fmt, "%d-%d-%d %d:%d:%d [process %d][thread %lu]: ", (int)(1900+p->tm_year), (int)(1+p->tm_mon), (int)(p->tm_mday), (int)(p->tm_hour), (int)(p->tm_min), (int)(p->tm_sec), getpid(), pthread_self()); strcat(new_fmt, "socket() error, thread exit.\n"); FILE *logfp = fopen("/tmp/ccdcc.log", "a"); fprintf(logfp, new_fmt); fclose(logfp); } while (0);

使用args:

do { char new_fmt[2048] = {0}; time_t timep; struct tm *p; timep = time(&timep); p = localtime(&timep); sprintf(new_fmt, "%d-%d-%d %d:%d:%d [process %d][thread %lu]: ", (int)(1900+p->tm_year), (int)(1+p->tm_mon), (int)(p->tm_mday), (int)(p->tm_hour), (int)(p->tm_min), (int)(p->tm_sec), getpid(), pthread_self()); strcat(new_fmt, "[ERR] send msg to client %s failed.\n"); FILE *logfp = fopen("/tmp/ccdcc.log", "a"); fprintf(logfp, new_fmt, inet_ntoa(host.sin_addr)); fclose(logfp); } while (0);

0 个答案:

没有答案