libc: handle file descriptor allocation errors

Fixes #3906
This commit is contained in:
Christian Prochaska 2020-10-06 19:07:52 +02:00 committed by Christian Helmuth
parent fe1ee05186
commit 0e01729d77

View File

@ -287,6 +287,12 @@ Libc::File_descriptor *Libc::Vfs_plugin::open_from_kernel(const char *path, int
File_descriptor *fd =
file_descriptor_allocator()->alloc(this, vfs_context(handle), libc_fd);
if (!fd) {
handle->close();
errno = EMFILE;
return nullptr;
}
handle->handler(&_response_handler);
fd->flags = flags & O_ACCMODE;
@ -360,6 +366,12 @@ Libc::File_descriptor *Libc::Vfs_plugin::open_from_kernel(const char *path, int
File_descriptor *fd =
file_descriptor_allocator()->alloc(this, vfs_context(handle), libc_fd);
if (!fd) {
handle->close();
errno = EMFILE;
return nullptr;
}
handle->handler(&_response_handler);
fd->flags = flags & (O_ACCMODE|O_NONBLOCK|O_APPEND);
@ -406,8 +418,6 @@ Libc::File_descriptor *Libc::Vfs_plugin::open(char const *path, int flags)
fd = file_descriptor_allocator()->alloc(this, vfs_context(handle), Libc::ANY_FD);
/* FIXME error cleanup code leaks resources! */
if (!fd) {
handle->close();
result_errno = EMFILE;
@ -487,8 +497,6 @@ Libc::File_descriptor *Libc::Vfs_plugin::open(char const *path, int flags)
fd = file_descriptor_allocator()->alloc(this, vfs_context(handle), Libc::ANY_FD);
/* FIXME error cleanup code leaks resources! */
if (!fd) {
handle->close();
result_errno = EMFILE;
@ -654,6 +662,12 @@ Libc::File_descriptor *Libc::Vfs_plugin::dup(File_descriptor *fd)
File_descriptor * const new_fd =
file_descriptor_allocator()->alloc(this, vfs_context(handle));
if (!new_fd) {
handle->close();
result_errno = EMFILE;
return Fn::COMPLETE;
}
new_fd->flags = fd->flags;
new_fd->path(fd->fd_path);