解决 fastdfs-nginx-module 编译报错问题

别null了 3,266 2022-09-02
  • 错误信息:
In file included from /usr/local/src/fastdfs-nginx-module-1.22/src//ngx_http_fastdfs_module.c:6:
/usr/local/src/fastdfs-nginx-module-1.22/src//common.c: In function ‘fdfs_http_request_handler’:
/usr/local/src/fastdfs-nginx-module-1.22/src//common.c:903:56: error: ‘%s’ directive output may be truncated writing up to 510 bytes into a region of size between 111 and 127 [-Werror=format-truncation=]
  902 |                 file_id_without_group = uri + 1; //skip /
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~         
  903 |                 snprintf(file_id, sizeof(file_id), "%s/%s", \
      |                                                        ^~
/usr/local/src/fastdfs-nginx-module-1.22/src//common.c:903:17: note: ‘snprintf’ output between 2 and 528 bytes into a destination of size 128
  903 |                 snprintf(file_id, sizeof(file_id), "%s/%s", \
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  904 |                         my_group_name, file_id_without_group);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/src/fastdfs-nginx-module-1.22/src//common.c:857:53: error: ‘%s’ directive output may be truncated writing up to 510 bytes into a region of size 128 [-Werror=format-truncation=]
  857 |                 snprintf(file_id, sizeof(file_id), "%s", uri + 1);
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
/usr/local/src/fastdfs-nginx-module-1.22/src//common.c:857:17: note: ‘snprintf’ output between 1 and 511 bytes into a destination of size 128
  857 |                 snprintf(file_id, sizeof(file_id), "%s", uri + 1);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:1205: objs/addon/src/ngx_http_fastdfs_module.o] Error 1
make[1]: Leaving directory '/usr/local/src/nginx-1.22.0'
make: *** [Makefile:10: build] Error 2
  • 问题分析:
    snprintf 是 C语言的库函数,以下是关于该函数的定义及描述:

int snprintf(char *str, size_t size, const char *format, …) 设将可变参数(…)按照 format 格式化成字符串,并将字符串复制到 str 中,size 为要写入的字符的最大数目,超过 size 会被截断

由此可以看出报错是因为字段长度超出限制了,可以增加目标数组的长度来解决问题,也可以使用特定语法来禁用截断告警

  • 解决办法:
    使用编译器特定的语法来禁用警告
    修改 fastdfs-nginx-module 中的 common.c 文件, 加入两行代码,禁用格式截断告警
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"

image-1662112250479

  • 参考资料:
  1. https://stackoverflow.com/questions/51534284/how-to-circumvent-format-truncation-warning-in-gcc
  2. https://www.fluentcpp.com/2019/08/30/how-to-disable-a-warning-in-cpp
  3. https://www.runoob.com/cprogramming/c-function-snprintf.html

# FastDFS