Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct event对象中柔性数组成员data的使用方式是否会造成访问未申请的内存? #53

Open
TitenWang opened this issue Feb 19, 2017 · 0 comments

Comments

@TitenWang
Copy link

  您好,我对struct event对象中柔性数组成员data的使用方式有一个疑问,能否帮忙解答下?
  在run_sentinel()函数中,dnspod调用create_event()函数创建了元素个数为size个的struct event_data类型的柔性数组data[],如下:
struct event *
create_event(int size)
{
 struct event *ev =
  malloc(sizeof(struct event) + sizeof(struct event_data) * size);
 ……
 return ev;
}
  所以该柔性数组的索引范围应该是[0, size-1],但是在后续使用该数组的时候,其使用的索引值是socket描述符,如下:
int
add_event(struct event *ev, struct event_help *help)
{
 struct epoll_event e = {0};
 int ret = 0;
 int epfd = ev->ie->epfd;
 e.data.fd = help->fd;
 if (e.data.fd < 0)
  return -1;
 if (help->type == ET_READ)
  e.events = EPOLLIN; // | EPOLLET;
 if (help->type == ET_WRITE)
  e.events = EPOLLOUT; // | EPOLLET;

 /*
 * 这个地方是不是会有点问题,因为ev->data这个柔性数组的大小为size(create_event()
 * 中指定),那么索引这个柔性数组的索引范围应该是[0,size-1],但是这里却是用socket
 * fd来索引这个数组,而socket fd是有可能会大于size的。
 */
 ev->data[help->fd].cb = help->cb;
 if (help->ext != NULL)
  ev->data[help->fd].ext = help->ext;
 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, help->fd, &e);
 if (ret < 0) {
  printf("fd is %d\n", help->fd);
  perror("epoll_ctl");
 }
 return ret;
}
  而socket fd大小是有可能会大于size的,这样是不是会造成访问了未申请的内存呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant