【问题标题】:How to solve gcc [-Werror=format-security] in function call?如何解决函数调用中的 gcc [-Werror=format-security]?
【发布时间】:2019-03-01 19:11:33
【问题描述】:

我有这个电话给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】:

    这个功能就像printf()和朋友一样,对吧?如果是这样,您会遇到与 printf(some_var) 相同的问题 - 如果您传递的字符串中包含格式序列,您会得到未定义的行为和坏事发生,因为没有参数存在告诉函数期望。解决方法是:

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

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

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 2023-03-28
      • 2019-10-06
      • 2023-03-08
      • 2015-09-21
      相关资源
      最近更新 更多