Skip to content

Commit

Permalink
Merge pull request ElektraInitiative#4654 from Eiskasten/lib/io
Browse files Browse the repository at this point in the history
[FLOSS H2] io file flag check
  • Loading branch information
markus2330 authored Nov 17, 2022
2 parents d196207 + 48ee754 commit 23c5bb7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ The text below summarizes updates to the [C (and C++)-based libraries](https://w
- <<TODO>>
- <<TODO>>

### <<Library>>
### io

- <<TODO>>
- Check file flags for elektraIoFdSetFlags: file flags must be exactly one of: read only, write only or read write _(Richard Stöckl @Eiskasten)_
- <<TODO>>
- <<TODO>>

Expand Down
12 changes: 10 additions & 2 deletions src/libs/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <kdblogger.h>
#include <kdbprivate.h>

#include <fcntl.h>
#include <stdio.h>

int elektraIoContract (KeySet * contract, ElektraIoInterface * ioBinding)
Expand Down Expand Up @@ -409,8 +410,15 @@ int elektraIoFdSetFlags (ElektraIoFdOperation * fdOp, int flags)
ELEKTRA_LOG_WARNING ("operation was NULL");
return 0;
}

fdOp->flags = flags; // TODO check if flags are valid
int rdwrFlags = flags & (O_RDONLY | O_WRONLY | O_RDWR);
// `open(2)` requires exactly one of these:
if (!(rdwrFlags == O_RDONLY || rdwrFlags == O_WRONLY || rdwrFlags == O_RDWR))
{
ELEKTRA_LOG_NOTICE ("file flags must be exactly one of: read only, write only or read write. actual flag is: %0d", flags);
return -1;
}
// since custom flags are allowed by `fcntl.h`, no further checks are required
fdOp->flags = flags;
return 1;
}

Expand Down

0 comments on commit 23c5bb7

Please sign in to comment.