microsoft / microsoft/STL

`fiopen` handling of non-standard flags

Open
#2,093 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question vNext
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

The implementation strategy in https://github.com/microsoft/STL/blob/main/stl/src/fiopen.cpp is quite unnatrual, which leaves to problems for non-standard flags, even the expect behaviors are not well-documented.

What does ios_base::_Nocreate exactly mean?

By https://github.com/microsoft/STL/blob/cc515bbb42dbbfba184527ad2e7c093a78c10442/stl/src/fiopen.cpp#L52, ios_base::_Nocreate implies ios_base::in. But what happens for a write-only mode, i.e. ios_base::out or ios_base::app? Is it the tradition meaning of "nocreate" in this case?

TOCTTOU issue on handling io_base::_Noreplace

This one seems a real bug. Between https://github.com/microsoft/STL/blob/cc515bbb42dbbfba184527ad2e7c093a78c10442/stl/src/fiopen.cpp#L71 and https://github.com/microsoft/STL/blob/cc515bbb42dbbfba184527ad2e7c093a78c10442/stl/src/fiopen.cpp#L80, if a file named by filename is created, the check is voided, so the flag is useless in this case. It looks like the same to the infamous anti-pattern of multiple calls to POSIX open to make sure the exclusive open-and-create semantics. The idiomatic correct way is using O_CREAT | O_EXCL in a single call, to enforce the atomicity.

Related implementation question

The concerned functions are implemented by the stdio extension function _fsopen. Why not use lowio (i.e. _wsopen) instead? This can easily resolve the correctness problems mentioned above, and it could be a bit more efficient due to the simpler mode translation and parsing (in the underlying implementation; once you have the implementation-specific knowledge, it seems not difficult to bypass the redundant passing like __acrt_stdio_parse_mode in _wfdopen) even if an object referenced by FILE* is still needed to be created later.

If lowio is not an appropriate choice for some coding policies I don't know, is it considerable to use C11-style "x" modes in https://github.com/microsoft/STL/blob/cc515bbb42dbbfba184527ad2e7c093a78c10442/stl/src/fiopen.cpp#L14?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stl/src/fiopen.cpp, especially the linked handling around lines 14, 52, 71, and 80. Review the _fsopen and _wsopen paths and the behavior of _Nocreate and _Noreplace; done means the non-standard flag semantics and the reported _Noreplace race are resolved or clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.