Python 格式不是字符串文字且没有格式参数[-Wformat security]

Python 格式不是字符串文字且没有格式参数[-Wformat security],python,c++,string,format,literals,Python,C++,String,Format,Literals,我不确定是什么导致了这个错误 ./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: 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);

同样的警告可以通过以下方式再现:

#包括
int main()
{
char str[]=“你好”;
printf(str);
}
main.cpp:6:12:警告:格式字符串不是字符串文字(可能不安全)
[-Wformat security]
编译器无法验证
str
是否包含
%s


<>第一个警告有一个错配:在字符串文字中,格式说明符(例如,另一个代码> %s <代码>)不足,因为后面有两个附加的参数。

我理解C++代码段中的意思。那么您是说需要更改的代码是:
PyErr\u格式(PyExc\u RuntimeError,mesg)
那么这里需要一个“%s”?@tv49使编译器满意,是的。第一个需要两个。
PyErr\u格式(“%s%s”,PyExc\u RuntimeError,mesg)PyErr\u格式(类型,“%s%s”,PyString\u AsString(old\u str),mesg)而对于第二个
PyErr\u格式(PyExc\u运行时错误,“%s”,mesg)当我在Fedora23x86u64中安装时,还有类似
PyErr\u格式(PyExc\u RuntimeError,mesg)的代码。宏A的解决方案解决了我的问题。非常感谢。
#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]