format不是字符串文字,也没有格式参数[-Wformat security]
format not a string literal and no format arguments [-Wformat-security]
我不确定是什么导致了这个错误
./lhapdf_wrap.cc: In function ‘void SWIG_Python_AddErrorMsg(const char*)’:
./lhapdf_wrap.cc:877:62: warning: too many arguments for format [-Wformat-extra-args]
PyErr_Format(type, "%s", PyString_AsString(old_str), mesg);
^
./lhapdf_wrap.cc:881:42: warning: format not a string literal and no format arguments [-Wformat-security]
PyErr_Format(PyExc_RuntimeError, mesg);
^
代码为:
SWIGRUNTIME void
SWIG_Python_AddErrorMsg(const char* mesg)
{
PyObject *type = 0;
PyObject *value = 0;
PyObject *traceback = 0;
if (PyErr_Occurred()) PyErr_Fetch(&type, &value, &traceback);
if (value) {
PyObject *old_str = PyObject_Str(value);
PyErr_Clear();
Py_XINCREF(type);
PyErr_Format(type, "%s %s", PyString_AsString(old_str), mesg);
Py_DECREF(old_str);
Py_DECREF(value);
} else {
PyErr_Format(PyExc_RuntimeError, mesg);
}
}
我已经查看了字符串文字错误,但%s已经存在?
使格式字符串文字显式:
printf("%s", str);
相同的警告可以通过以下片段重现:
#include <stdio.h>
int main()
{
char str[] = "hello";
printf(str);
}
main.cpp:6:12: warning: format string is not a string literal (potentially insecure)
[-Wformat-security]
编译器无法验证str
是否包含%s
。
第一个警告不匹配:字符串文字中的格式说明符(例如另一个%s
)不足,因为后面还有两个额外的参数。
相关文章:
- 引用捕获和在 lambda 中通过引用发送参数有什么区别 (C++)
- 未命名的非类型模板参数有什么意义?
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 为什么我应该在scanf()-家族成员中包含一个长度修饰符作为参数?有什么好处?使用长度修改器进行扫描的作用
- 在 C++17 中使用 const std::string& 参数有意义吗?
- 标准对 char 数组作为模板参数有什么看法?
- STD ::列表对其分配器参数有什么作用
- std::ios_base::register_callback 的索引参数有什么作用?
- 在结构C++,构造函数中的默认值和默认参数有什么区别
- 此模板函数上的最后一个参数有什么用?
- 对于属性上的 NOTIFY 信号,如果我给它一个参数有什么区别?
- 使用STD ::可选避免函数中的默认参数有任何优势
- 根据数值模板参数有条件地编译转换运算符
- 使用std :: enable_if作为函数参数与模板参数有什么区别?
- 仅当模板参数有重载运算符时
- push_back opencv中输入参数的格式不匹配
- NULL 指针参数有多贵
- std::tie 和 std::make_tuple 带有 std::ref 参数有什么区别
- (string&)string() 和 (string&)" 作为默认参数有什么区别?
- 基于模板参数有条件地定义模板类的构造函数