gpt4 book ai didi

c - 可能 `epoll_ctl`修改传递给它的 `epoll_event`结构?

转载 作者:太空狗 更新时间:2023-10-29 11:07:42 26 4
gpt4 key购买 nike

Linux kernel manpages声明epoll_ctl过程如下:

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

显然,event 参数被声明为指向 epoll_event struct 的指针。

在这个问题的上下文中,上述观察的意义在于指针类型声明之前没有 const,因此,该过程似乎被允许修改传递的内容结构。

这是某种遗漏,还是程序设计成那样,我们必须假设传递的结构确实可能在程序内部被修改?

我明白这里的声明是明确的,但是有理由相信这是一个遗漏吗?

我也看过 relevant source code in kernel 4.6 tree ,而且我没有看到太多证据表明该过程甚至打算修改结构,所以就这样。

最佳答案

找到一个相当conclusive answer on the Linux mailing list .在这里引用 Davide Libenzi,“epoll”的主要或唯一作者:

From: Davide Libenzi <davidel <at> xmailserver.org>Subject: Re: epoll_ctl and const correctnessNewsgroups: gmane.linux.kernelDate: 2009-03-25 16:23:21 GMT (7 years, 17 weeks, 1 day, 9 hours and 4 minutes ago)

On Wed, 25 Mar 2009, nicolas sitbon wrote:

Currently, the prototype of epoll_ctl is :

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

I searched in the man of epoll_ctl and google, and it seems that thestructure pointed to by event isn't modify, valgrind confirms thisbehaviour, so am I wrong? or the good prototype is

int epoll_ctl(int epfd, int op, int fd, struct epoll_event const *event);

根据目前的ctl操作来看,是的。但这样做会阻止稍后添加其他非常量操作。

  • 大卫

要点是,尽管事实上的行为不是修改结构,但接口(interface)省略了 const 修饰符 故意 因为将来可能会添加其他控制操作通过相同的系统调用,需要一个由 event 参数指向的潜在可修改结构。

我应该先访问内核邮件列表,为另一个关于 SO 的冗余信息道歉。将问题和这个答案留给后代。

关于c - 可能 `epoll_ctl`修改传递给它的 `epoll_event`结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com