如何在函数调用中解决gcc [-Werror = format-security]?

问题描述 投票:0回答:1

我打电话给czmq api:

int rc = zsock_connect(updates, ("inproc://" + uuidStr).c_str());
(Note: uuidStr is of type std::string and zsock_connect expects a const char* as its second argument)

这给出了编译错误:

error: format not a string literal and no format arguments [-Werror=format-security]
int rc = zsock_connect(updates, ("inproc://" + uuidStr).c_str());
                                                               ^                                                                                                    

我试过了:

const char* connectTo = ("inproc://" + uuidStr).c_str();
int rc = zsock_connect(updates, connectTo);

并且

int rc = zsock_connect(updates, (const char*)("inproc://" + 
uuidStr).c_str());

但错误仍然存​​在。

我该如何纠正?

上下文;我正在尝试使用pip install将此代码编译为Linux上的Python扩展。在Windows上,它使用pip install编译并运行得很好,可能是编译器更宽松。

linux gcc zeromq
1个回答
1
投票

这个功能就像printf()和朋友一样,对吗?如果是这样,你遇到与printf(some_var)相同的问题 - 如果你传递的字符串中包含格式序列,你会得到未定义的行为和不好的事情,因为你没有告诉你功能期待。解决方法是做类似的事情:

int rc = zsock_connnect(updates, "inproc://%s", uuidStr.c_str());

基本上,给它一个格式,将您的字符串作为参数。

© www.soinside.com 2019 - 2024. All rights reserved.